Please help me to write content from one text file to another. I have written a code already but I am not getting the correct output as expected.
import java.io.*;
import java.util.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException{
// TODO code application logic here
double m1, m2, m3, m4, m5, total, t, avg;
int i;
String fname;
t=0;
fname = "";
Scanner infile = new Scanner(new FileReader("C:\\111\\students.txt"));
PrintWriter outFile = new PrintWriter("C:\\111\\prog.txt");
// System.out.println(fname);
// System.out.println(fname);
System.out.println("STUDENTS MARK LIST");
outFile.println("STUDENTS MARK LIST");
System.out.println("**********************************************");
outFile.println("**********************************************");
for (i=0; i <5 ; i++)
{
fname = infile.next();
outFile.println("Student Name: " + fname);
m1 = infile.nextDouble();
m2 = infile.nextDouble();
m3 = infile.nextDouble();
m4 = infile.nextDouble();
m5 = infile.nextDouble();
total = m1+m2+m3+m4+m5;
avg = total /5;
t = t+avg;
System.out.println ("STUDENT NAME: " + fname);
outFile.println ("STUDENT NAME: " + fname);
System.out.printf ("Mark 1: %5.2f %n", m1);
System.out.printf ("Mark 1: %5.2f %n", m1);
System.out.printf ("Mark 2: %5.2f %n", m2);
outFile.printf ("Mark 2: %5.2f %n", m2);
System.out.printf ("Mark 3: %5.2f %n", m3);
outFile.printf ("Mark 3: %5.2f %n", m3);
System.out.printf ("Mark 4: %5.2f %n", m4);
outFile.printf ("Mark 4: %5.2f %n", m4);
System.out.printf ("Mark 5: %5.2f %n", m5);
outFile.printf ("Mark 5: %5.2f %n", m5);
System.out.printf ("Total: %5.2f %n", total);
outFile.printf ("Total: %5.2f %n", total);
System.out.printf ("Average Score: %5.2f %n", avg);
outFile.printf ("Average Score: %5.2f %n", avg);
System.out.println("**********************************************");
outFile.println("**********************************************");
}
t = t/5;
System.out.printf ("Average Mark of Five Students: %5.2f %n", t);
开发者_运维问答 outFile.printf ("Average Mark of Five Students: %5.2f %n", t);
System.out.println("**********************************************");
outFile.println("**********************************************");
infile.close(); //Step 10
outFile.close();
}
}
Error: at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at studenttestscore.Main.main(Main.java:42)
Probably you have one student per line: put a inFile.nextLine()
after m5 = infile.nextDouble();
精彩评论