Reputation: 13
I've been spinning my wheels on a homework assignment for the last 3 hours and am not making very much progress. I was wondering if someone could help nudge me in the right direction.
The assignment is to read data from a file and convert it to something that is more reader friendly. The data file looks something like this:
0.30 0.30 0.40
161
3333 70 60 50
4444 50 50 50
5555 80 90 80
0
162
1212 90 85 92
6666 60 80 90
7777 90 90 90
8888 95 87 93
9999 75 77 73
0
263
2222 90 65 75
8989 60 40 60
9090 70 80 30
0
The file contains 5 different types of numbers.
1. The three digit numbers are course numbers.
2. The four digit numbers are student numbers.
I am supposed to read all of the data in the file and then format and output it so it's easy to read:
Grade Data For Class 161
ID Programs Midterm Final Weighted Average Programs grade
-- -------- ------- ----- ---------------- --------------
3333 70 60 50 59.00 Pass
4444 50 50 50 50.00 Fail
5555 80 90 80 83.00 Pass
Class Average: 64.00
I have the scanner setup and am able to parse out the numbers from the text, my problem is that they are all saved as Strings so I can't do any mathematical checks on them. I'm starting to wonder if this is even the best way to go about this.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class classAverage
{
public static void main(String[] args) throws IOException
{
//in variable equals entire file
Scanner in = new Scanner(new File("courseData.txt"));
int classID;
System.out.println("ID Programs Midterm Final Weighted Average Programs Grade");
System.out.println("-- -------- ------- ----- ---------------- --------------");
while(in.hasNextLine()) //While file has new lines
{
//line equals each line of text
String line = in.nextLine();
Scanner lineParser = new Scanner(line);
System.out.println(line);
for(; lineParser.hasNext();)
{
//number = each number
String number = lineParser.next();
System.out.println(number);
if(number < 1 && number > 0)
{
double programsAverage = number.nextDouble();
double midtermAverage = number.nextDouble();
double finalAverage = number.nextDouble();
System.out.println(programsAverage);
System.out.println(midtermAverage);
System.out.println(finalAverage);
}
}
}
}
}
Update I've included my updated code. My problem now is my conditions in my for statements. These should be checking the values of the incoming Scanner data to see if the data is:
- a weight calculator(0,1)
- a score(1,100),
- a classNumber (100,400),
- a studentNumber(1000,9999),
- a class separator (0).
I am thinking something like:
for(in.next(); in < 100 && in > 1; next());
This isn't quite doing it though.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
/**
* Write a description of class classAverage here.
*
* @author
* @version
*/
public class classAverage
{
public static void main(String[] args) throws IOException
{
//in variable equals entire file
Scanner in = new Scanner(new File("courseData.txt"));
int classNumber;
int studentNumber;
int programs;
int midterm;
int finals;
double programWeight = in.nextDouble();
double midtermWeight = in.nextDouble();
double finalWeight = in.nextDouble();
//System.out.println(programWeight + " " + midtermWeight + " " + finalWeight);
for(int k = 0; k < 3; k++)
{
for(int i = 0; i <= 0; i++)
{
classNumber = in.nextInt();
System.out.println("Grades for class: " + classNumber);
System.out.println(" ID Programs Midterm Final Weighted Average Programs Grade");
System.out.println(" -- -------- ------- ----- ---------------- --------------");
}
int studentCount = 0;
double sumAverage = 0.0;
for(int j = 0; j <= 2; j++)
{
studentNumber = in.nextInt();
programs = in.nextInt();
midterm = in.nextInt();
finals = in.nextInt();
studentCount++;
double weightedAverage = (programWeight * programs) + (midtermWeight * midterm) + (finalWeight * finals);
sumAverage += weightedAverage;
System.out.printf("%d %d %d %d %.2f ", studentNumber,programs,midterm,finals,weightedAverage);
if(programs >= 70)
{
System.out.print("PASS");
} else {
System.out.print("FAIL");
}
System.out.println();
}
double classAverage = sumAverage / studentCount;
System.out.printf("Class average is: %.2f", classAverage);
System.out.println("\n\n");
}
}
}
Upvotes: 0
Views: 127
Reputation: 36304
You have 2 options here :
Use
Scanner#hasNextInt()
andnextInt()
instead ofnextLine()
to readIntegers
directly instead of numbers in String format.Keep your reading / scanning code intact. Use
Integer.parseInt()
to convert String to Integers. --> Preferred solution. As it is more efficient.
Upvotes: 1