输出文件没有正确读取输入文件,格式混乱。

huangapple go评论76阅读模式
英文:

Output file not reading input file correctly, format out of order,

问题

我正在尝试创建一个程序,该程序接受12名学生的考试统计信息。在这个程序中,程序会读取输入文件,然后将其打印到输出文件,但存在一系列问题。程序能够读取输入文件,但无法打印第一个学生的ID。无论我怎么做,"C"和"I"都不会打印在"More Right/Wrong"列下面。此外,"<10"也无法打印在"Omit"列下面。以下是我的代码。

import java.util.Scanner;
import java.io.*;

public class Topic3_HW3 {

	public static void main(String[] args) throws IOException {
		PrintWriter outputFile;
		outputFile = new PrintWriter("HW3_output.txt");
		File myFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\Topic3_input.txt");
		Scanner inputFile = new Scanner(myFile);
		outputFile.printf("%40s", "\tTable of Grades");
		outputFile.printf("\n%1s %7s %9s %12s %9s %10s %7s %12s %8s", "\nStudent", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");
		outputFile.println();

		double pct;
		int grade, id, corr, incorr, total, omit;
		int student = 0;

		System.out.print("Enter student's ID (negative 1 to stop): ");
		id = inputFile.nextInt();
		while (id != -1) {
			System.out.print("\nEnter the number of questions the student answered correctly: ");
			corr = inputFile.nextInt();
			System.out.print("\nEnter the number of questions the student answered incorrectly: ");
			incorr = inputFile.nextInt();
			student++;
			total = corr + incorr;
			if (50 < total) {
				System.out.println("ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.");
			}
			System.out.print("\nEnter the next student's ID (negative 1 to stop): ");
			id = inputFile.nextInt();
			omit = 50 - (corr + incorr);
			grade = corr * 2;
			pct = (double) corr / total;

			if (omit < 10)
				outputFile.printf("%-15s", "<10");
			if (10 < omit)
				outputFile.printf("%-15s", "10<");
			if (omit == 10)
				outputFile.printf("%-15s", "10");
			if (corr < incorr)
				outputFile.printf("%-10s", "I");
			if (incorr < corr)
				outputFile.printf("%-10s", "C");
			if (corr == incorr)
				outputFile.printf("%-10s", "=");

			outputFile.printf("\n%1s%14s%7s%12s%12s%9s%11s%10.3f", student, id, corr, incorr, total, omit, grade, pct);
		}

		System.out.println("\nThe program is finished.");
		outputFile.flush();
		outputFile.close();
		inputFile.close();
	}
}

我尝试更改了"C"和"I"的格式化,但即使我将它们改为"%-100s",它们仍然不会在百分比列的值之前打印。我期望表格能够打印出相应列的完整值,包括输入文件的第一行。我还期望"C"和"I"能够打印在"More Right/Wrong"列下,而"10<"则打印在"Omit"列旁边的数字下面。但实际上,列值打印得有些混乱。

希望这个解释对你有所帮助。

英文:

i am trying to create a program that takes exam statistics for 12 students. In this program the input file is read by the program and then printed to the output file, but there are a slew of issues. the program is reading the inputfile but not printing the first ID. No matter what I do, the C's and I's will not print under the More Right/Wrong column. Additionally, The <10 will not print uder the Omit column either.here is my code.

import java.util.Scanner;
import java.io.*;
public class Topic3_HW3 {
public static void main(String[] args) throws IOException {
PrintWriter outputFile;
outputFile = new PrintWriter(&quot;HW3_output.txt&quot;);
// outputFile = new PrintWriter(System.out);
// TODO Auto-generated method stub
File myFile = new File(&quot;C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\Topic3_input.txt&quot;);
Scanner inputFile = new Scanner(myFile);
// print table header and columns
outputFile.printf(&quot;%40s&quot;, &quot;\tTable of Grades&quot;);
outputFile.printf(&quot;\n%1s %7s %9s %12s %9s %10s %7s %12s %8s&quot;,&quot;\nStudent&quot;,&quot;ID&quot;,&quot;Correct&quot;,&quot;Incorrect&quot;,&quot;Total&quot;,&quot;More Right/Wrong&quot;,&quot;Omit&quot;,&quot;Grade&quot;,&quot;Percent&quot;);
outputFile.println();
// declare datatypes for different variables
double pct;
int grade, id, corr, incorr, total, omit;
int student = 0;
//prompt user to input initial student&#39;s ID
System.out.print(&quot;Enter student&#39;s ID(negative 1 to stop): &quot;);
id = inputFile.nextInt();
while (id != -1) {
//prompt user to enter said student&#39;s wrong/right questions
System.out.print(&quot;\nEnter the amount of questions the student answered correctly: &quot;);
corr = inputFile.nextInt();
System.out.print(&quot;\nEnter the amount of questions the student answered incorrectly: &quot;);
incorr = inputFile.nextInt();
student++;
total = corr + incorr;
// print error if total is more than 50 questions
if (50 &lt; total) {
System.out.println(&quot;ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.&quot;);}
System.out.print(&quot;\nEnter the desired student&#39;s ID(negative 1 to stop): &quot;);
id = inputFile.nextInt();
omit = 50 - (corr + incorr);
grade = corr * 2;
// calculate percentage correct. type cast correct and total to make sure it
// doesnt truncate
pct = (double) corr / total;
//print if the omitted amount of questions is less, more, or equal to 10
if (omit &lt; 10)
outputFile.printf(&quot;%-15s&quot;, &quot;&lt;10&quot;);
if (10 &lt; omit)
outputFile.printf(&quot;%-15s&quot;, &quot;10&lt;&quot;);
if (omit == 10)
outputFile.printf(&quot;%-15s&quot;, &quot;10&quot;);
if (corr&lt;incorr)
outputFile.printf(&quot;%-10s&quot;,&quot;I&quot;);
if(incorr&lt;corr)
outputFile.printf(&quot;%-10s&quot;,&quot;C&quot;);
if (corr==incorr)
outputFile.printf(&quot;%-10s&quot;,&quot;=&quot;);
//print values for corresponding columns
outputFile.printf(&quot;\n%1s%14s%7s%12s%12s%9s%11s%10.3f&quot;, student, id, corr, incorr, total, omit, grade, pct);
}
//notify viewer that program has run
System.out.println(&quot;\nThe program is finished.&quot;);
// flush buffer and close i/o files
outputFile.flush();
outputFile.close();
inputFile.close();
}
}

输出文件没有正确读取输入文件,格式混乱。]

I tried changing the %-10 formatting for the Cs and Is but even if I do %-100 it still will not print before the percent columns values. I was expecting the table to print the full values for their corresponding columns including line 1 of the input file. I was also expecting for the Cs and Is to print under More Right/Wrong column and the 10<s to print under the omit column next to the number.Instead, the column values are printing all over the place

答案1

得分: 1

I've translated the code parts as you requested. Here they are:

> No matter what I do, the C's and I's will not print under the More Right/Wrong column.

无论我做什么C和I都不会打印在More Right/Wrong列下


When dealing with formatters like this, it's REALLY important that the column structure is the same across iterations, for example...

当处理这样的格式化程序时非常重要的一点是在各个迭代中列的结构保持一致例如...

outputFile.printf("\n%1s %7s %9s %12s %9s %10s %7s %12s %8s","\nStudent","ID","Correct","Incorrect","Total","More Right/Wrong","Omit","Grade","Percent");
//...
outputFile.printf("\n%1s%14s%7s%12s%12s%9s%11s%10.3f", student, id, corr, incorr, total, omit, grade, pct);

The first title column is 1 character wide and the second is 7, but the second output column is 14??

第一个标题列宽度为1个字符第二个为7个字符但第二个输出列为14个字符吗

Now, take into consideration that `Student` is 7 characters wide anyway and the first columns width doesn't make sense.

现在请考虑一下,“Student本身宽度就为7个字符因此第一列的宽度没有意义

Instead it "should" look something more like...

而更合理的方式应该如下...

System.out.printf("\n%-7s %-4s %-7s %-9s %-5s %-16s %-4s %-6s %-8s", "Student", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");
//...
System.out.printf("%7d %4d %7d %9s %5s %16s %4d %6.3f %3.2f", student, id, corr, incorr, total, omit, grade, pct);

> Additionally, The <10 will not print under the Omit column either.

此外"<10" 也不会在Omit列下打印这是因为您在打印 "omit" 之前打印其他信息从可读性的角度来看最好将 "omit" 信息作为整个数据的一部分一起打印

So, I modified your code slightly to fix the columns and add the "omit" information into the final data line...

因此我稍微修改了您的代码修复了列并将 "omit" 信息添加到最终的数据行中...

File myFile = new File("marks.txt");
try (Scanner inputFile = new Scanner(myFile)) {
    // print table header and columns
    System.out.printf("%40s", "\tTable of Grades");
    System.out.printf("\n%-7s %-4s %-7s %-9s %-5s %-16s %-4s %-6s %-8s", "Student", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");
    System.out.println();

    int student = 0;

    int id = inputFile.nextInt();
    while (id != -1) {
        int corr = inputFile.nextInt();
        int incorr = inputFile.nextInt();
        student++;
        int total = corr + incorr;
        int omit = 50 - (corr + incorr);
        double grade = corr * 2;
        double pct = (double) corr / total;
        String value = "";
        if (omit < 10) {
            value = "<10";
        }
        if (10 < omit) {
            value = "10<";
        }
        if (omit == 10) {
            value = "10";
        }
        if (corr < incorr) {
            value = "I";
        }
        if (incorr < corr) {
            value = "C";
        }
        if (corr == incorr) {
            value = "=";
        }
        System.out.printf("%7d %4d %7d %9s %5s %16s %4d %5.3f %3.2f", student, id, corr, incorr, total, value, omit, grade, pct);
        id = inputFile.nextInt();
    }
    System.out.println("\nThe program is finished.");
}

This now prints the desired table format.

英文:

> No matter what I do, the C's and I's will not print under the More Right/Wrong column.

When dealing with formatters like this, it's REALLY important that the column structure is the same across iterations, for example...

outputFile.printf(&quot;\n%1s %7s %9s %12s %9s %10s %7s %12s %8s&quot;,&quot;\nStudent&quot;,&quot;ID&quot;,&quot;Correct&quot;,&quot;Incorrect&quot;,&quot;Total&quot;,&quot;More Right/Wrong&quot;,&quot;Omit&quot;,&quot;Grade&quot;,&quot;Percent&quot;);
//...
outputFile.printf(&quot;\n%1s%14s%7s%12s%12s%9s%11s%10.3f&quot;, student, id, corr, incorr, total, omit, grade, pct);

The first title column is 1 character wide and the second is 7, but the second output column is 14??

Now, take into consideration that Student is 7 characters wide anyway and the first columns width doesn't make sense.

Instead it "should" look something more like...

System.out.printf(&quot;\n%-7s %-4s %-7s %-9s %-5s %-16s %-4s %-6s %-8s&quot;, &quot;Student&quot;, &quot;ID&quot;, &quot;Correct&quot;, &quot;Incorrect&quot;, &quot;Total&quot;, &quot;More Right/Wrong&quot;, &quot;Omit&quot;, &quot;Grade&quot;, &quot;Percent&quot;);
//...
System.out.printf(&quot;\n%7d %4d %7d %9s %5s %16s %4d %6.3f %3.2f&quot;, student, id, corr, incorr, total, value, omit, grade, pct);

> Additionally, The <10 will not print uder the Omit column either.here is my code.

Yes, this is because you're printing the "omit" BEFORE you print any of the other information. While there is away you "could" do it like this, from a readability point of view, you should be including the "omit" information as part of the whole data when you print it.

So, I modified your code slightly to fix the columns and add the "omit" information in to the final data line...

File myFile = new File(&quot;marks.txt&quot;);
try (Scanner inputFile = new Scanner(myFile)) {
// print table header and columns
System.out.printf(&quot;%40s&quot;, &quot;\tTable of Grades&quot;);
System.out.printf(&quot;\n%7s %4s %7s %10s %9s %16s %4s %5s %8s&quot;, &quot;Student&quot;, &quot;ID&quot;, &quot;Correct&quot;, &quot;Incorrect&quot;, &quot;Total&quot;, &quot;More Right/Wrong&quot;, &quot;Omit&quot;, &quot;Grade&quot;, &quot;Percent&quot;);
System.out.println();
int student = 0;
int id = inputFile.nextInt();
while (id != -1) {
int corr = inputFile.nextInt();
int incorr = inputFile.nextInt();
student++;
int total = corr + incorr;
// Not sure if this has anything to do with your current
// assignment, but it&#39;s just adding noise
//if (50 &lt; total) {
//    System.out.println(&quot;ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.&quot;);
//}
int omit = 50 - (corr + incorr);
double grade = corr * 2;
// calculate percentage correct. type cast correct and total to make sure it
// doesnt truncate
double pct = (double) corr / total;
//print if the omitted amount of questions is less, more, or equal to 10
String value = &quot;&quot;;
if (omit &lt; 10) {
value = &quot;&lt;10&quot;;
}
if (10 &lt; omit) {
value = &quot;10&lt;&quot;;
}
if (omit == 10) {
value = &quot;10&quot;;
}
if (corr &lt; incorr) {
value = &quot;I&quot;;
}
if (incorr &lt; corr) {
value = &quot;C&quot;;
}
if (corr == incorr) {
value = &quot;=&quot;;
}
//print values for corresponding columns
//System.out.printf(&quot;\n%7s %4s %7s %10s %9s %16s %4s %5s %8s&quot;, &quot;Student&quot;, &quot;ID&quot;, &quot;Correct&quot;, &quot;Incorrect&quot;, &quot;Total&quot;, &quot;More Right/Wrong&quot;, &quot;Omit&quot;, &quot;Grade&quot;, &quot;Percent&quot;);
System.out.printf(&quot;\n%7d %4d %7d %10s %9s %16s %4d %5.3f %3.2f&quot;, student, id, corr, incorr, total, value, omit, grade, pct);
id = inputFile.nextInt();
}
//notify viewer that program has run
System.out.println(&quot;\nThe program is finished.&quot;);
// flush buffer and close i/o files
}

This now prints

                        	Table of Grades
Student ID   Correct Incorrect Total More Right/Wrong Omit Grade  Percent 
1 1234      10        11    21                I   29 20.000 0.48
2 1002      47         2    49                C    1 94.000 0.96
3 9000      31        19    50                C    0 62.000 0.62
4 8110      12        10    22                C   28 24.000 0.55
5 9062      40         0    40                C   10 80.000 1.00
6 9001      25        25    50                =    0 50.000 0.50
7 2764       1        49    50                I    0  2.000 0.02
8 1010      10        10    20                =   30 20.000 0.50
9 2324      19        30    49                I    1 38.000 0.39
10 1623      20        28    48                I    2 40.000 0.42
11 1423      20        30    50                I    0 40.000 0.40
12 6969       0        50    50                I    0  0.000 0.00
13 4200      41         6    47                C    3 82.000 0.87
The program is finished.

Now, if you want the columns to be right aligned, then you can use - before the column width, for example %-7d

> I still don't understand why line 1 is getting deleted from the table

Look at how you code is running...

id = inputFile.nextInt();
while (id != -1) {
//...
id = inputFile.nextInt();
//...
outputFile.printf(&quot;\n%1s%14s%7s%12s%12s%9s%11s%10.3f&quot;, student, id, corr, incorr, total, omit, grade, pct);
  • You get the first id
  • You check for the end of file
  • You load the various values
  • You read the NEXT id
  • You print the values

... which id is getting printed?

There is a concept called "desk checking", where you take a pencil and paper and you walk through the program, updating the state as you go, it's a really good way to get you to think about how you code is actually running.

> and everything else is printing a line down

Look at...

outputFile.printf(&quot;\n%1s %7s %9s %12s %9s %10s %7s %12s %8s&quot;,&quot;\nStudent&quot;,&quot;ID&quot;,&quot;Correct&quot;,&quot;Incorrect&quot;,&quot;Total&quot;,&quot;More Right/Wrong&quot;,&quot;Omit&quot;,&quot;Grade&quot;,&quot;Percent&quot;);

You have two line breaks, one in the "format" and one in the values (&quot;\nStudent&quot;)

Personally, I would only put one at the end of each output, for example...

System.out.printf(&quot;%40s&quot;, &quot;\tTable of Grades\n&quot;);
System.out.printf(&quot;%-7s %-4s %-7s %-9s %-5s %-16s %-4s %-6s %-8s\n&quot;, &quot;Student&quot;, &quot;ID&quot;, &quot;Correct&quot;, &quot;Incorrect&quot;, &quot;Total&quot;, &quot;More Right/Wrong&quot;, &quot;Omit&quot;, &quot;Grade&quot;, &quot;Percent&quot;);

> Additionally we have to print the prompt as repeating instead of reading from the input file only

I'm sure you can figure that out, but it's otherwise just adding noise to the question

> there's a couple of things that I cant use yet like String value

🙄 That's one of the dangers of posting on SO, we'll answer the question usually in the most optimal way we know how.

Having said that, there is away to make it work, but sequence and order are important.

Start by printing the preceding information first...

System.out.printf(&quot;%7d %4d %7d %9s %5s&quot;, student, id, corr, incorr, total);

Then print the +/- information...

if (omit &lt; 10) {
System.out.printf(&quot;%16s&quot;, &quot;&lt;10&quot;);
}
if (10 &lt; omit) {
System.out.printf(&quot;%16s&quot;, &quot;10&lt;&quot;);
}
if (omit == 10) {
System.out.printf(&quot;%16s&quot;, &quot;10&quot;);
}
if (corr &lt; incorr) {
value = &quot;I&quot;;
System.out.printf(&quot;%16s&quot;, &quot;I&quot;);
}
if (incorr &lt; corr) {
value = &quot;C&quot;;
System.out.printf(&quot;%16s&quot;, &quot;C&quot;);
}
if (corr == incorr) {
System.out.printf(&quot;%16s&quot;, &quot;=&quot;);
}

Oh and this should be an if-elseif-else statement, but I'm guessing you're not allowed to use those...

And then print the rest...

System.out.printf(&quot;%4d %6.3f %3.2f\n&quot;, omit, grade, pct);

huangapple
  • 本文由 发表于 2023年2月27日 11:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定