John
John

Reputation: 177

Reading from a file, java using Scanner

I have been trying out reading my file using Scanner.

The error i am getting is NoSuchElementException: No line found All of the arrays are String arrays to store information from the file. The delimiter being used is ";".

private void choice() {
        File file = new File("info.txt");

        try {
            Scanner read = new Scanner(file);
            read.useDelimiter(";");
            while (read.hasNextLine()){
                if (read.hasNext()){
                    realName[i] = read.next();
                } else {
                    break;
                }
                if (read.hasNext()){
                    userName[i] = read.next();                       
                } else {
                    break;
                }
                if (read.hasNext()){
                    password[i] = read.next();
                } else {
                    break;
                }
                if (read.hasNext()){
                    address[i] = read.next();
                } else {
                    break;
                }
                if (read.hasNext()){
                    contact[i] = read.next();
                } else {
                    break;
                }
                if (read.hasNext()){
                    cardType[i] = read.next();
                } else {
                    break;
                }
                if (read.hasNext()){
                    cardNo[i] = read.next();
                } else {
                    break;
                }
                i++;
             }
        }
        catch ......
    }

If I use the code above, I will always be getting the NoSuchElementException: No line found.

Now I use the code below.. everything will be printed out nicely with no errors.

private void choice() {
        File file = new File("info.txt");

        try {
            Scanner read = new Scanner(file);
            read.useDelimiter(";");
            while (read.hasNextLine()){
                realName = read.nextLine();
                System.out.println(realName);
            }
        }
        catch ......
    }

Everything will be printed nicely line by line. There will not be any problems but this is not what I wanted. I want to store them in arrays.

Is there anything that I have done wrong for the 1st part of the coding?

I also use read.hasNext() rather than read.hasNextLine(). But there are also errors.

Any kind soul could help?

==Edit Sorry. I did not show the datas in the info.txt, it goes like this.

Mr Dash;dash;dash123;Dash Lane 1;414924934;VISA;1219240241029021092
Mr Long;long;long123;Long Lane 1;3924929;MASTERCARD;123902039109329

Upvotes: 2

Views: 1601

Answers (2)

Achintya Jha
Achintya Jha

Reputation: 12843

You are calling hasNextLine once and nextLine more than once inside your loop so you might get error if it reaches EOF. Since you are using delimiter I think you are trying to read each word from single line in a single iteration. Try this:

int i=0;
while (read.hasNextLine()){                
    realName[i] = read.next();
    userName[i] = read.next();
    password[i] = read.next();
    address[i] = read.next();
    contact[i] = read.next();
    cardType[i] = read.next();
    cardNo[i] = read.next();
    i++;               
}

Upvotes: 2

sanbhat
sanbhat

Reputation: 17622

Before every read.nextLine() you must check read.hasNextLine(), otherwise you might get exception!

May be somewhere in your while loop, you are doing read.nextLine() and there is no next line (EOF reached)

      while (read.hasNextLine()){
            for(int i=0; i<=10; i++){
                    realName[i] = read.nextLine();
                    userName[i] = read.nextLine(); // check for read.hasNextLine();
                    password[i] = read.nextLine(); // check for read.hasNextLine();
                    address[i] = read.nextLine(); // check for read.hasNextLine();
                    contact[i] = read.nextLine(); // check for read.hasNextLine();
                    cardType[i] = read.nextLine(); // check for read.hasNextLine();
                    cardNo[i] = read.nextLine(); // check for read.hasNextLine();
           }
      }

Upvotes: 1

Related Questions