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.
在过去的3个小时里,我一直在做家庭作业,我没有取得很大的进步。我想知道是否有人可以帮我推动我朝着正确的方向前进。
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
0.30 0.30 0.40
161
3333 70 60 50
3333 70 60 50
4444 50 50 50
4444 50 50 50
5555 80 90 80
5555 80 90 80
0
162
1212 90 85 92
1212 90 85 92
6666 60 80 90
6666 60 80 90
7777 90 90 90
7777 90 90 90
8888 95 87 93
8888 95 87 93
9999 75 77 73
9999 75 77 73
0
263
2222 90 65 75
2222 90 65 75
8989 60 40 60
8989 60 40 60
9090 70 80 30
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.
该文件包含5种不同类型的数字。这三位数字是课程编号。这四位数字是学号。
- The two digit numbers are scores for assignments, mid terms, and finals for each student.
- The decimals across the top are used to find the weighted average for the scores.
- zeros are a break between courses.
两位数字是每个学生的作业,中期和决赛的分数。
顶部的小数用于查找分数的加权平均值。
零是课程之间的休息。
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)
重量计算器(0,1)
- a score(1,100),
- a classNumber (100,400),
一个classNumber(100,400),
- a studentNumber(1000,9999),
- a class separator (0).
类分隔符(0)。
I am thinking something like:
我想的是:
for(in.next(); in < 100 && in > 1; next());
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");
}
}
}
1 个解决方案
#1
You have 2 options here :
这里有2个选项:
Use
Scanner#hasNextInt()
andnextInt()
instead ofnextLine()
to readIntegers
directly instead of numbers in String format.使用Scanner#hasNextInt()和nextInt()而不是nextLine()直接读取Integers而不是String格式的数字。
Keep your reading / scanning code intact. Use
Integer.parseInt()
to convert String to Integers. --> Preferred solution. As it is more efficient.保持您的阅读/扫描代码完好无损。使用Integer.parseInt()将String转换为Integers。 - >首选解决方案。因为它更有效率。
#1
You have 2 options here :
这里有2个选项:
Use
Scanner#hasNextInt()
andnextInt()
instead ofnextLine()
to readIntegers
directly instead of numbers in String format.使用Scanner#hasNextInt()和nextInt()而不是nextLine()直接读取Integers而不是String格式的数字。
Keep your reading / scanning code intact. Use
Integer.parseInt()
to convert String to Integers. --> Preferred solution. As it is more efficient.保持您的阅读/扫描代码完好无损。使用Integer.parseInt()将String转换为Integers。 - >首选解决方案。因为它更有效率。