Results 1 to 2 of 2
  1. #1
    sara2017 is offline Member
    Join Date
    Apr 2017
    Posts
    8
    Rep Power
    0

    Default How to read from text file into an array using for loop

    trying to read from text file some questions and answers and score.. but its showing
    "Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at test.test.ProcessQuestion(test.java:53)
    at test.test.main(test.java:23)


    ---------------------------------------------


    Java Code:
    package test;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    public class test {
    
    	public static void main(String[] args) throws IOException {
    		
    		
    
    		//Declare and initialize variables
    		
    			
    		int Orgscore[]=new int[10];
    		String q1[] = new String[10];
    		String ansA[] = new String[10];
    		String ansB[] = new String[10];
    		String ansC[] = new String[10];
    		String ansD[] = new String[10];
    		String correctAns[] = new String[10];
    		
    	
    		ProcessQuestion(q1,ansA,ansB,ansC,ansD,correctAns,Orgscore);
    				
    		
    		
    		
    	
    		
    	}
    	
    	public static int ProcessQuestion(String questionArray[] ,String answerA[],String answerB[],String answerC[],String answerD[], String correctAnswer[], int pointValue[])throws IOException
    	{
    		
    		 String space[]= new String[10];
    		 String UserChoice[] = new String[10];
    		 int score =0;
    		String blank;
    		File Infile = new File("test.txt");
    		//declare a Scanner to read from the file
    	
    		Scanner fileInput = new Scanner(Infile);
    		//FOR LOOP
    		for(int i = 1; i< space.length;i++)
    		{
    					    		
    			questionArray[i] =     fileInput.nextLine();
    		    answerA[i] =   fileInput.nextLine();
    			answerB[i] =   fileInput.nextLine();
    			answerC[i] =   fileInput.nextLine();
    			answerD[i] =   fileInput.nextLine();
    			correctAnswer[i] = fileInput.nextLine();
    			pointValue[i]=   fileInput.nextInt();
    			blank = fileInput.nextLine();
    		    UserChoice[i] = JOptionPane.showInputDialog(questionArray[i]+"\n"+answerA[i]+"\n"+answerB[i]+"\n"+answerC[i]+"\n"+answerD[i]+"\n"+"Yout current score: "+pointValue[i]);
    			
    		score+=pointValue[i];
    		}
    		
    		fileInput.close();
    		return score;
    		
    	}
    	
    	
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to read from text file into an array using for loop

    Look at the Scanner class methods you are using and see what they say about NoSuchElementException.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 13
    Last Post: 05-30-2014, 11:12 PM
  2. Read text file into array and display it
    By Waflix in forum New To Java
    Replies: 1
    Last Post: 07-10-2012, 09:58 PM
  3. read from text file into array of objects
    By janey4115 in forum New To Java
    Replies: 1
    Last Post: 11-20-2011, 12:40 PM
  4. Replies: 2
    Last Post: 04-01-2011, 03:04 PM
  5. Replies: 7
    Last Post: 11-01-2010, 02:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •