繁体   English   中英

如何在Java中从.txt文件提取信息?

[英]How do I pull information form a .txt file in Java?

我目前有一些可以接受控制台输入的代码,但是我需要使其识别名称和测试分数的列表。 我目前拥有的代码:

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。

在此先感谢您的帮助!

首先,您不应使用以大写字母开头的变量名称,即:

string Name;

仅类别名称应以大写字母开头。

另外,我什至不确定您的代码是否可以编译。 到处都有system.out.println应该是System.out.println 请注意,System是一个类,因此第一个字母是大写

要读取数据,可以使用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

由于您正在阅读以下格式:

名称分数1分数2分数3分数4分数5

您可以使用一些String函数来获取每个字段。

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
...

这是使用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才能转到新行。

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM