Connor
Connor

Reputation: 23

No Line Found error while reading text file

I'm attempting to grab data sets from a file named poll.txt and then use relevant data.

The content of the poll.txt:

CT 56 31 7 Oct U. of Connecticut

NE 37 56 5 Sep Rasmussen

AZ 41 49 10 Oct Northern Arizona U.

The source code, ElectoralVotes.java:

import java.io.*;
import java.util.*;

public class ElectionResults {

    public static void main(String[] args) throws FileNotFoundException {
        int dVotes = 0;
        int sVotes = 0;

        Scanner scanner = new Scanner(new File("poll.txt"));
        String[] nonDigits = new String[29];
        int[] Digits = new int[10];
        int i = 0;
        int j = 1;
        while (scanner.hasNextLine()) {
            while (scanner.hasNext()) {
                nonDigits[i++] = scanner.next();
            }
            i = 0;
            while (j <= 3) {
                Digits[i] = Integer.parseInt(nonDigits[j]);
                i++;
                j++;
            }

            if (Digits[0] > Digits[1]) {
                sVotes += Digits[2];
            } else {
                dVotes += Digits[2];
            }
            scanner.nextLine();
        }
    }
}

However when I run the program, only one of the lines is being used before giving an exception:

Exception in thread "main" java.util.NoSuchElementException: No line found error.

I've tried moving around the "scanner.nextLine();" statement to no avail. The program works fine if I don't ask for a nextLine but I obviously need it, and I can't seem to figure out what's wrong.

Upvotes: 0

Views: 2279

Answers (4)

Connor
Connor

Reputation: 23

I finally got it to work. The other problem was that j was not being reinitialized after each loop and was reusing the data still in the Digits array. Here is the code that ended up working.

import java.io.*;
import java.util.*;

public class ElectionResults
{
  public static void main (String[] args) 
  throws  FileNotFoundException {
     int dVotes = 0;
     int sVotes = 0;

     Scanner scanner = new Scanner(new File("poll.txt"));
     String [] nonDigits = new String [29];
     int [] Digits = new int [10];
     int i = 0;

     while(scanner.hasNextLine()){
        String line = scanner.nextLine();

        Scanner linescan = new Scanner(line);
        i=0;
        while(linescan.hasNext()){
           String temp = linescan.next();

           nonDigits[i]=temp;
           i++;
        }

        i=0;
        int j = 1;
        while(j<=3)
        {
           Digits[i] = Integer.parseInt(nonDigits[j]);
           i++;
           j++;
        }
        if (Digits[0]>Digits[1])
           sVotes += Digits[2];
        else
           dVotes += Digits[2];  

     }
     System.out.println("Smith Votes:\t" + sVotes);
     System.out.println("Dow Votes:\t" + dVotes);
     if (sVotes > dVotes)
        System.out.println("Smith is currently in the lead!");
     if (dVotes > sVotes)
        System.out.println("Dow is currently in the lead!");
        if(dVotes == sVotes)
        System.out.println("It's a tie!");
  }
}

Upvotes: 0

Aung Thaw Aye
Aung Thaw Aye

Reputation: 437

Please try this..

If the format of poll file will not be changed and it is fixed, then please try the following code..

String area = null;
String personName = null;
while (scanner.hasNextLine())
{
    // e.g to extract "CT" from "CT 56 31 7 Oct U. of Connecticut"
    area = scanner.next(); 

    // e.g to extract "56" from "CT 56 31 7 Oct U. of Connecticut"
    dVotes = scanner.nextInt(); 

    // e.g to extract "31" from "CT 56 31 7 Oct U. of Connecticut"
    sVotes = scanner.nextInt();

    // e.g to skip "7" from "CT 56 31 7 Oct U. of Connecticut"
    scanner.nextInt();

    // e.g to skip "Oct" from "CT 56 31 7 Oct U. of Connecticut"
    scanner.next();

    // e.g to extract "U. of Connecticut" from "CT 56 31 7 Oct U. of Connecticut"
    personName = scanner.nextLine();
    System.out.println(area + " " + dVotes + " " + sVotes + " " + personName);
}

Above code will be working fine if the file follows the format of your current poll.txt. You may need to add your calculation there.. I just show how to extract values from poll.txt.

Upvotes: 1

coderplus
coderplus

Reputation: 5913

The data gets completely consumed in the inner loop. Jon has already pointed that out. This example might help you out :-)

public class ElectionResults {

public static void main(String[] args) throws FileNotFoundException {

    Scanner scanner = new Scanner(new File("c:\\poll.txt"));
    while (scanner.hasNextLine()) {
         String line = scanner.nextLine();
         Scanner linescanner = new Scanner(line);
         //this inner loop will scan through the line.
         while(linescanner.hasNext())
         {
             System.out.print(linescanner.next() + " ");
         }
         System.out.println();
         linescanner.close();
    }
    scanner.close();
}

}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500695

You're calling scanner.nextLine() at the end of your loop, which will try read the next line. You're then just throwing away the data!

I suspect that's throwing the exception, because you've run out of data. I suspect that your loop of:

while(scanner.hasNext()){
   nonDigits[i++] = scanner.next();
}

is completely consuming the data. (The behaviour of Scanner has never been terribly clear to me from the docs, admittedly.) I suspect you should actually be reading a line at a time, with:

while (scanner.hasNextLine()) {
    String line = scanner.nextline();
    // Now use line
}

... and parsing the line separately.

Upvotes: 1

Related Questions