With this project we are learning how to pull and outside file and use it in our Eclipse program. In the outside file (wordpad) there needs to be 50 numbers (1-50, a new number each line:1 return, 2 return, 3 return, etc.). With these numbers we have to ask the user to pull in the file (with input.txt for input, output.txt for output). Then we ask the user for 5 names and average the numbers for each student. For example user enters Joe, then Jack, then jill, then james, and jake which should output:
Joe: 5.5 (average of 1-10)
jack: 15.5 (average of 11-20)
jill: 25.5 (average of 21-30)
james: 35.5 (average of 31-40)
jake: 45.5 (average of 41-50)
What do you guys think? Any advise or help?
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class LineIO {
public static void main(String[] args)throws FileNotFoundException{
Scanner console = new Scanner(System.in);
System.out.print("Enter Input file name: ");
String inputFileName = console.next();
System.out.print("Output file: ");
String outputFileName = console.next();
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
int lineNumber = 1;
int lineNumber2 = 10;
int lineNumber3 = 20;
int lineNumber4 = 30;
int lineNumber5 = 40;
// Read the input and write the output
System.out.println("Enter name 5 names:");
String person1 = console.next();
String person2 = console.next();
String person3 = console.next();
String person4 = console.next();
String person5 = console.next();
while (in.hasNextLine())
{
String line = in.nextLine();
System.out.println(person1 + " average score is " + lineNumber);
System.out.println(person2 + " average score is " + lineNumber2);
System.out.println(person3 + " average score is " + lineNumber3);
System.out.println(person4 + " average score is " + lineNumber4);
System.out.println(person5 + " average score is " + lineNumber5);
out.println("/* " + lineNumber + " */ " + line );
if (lineNumber <= 9)
lineNumber++;
else
break;
if (lineNumber2 <= 19)
lineNumber2++;
else
break;
if (lineNumber3 <= 29)
lineNumber3++;
else
break;
if (lineNumber4 <= 39)
lineNumber4++;
else
break;
if (lineNumber5 <= 49)
lineNumber5++;
break;
}
in.close();
out.close();
}
}
Can someone show me how to average the numbers successfully. I can finish the from there. Thank you.