Hi, 
Am going through a few java tutorials and am coming into problems. I have two text files:
results2.txt in the format "Name Mark1 Mark 2" and results3.txt - here i need to read the stuff from results1.txt and input a third test result and calculate the average. So results3.txt will look like:
"Name Test1 Test2 Test3 Average"
However, I keep getting an input mismatch exception where i prompt the user to enter the test 3 result. Here my code (i'll split it into methods later on):
	
	
	
		
	
		
			
		
		
	
				
			Am going through a few java tutorials and am coming into problems. I have two text files:
results2.txt in the format "Name Mark1 Mark 2" and results3.txt - here i need to read the stuff from results1.txt and input a third test result and calculate the average. So results3.txt will look like:
"Name Test1 Test2 Test3 Average"
However, I keep getting an input mismatch exception where i prompt the user to enter the test 3 result. Here my code (i'll split it into methods later on):
		Code:
	
	public class TestResults2 {
    public static void main(String[] args) {
        Scanner input = null; //Output from results2.txt 
        PrintWriter pw = null; //pw for results3.txt
        String name; //Holds name as string
        int testOne=0, testTwo=0, testThree=0; //Holds test scores as int
        
        try {
            //Open both files
            input = new Scanner(new File("results2.txt"));
            pw = new PrintWriter(new FileWriter("results3.txt",true));
            
            //Loop through results2.txt, read in marks
            while (input.hasNext()){
                name = input.next();
                testOne = input.nextInt();
                testTwo = input.nextInt();
                //Input markthree from kybd
                System.out.println("Enter 3rd Result: ");
                testThree = input.nextInt();
                
                //Write all to results3.txt
                int average=((testOne + testTwo + testThree)/3); //Average of 3 scores
                pw.printf("%-15s %5d %3d %5d %5d %n",name,testOne,testTwo,testThree,average);
                
            }//End While  
        }//End Try
        catch (IOException ioe) {
            System.out.println("Error handling file!");
            System.out.println(ioe.toString());
        }//End catch
    }//End Main
}//End Class
	
			
				Last edited: