2013-12-13 103 views
0

我目前有一些代码可以接受控制台输入,但我需要让它识别名称和测试分数列表。 代码,我目前有:如何从Java中获取.txt文件的信息?

import java.io.*; 
import utils.io.*; 

class student 
{ 
    String name; 
    double sResult[]; 
    int result; 
    double sum; 

    void getdata() 
    { 
     System.out.println("Enter name:"); 
     Name = (string) system.in.read(); 
     int index=0; 

     for (int counter=1; counter<=5; counter++) 
     { 
     System.out.println("Enter result for subject"+counter+":"); 
     sResult[count] = (double) system.in.read(); 
     } 
    } 

    public static void CalculateAverage() 
    { 
     sum = 0; 
     for (int i=1;i<=5;i++) 
     { 
     sum += sResult[i]; 
     } 
     return (int) Math.round(sum/(values.length-1)); 
    } 

    Public static char calculateGrade() 
    { 
     result=sum; 
     if (result>=0 && result <=59) 
     { 
     return ('F'); 
     } 
     else 
     if (result >=60 && result<=69) 
     { 
     return ('E'); 
     } 
     else 
     if (result>=0 && result<79) 
     { 
     return ('D'); 
     } 
     else 
     if (result >=70 && result<=79) 
     { 
     return ('C'); 
     } 
     else 
     if (result>=80 && result<=89) 
     { 
     return ('B'); 
     } 
     else 
     if (result>=90 && result<=100) 
     { 
     return ('A'); 
     } 
    } 
} 

和一流的检测

public class test 
{ 
    public static void main(String[] args) 
{ 
    Student std; 
    do 
    { 
     std=new Student(); 
     std.getdata(); 
     System.out.println("Student Name:"+std.Name); 
     System.out.println("Average for"+std.Name+" "+"is:"+std.average()); 
     System.out.println("Grade for"+std.Name+" "+"is:"+std.gradecal()); 
     System.out.println("Want to continue (1-Yes,2-No"); 
    } 
    while(System.in.read()==1); 
} 

的文本文档格式名称score1 score2 score3 score4 score5

我只需要帮助搞清楚如何导入值,然后我可能会弄清楚如何使用PrintWriter重写.txt。

在此先感谢您的帮助!

+0

此代码甚至编译! – nachokk

+0

你尝试过'BufferedReader()'吗?你想在这里导入'import utils.io。*;'? – Baby

+0

有多种方式可以读取Java中的文本文件。一种是在FileInputStream上使用BufferedReader。你也可以使用java.util.Scanner,或者如果你想要最新的东西,你可以用新的Files类来选择Java nio2。 – daniel

回答

0

以下是您如何使用Scanner来做到这一点。

Scanner inputFile = new Scanner(new File("inputfile.txt")); 
while(inputFile.hasNext()){ //if there is still something to read 
    String name = inputFile.next(); 
    int score1 = inputFile.nextInt();  
    .... 
    int score5 = inputFile.nextInt(); 
    //Do something here with the scores 
    inputFile.next(); //Read the new line character and prepare for the next iteration 
} 

要回写到文件,您可以检查Davy的答案并使用Buffered Writer。请注意,write()方法的工作方式与System.out.println()类似,但您需要自行打印\n才能进入新行。

+0

我很确定这就是我要找的。一旦我尝试过,我会尽快回复您。 – user2863980

+0

这就是我需要的:D。谢谢! – user2863980

0

首先,你应该不会有变量名称以大写,即:

string Name; 

只有类名称应该以大写字母开头。

另外,我甚至不确定你的代码是否编译。无论你在哪里system.out.println应该是System.out.println。注意系统是一个类,所以第一个字母是大写

要读取数据,可以用BufferedReader来写,你可以用BufferedWriter。 用法:

BufferedReader in = new BufferedReader(new FileReader("FileName.txt")); 
BufferedWriter out = new BufferedWriter(new FileWriter("OutputFileName.txt")); 

in.readLine(); //This reads a single line from the text file 
out.write("Your Output Text"); 
in.close(); // Closes the file input stream 
out.close(); // Closes the file output stream 

既然你正在阅读的格式:

名score1 score2 score3 score4 score5

你可以使用一些字符串函数来获取每个字段。

String inputLine = in.readLine(); 
String [] fields = inputLine.split(" "); // Splits at the space 
System.out.println(fields[0]); //prints out name 
System.out.println(fields[1]); //print out score1 
... 
+0

拆分方法是否适用于扫描仪类型? – user2863980

+0

是的,split方法来自String类。请参阅:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html –

+0

谢谢!这将有助于! – user2863980

0
public static void main(String... args) throws IOException { 
    Scanner s = new Scanner(new BufferedReader(new FileReader("textfile.txt"))); 
    while(s.hasNext()){ 
     Student student = new Student(s.next(), s.nextDouble(), s.nextDouble(), 
             s.nextDouble(), s.nextDouble(), s.nextDouble())); 
     // Do something with student 
     s.next() // consume endline 
    } 
} 
相关问题