Jupiter
Jupiter

Reputation: 1

Beginner Java Programmer Struggling (no line found)

The goal is is to ask what the capitol is of each state, state whether right or wrong, and then give total number of correct answers at the end. When compiling it gives me no errors, but when i try to run it it says: java.uitl.NoSuchElementException: No line found (in java.util.Scanner)

There are probably more errors then just this one, but i would rather just focus on this one first. Any feedback would be appreciated, thanks!

Code:

 import java.util.*;
import java.io.*;
public class States
{
    public static void main(String[] args)
    throws java.io.IOException
     {
        Scanner inputt = new Scanner (System.in);
        String[][] cs = new String [50][1];
        while (true)
        {
            String in = " ";
            boolean ans = false;
            int tally = 0;
            int count = 0;
            load(cs);
            in = input(count, cs);
            ans = check(in, count, cs);
            if (ans == true)
            {
                tally ++;
                continue;
            }
            count= count +2;
            if (count > 100)
            {
                System.out.println("The count of correct answers is " + tally + ".");
                break;
            }
        }//end main method while loop
     }//end main method
    public static void load(String[][] cs)
    throws java.io.IOException
    {
       String filName = " ";
       filName = "C://Users/Troy/Documents/Class Documents/CSC 225/StateCapitals.txt";
       Scanner inputt = new Scanner(new File(filName));
       for (int row = 0; row < cs.length; row ++)
       {
           for (int col = 0; col < cs[row].length; col ++)
           {
           cs[row][col] = inputt.nextLine();
        }
        }//end for loop
        inputt.close();
    }//end load method
    public static String input(int count, String[][] cs)
    {
        Scanner inputt = new Scanner (System.in);
        System.out.print("What is the capital of " + cs[count][0] + "? -");
        String t = inputt.nextLine().trim().toUpperCase();
        return t;
    }//end input method
    public static Boolean check(String in, int count, String [][] cs)
    {
      boolean correct = false;
      if (cs[count +1][0].compareTo(in)==0)
      {
          correct=true;
          System.out.println("Your answer is correct.");
        }
       else
       {
         System.out.println("The correct answer should be " + cs[count + 1][0]);
        }
        return correct;
    }//end check method
}//end class

Upvotes: 0

Views: 81

Answers (1)

Michael Queue
Michael Queue

Reputation: 1400

You are not reading in your file correctly using the scanner class in the beginning of your load method.

Try using something like this

ArrayList<String> lines = new ArrayList<>();

JOptionPane.showMessageDialog(null, "Please choose a file");        
JFileChooser input = new JFileChooser();
int a = input.showOpenDialog(null);
String file = "";

if (a == JFileChooser.APPROVE_OPTION) {
    File selectedFile = input.getSelectedFile();
    file = selectedFile.getPath();
}

//use file input to read in line one at a time
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
    lines.add(line);
}

Alternatively, if you want to use the Scanner class you could use something like this

// Location of file to read
File file = new File("data.txt");

try {
    Scanner scanner = new Scanner(file);

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        System.out.println(line);
    }
    scanner.close();
} 

catch (FileNotFoundException e) {
        e.printStackTrace();
}

Upvotes: 1

Related Questions