Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Scanner class reading strings

I've created a scanner class to read through the text file and get the value what I'm after. Let's assume that I have a text file contains.

List of people: length 3

1 : Fnjiei : ID 7868860 : Age 18

2 : Oipuiieerb : ID 334134 : Age 39

3 : Enekaree : ID 6106274 : Age 31

I'm trying to get a name and id number and age, but everytime I try to run my code it gives me an exception. Here's my code. Any suggestion from java gurus?:) It was able to read one single line....... but no more than a single line of text.

    public void readFile(String fileName)throws IOException{
    Scanner input = null;
    input = new Scanner(new BufferedReader(new FileReader(fileName)));
    try {
        while (input.hasNextLine()){
            int howMany = 3;
            System.out.println(howMany);
            String userInput = input.nextLine();
            String name = "";
            String idS = "";    
            String ageS = "";
            int id;
            int age;
            int count=0; 
            for (int j = 0; j <= howMany; j++){
                for (int i=0; i < userInput.length(); i++){
                    if(count < 2){ // for name
                        if(Character.isLetter(userInput.charAt(i))){
                            name+=userInput.charAt(i); // store the name
                        }else if(userInput.charAt(i)==':'){
                            count++;
                            i++;
                        }
                    }else if(count == 2){ // for id
                        if(Character.isDigit(userInput.charAt(i))){
                            idS+=userInput.charAt(i); // store the id
                        }
                        else if(userInput.charAt(i)==':'){
                            count++;
                            i++;
                        }
                    }else if(count == 3){ // for age
                        if(Character.isDigit(userInput.charAt(i))){
                            ageS+=userInput.charAt(i); // store the age
                        }
                    }
                    id = Integer.parseInt(idS); // convert id to integer
                    age = Integer.parseInt(ageS); // convert age to integer
                    Fighters newFighters = new Fighters(id, name, age);
                    fighterList.add(newFighters);
                }
                userInput = input.nextLine();
            }
        }
    }finally{
        if (input != null){
            input.close();
        }
    }
}

My appology if my mere code begs to be changed.

Edited It gives me a number format exception!!! I dont know how many empty space would be there between these values.

like image 283
Max Avatar asked Sep 09 '26 18:09

Max


1 Answers

Here's a solution that uses only Scanner API, the important one being findInLine. It can handle minor syntactic variations in the input format, and yet it's very readable, requiring no need for fancy regex or magic array indices.

    String text =
        "List  of @#%^$ people : length  3  !@%# \n" + 
        "1 :   Fnjiei   : ID 7868860 ::: Age 18\n" +
        "   2: Oipuiieerb : ID 334134 : Age 39 (old, lol!) \r\n" + 
        " 3 : Enekaree : ID 6106274 => Age 31\n";
    Scanner sc = new Scanner(text);

    sc.findInLine("length");
    final int N = sc.nextInt();

    for (int i = 0; i < N; i++) {
        sc.nextLine();
        sc.findInLine(":");
        String name = sc.next();
        sc.findInLine("ID");
        long id = sc.nextLong();
        sc.findInLine("Age");
        int age = sc.nextInt();
        System.out.printf("[%s] %s (%s)%n", id, name, age);
    }

This prints:

[7868860] Fnjiei (18)
[334134] Oipuiieerb (39)
[6106274] Enekaree (31)

API links

  • Scanner.findInLine(Pattern pattern)
    • Attempts to find the next occurrence of the specified pattern ignoring delimiters.
    • Use this Pattern.compile overload if performance is an issue
like image 81
polygenelubricants Avatar answered Sep 11 '26 09:09

polygenelubricants



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!