//Split string starts on the while loop
我试图在不使用String []拆分方法的情况下重新编码该程序。有没有办法解决这个问题?
喜欢某种不同的方法?还是我坚持使用这种方法?
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
public class Assignment3 {
public static void main(String args[]) throws IOException {
//create format for date
SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss a");
//output date
System.out.println("Welcome to the batch grade calculator");
System.out.println("Start time is: " +sdf.format(new Date()));
System.out.println("-------------------------------------");
Assignment3 calc = new Assignment3();
//calculate grade for students
calc.calculateGrade();
System.out.println("-------------------------------------");
System.out.println("Current end time : " +sdf.format(new Date()));
}
public void calculateGrade() throws IOException {
System.out.print("Input grade file: ");
//read data form file, entered by user
Scanner input = new Scanner(System.in);
Scanner scanner = new Scanner(new File(input.next()));
//declare variables
String id="";
String Lname="";
String Fname="";
int q[]= new int[5];
int a[] = new int[4];
int mt=0;
int fe=0;
int pr=0;
int QUIZZES=5;
int ASSIGNMENTS=4;
int processed=0;
int failed=0;
double sumOfgrades=0;
// create file entered by user
System.out.print("Otput grade file: ");
Scanner output = new Scanner(System.in);
File file = new File (output.next());
// use writer to read information from file
PrintWriter printWriter = new PrintWriter (file);
//print header of file
printWriter.printf("%5s", "Grade");
printWriter.printf("%10s", "ID");
printWriter.printf("%20s", "Quizzess");
printWriter.printf("%20s", "Assignments");
printWriter.printf("%20s", "Midterm");
printWriter.printf("%20s", "Final");
printWriter.printf("%20s", "Professionalism");
printWriter.println();
for(int i=0;i<6;i++)
{
printWriter.print("===================");
}
printWriter.println();
//read header of file
if(scanner.hasNext())
scanner.nextLine();
//use loop to read each line
**while (scanner.hasNextLine())
{
// read line
String line=scanner.nextLine();
//split line
// ***the split method starts on this line.
String[] split=line.split("\t");
int k=0;
//get id, last name, first name
id=split[k++];
Lname=split[k++];
Fname=split[k++];
double sum=0;
//get quizzes
for(int i=0;i<QUIZZES;i++)
{
q[i]= Integer.parseInt(split[k++]);
sum+=q[i];
}
// evaluate average quiz mark
double quizMark=sum/QUIZZES;
sum=0;
// get assignments
for(int i=0;i<ASSIGNMENTS;i++)
{
a[i]=Integer.parseInt(split[k++]);
sum+=a[i];
}
// evaluate average assignment mark
double assignmentsMark =sum/ASSIGNMENTS;
mt=Integer.parseInt(split[k++]);
fe=Integer.parseInt(split[k++]);
pr=Integer.parseInt(split[k++]);****
double TECHNICAL_MARK=calculateTechnicalMark(quizMark,assignmentsMark,mt,fe);
double grade= (TECHNICAL_MARK * 4 +pr)/5.0;
// print information about each student
String s="";
if(grade <50)
s+="*";
printWriter.printf("%5s", s+ "["+ getLetterGrade(grade)+"]");
printWriter.printf("%10s", id);
printWriter.printf("%20s", getLetterGrade(quizMark));
printWriter.printf("%20s", getLetterGrade(assignmentsMark));
printWriter.printf("%20s", getLetterGrade(mt));
printWriter.printf("%20s", getLetterGrade(fe));
printWriter.printf("%20s", getLetterGrade(pr));
printWriter.print( "\r\n");
sumOfgrades+=grade;
processed++;
if(!countProfessionalism(TECHNICAL_MARK))
{
failed++;
}
}
printWriter.print("\n\nThe * at the beginning of the line indicates that the student did not pass the course’s technical components.");
// print main information
System.out.println ("# students processed: "+processed);
System.out.println ("# students who failed: "+failed);
System.out.print ("# class average(%): ");
System.out.printf("%.2f",sumOfgrades/processed);
System.out.println("%");
System.out.println("# class average(letter): "+getLetterGrade(sumOfgrades/processed));
// close the writer
printWriter.close ();
// close the scanner
scanner.close();
}
/**
* This function will return the Letter Grade as a String for the mark passed
* to the method as the parameter double mark
*/
public String getLetterGrade(double mark) {
if(mark >=90)
return "A+";
else
if(mark >=85)
return "A";
else
if(mark >=80)
return "A-";
else
if(mark>=77)
return "B+";
else
if(mark>=73)
return "B";
else
if(mark>=70)
return "B-";
else
if(mark>=67)
return"C+";
else
if(mark>=63)
return "C";
else
if(mark>=60)
return "C-";
else
if(mark>=55)
return "D+";
else if(mark>=50)
return "D";
else
return "F";
}
/**
* This function will return the technical mark for a student given
* the four components that comprise the technical mark
*/
public double calculateTechnicalMark(double quizMark, double assignmentsMark, double midtermMark, double finalMark) {
// write function / method code her
return (quizMark +assignmentsMark +midtermMark +finalMark )/4.0;
}
/**
* This function will return true if the student can have their
* professional mark count towards their final mark, false otherwise
*/
public boolean countProfessionalism(double technicalMark) {
if(technicalMark >50)
return true;
else return false;
}
}
答案 0 :(得分:0)
在您的情况下,您只需使用 扫描程序 或 OpenCSV
示例文件:sample.txt
row1col1 row1col2 row1col3 row2col1 row2col2 row2col3 row3col1 row3col2 row3col3
final String filePath = System.getProperty("user.dir") + "/sample.txt";
try (final CSVReader reader = new CSVReader(new FileReader(filePath), '\t')) {
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[0] + " - " + nextLine[1] + " - " + nextLine[2]);
}
}
<强>输出:强>
row1col1 - row1col2 - row1col3 row2col1 - row2col2 - row2col3 row3col1 - row3col2 - row3col3
OpenVV在Maven中可用,此处:https://mvnrepository.com/artifact/net.sf.opencsv/opencsv/2.3
final String filePath = System.getProperty("user.dir") + "/sample.txt";
try (final Scanner sc = new Scanner(new FileReader(filePath))) {
while (sc.hasNextLine()) {
System.out.println(sc.next());
}
}
<强>输出:强>
row1col1 row1col2 row1col3 row2col1 row2col2 row2col3 row3col1 row3col2 row3col3
答案 1 :(得分:0)
假设您正在从性能角度探索替代方案,下面是对Java SE中4种方法的比较:
测试程序和比较结果为here。
虽然StringTokenizer在我比较的4种方法中表现最佳,但javadoc提出了一个警告:
StringTokenizer是一个遗留类,出于兼容性原因而保留,尽管在新代码中不鼓励使用它。建议任何寻求此功能的人都使用String的split方法或java.util.regex包。
String.split()方法最简单易用,但Pattern.split()的性能更高,但需要额外的步骤。