Quick Java Help?

Soldato
Joined
8 Oct 2005
Posts
4,185
Location
Midlands, UK
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):

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:
Associate
Joined
4 Jul 2006
Posts
211
i dont really get what your trying to do here. your reading from results2.txt that has 4 strings a line right? and you've made a reference to this file with input but then you have a bit where you get the input for the third test by doing this:

//Input markthree from kybd
System.out.println("Enter 3rd Result: ");
testThree = input.nextInt();

wouldnt testThree in your case hold the average?
 
Soldato
OP
Joined
8 Oct 2005
Posts
4,185
Location
Midlands, UK
i dont really get what your trying to do here. your reading from results2.txt that has 4 strings a line right? and you've made a reference to this file with input but then you have a bit where you get the input for the third test by doing this:

//Input markthree from kybd
System.out.println("Enter 3rd Result: ");
testThree = input.nextInt();

wouldnt testThree in your case hold the average?

I probs explained it crap :)

Results2.txt had a string and 2 ints E.g.

NAME T1 T2
Bill 20 20

I then wanted the user to inout a third int (T3) and to output all scores plus an average of those scores to a txt file called results3.txt - String, int, int, int, int. So the final file may look like this:

NAME T1 T2 T3 AV
Bill 20 20 20 20
Jim 20 20 20 20


etc.
 
Soldato
OP
Joined
8 Oct 2005
Posts
4,185
Location
Midlands, UK
Actually, can't work out why the following prints a blank line at the top of the text file and then the results, gah its so annoying:

Code:
pw.printf("%-15s %5d %3d %3d %3d %n",name,testOne,testTwo,testThree,average);
 
Soldato
Joined
15 Jan 2004
Posts
14,199
Location
Hall
im assuming it's because you're using input.hasnext as an iterator? and the %n formatter is invoking a new line. It's probably due to your code layout for the printwriter.
 
Soldato
OP
Joined
8 Oct 2005
Posts
4,185
Location
Midlands, UK
Ah cheers. It was becuase i had the pw set to append 9, when its not needed (must write a new line by default). The below works:

pw = new PrintWriter(new FileWriter("results3.txt"));


Cheers for help people.
 
Back
Top Bottom