Hello JavaWorld,
I'm having trouble with this program. I don't understand why I am getting an exception thrown to me. I am successfully reading the file, and displaying it's contents on my console. Then I attempt to store the contents into a String file, and then I plan on storing the contents into an ArrayList.
ArrayList<String>Question=new ArrayList(); public void ReadFile() throws IOException{ Scanner sr = new Scanner(System.in); System.out.println("Please enter file name"); String fileName = sr.nextLine(); File infile = new File("C:\\test\\"+fileName+".txt"); if(infile.exists()){ System.out.println("The quiz file has been found"); } else{ System.out.println("Sorry, the file has not been found"); } Scanner file = new Scanner(new File("C:\\test\\"+fileName+".txt")); while(file.hasNext()){ System.out.println(file.next()); String str = file.next(); }
I honestly don't see where I am going wrong here. It's pretty straightforward. I limited the text file to just"hi" and still it wont work. Any input would be greatly appreciated.
Thanks in advance.