Home > database >  How to ignore whitespace and read from a text file in java?
How to ignore whitespace and read from a text file in java?

Time:05-29

I am having troubles with the following code.

public class ReadFromFile {
    public static void main(String[] args) {
        Scanner scan=null;
        try {
            scan=new Scanner(new File("AllAccInfo.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String input=scan.nextLine();
        while(input!=null) {
            String[] data=input.split("[\\: \t]");
            try {
                input=scan.nextLine();
            }
            catch(NoSuchElementException e) {
                break;
            }
        }
    }   
}

Here is my Text File

SavingAccount: Sourav Roy Komol 25478963    79863   77010.0 5000.0  45000.0
CurrentAccount: Shami Kaiser    1234789 22167   88000.0 5000.0

The Output of the code is

Roy
Kaiser

I want to pick the the whole name like Sourav Roy Komol and Shami Kaisar into one String variable. Such that 1st My Programme 1st read the 1st line and pick the "Sourav Roy Komol" will pick the "String x". Same Process same for "Shami Kaisar". How can I pick these values with ignoring whitespace? Here 1234789 and 22167 is also a String variable.

CodePudding user response:

Since the input data is tab-delimited (I copy pasted your input data where each field is separate by a tab), you could easily separate the fields using a StringTokenizer. Here's a simple example showing how that could work, using the first line of your input example, and also showing the output when it runs.

String one = "SavingAccount: Sourav Roy Komol\t25478963\t79863\t77010.0\t5000.0\t45000.0";
StringTokenizer tokenizer = new StringTokenizer(one, "\t");
while (tokenizer.hasMoreTokens()) {
    System.out.println("token: "   tokenizer.nextToken());
}

And the output here:

token: SavingAccount: Sourav Roy Komol
token: 25478963
token: 79863
token: 77010.0
token: 5000.0
token: 45000.0

This should be enough of working code to get you going. You will probably want to exclude the "SavingAccount: " part from the first token, leaving just the name ("Sourav Roy Komol"). There are a few options for how you could do that, but the code here should be enough to address your parsing issues.

CodePudding user response:

Since there is no full code so I am assuming you are looking for splitting your string so that name will appear as one string.

In that case, you can use below regex in your split & later trim() to remove trailing whitespace:

String[] data=input.split("[\\^0-9]");
String name = data[0].trim();  //this will trim trailing whitespace of your returned string.

If you print, String name in console, you will see full name with trailng whitespace removed. If you want to keep trailing white space, you can ignore doing trim() to returned string.

This suggestion assumes that your input file will always have names at first place.

CodePudding user response:

Your regex will only split for one of the three characters you specified. What you should do is to use a capturing group to either split by a colon or a set of white spaces followed by one or more digits. This will give you: the first string before the colon, the name and then each number.

public class Main {
    public static void main(String[] args) {
        Scanner scan = null;
        try {
            scan = new Scanner(new File("AllAccInfo.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        String[] data;
        String input = scan.nextLine();
        while (input != null) {
            data = input.split("(:|\\s (?=\\d ))");
            try {
                System.out.println(data[1].trim());
                input = scan.nextLine();
            } catch (NoSuchElementException e) {
                break;
            }
        }
    }
}
Output
Sourav Roy Komol
Shami Kaiser

CodePudding user response:

Method 1 Assuming you have a plan txt as your database and knows how to get only names from the txt file. Then do this

  1. Replace whitespace with underscore st = st.replaceAll("\\s ","_"). This gets all names to have an underscore like this Sourav_Roy_Komol
  2. Then when rendering it to the screen Do this st = st.replaceAll("_"," "), this replaces the underscore with a whitespace.

Method 2 Replace split("[\: \t]") to split("[\s ]");

Hope is clear enough?.

  • Related