2012-06-10 22 views
0

我是第一个使用Java编程任务的初学者。 对于我们的编程任务,我们会给出一个像这样的学生的.txt文件:如何在Java中存储未知大小的文件输入中的多个变量?

我的问题是:我有一个特定的类,用于将文件中的数据转换为用于打印中不同类的变量它到了屏幕。但是,我不知道从课程编号的输入文件中获取变量的好方法,因为该编号不是预先确定的。我能想到的迭代未知量的唯一方法是使用循环,但每次都会覆盖我的变量。另外,老师要求我们不要使用任何JCL课程(我真的不知道这是什么意思)。

对不起,如果我做了一个糟糕的工作来解释这一点,但我想不到更好方式来概念化它。让我知道我是否可以澄清。

编辑:

public static void analyzeData() 
{ 
    Scanner inputStream = null; 

    try 
    { 
     inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt")); 
    } 

    catch (FileNotFoundException e) 
    { 
     System.out.println("File Programming Assignment 1 Data.txt could not be found or opened."); 
     System.exit(0);   
    } 

    int numberOfStudents = inputStream.nextInt(); 
    int tuitionPerHour = inputStream.nextInt(); 
    String firstName = inputStream.next(); 
    String lastname = inputStream.next(); 
    String isTuitionPaid = inputStream.next(); 
    int numberOfCourses = inputStream.nextInt(); 
    String courseName = inputStream.next(); 
    String courseNumber = inputStream.next(); 
    int creditHours = inputStream.nextInt(); 
    String grade = inputStream.next(); 

为了表明我现在使用的方法,我只是使用扫描仪从文件和扫描仪的InputStream读取,我使用nextInt()或next()获得来自文件的变量。当我不确切知道每个学生将有多少班时,这显然不起作用。

+1

无码无饼干! –

+0

[你能提供一个代码样本给我们看看你做了什么吗?](http://meta.stackexchange.com/a/128572/175248)我们有很多方法可以解析这个文本文件,但我们不确定你是如何接近它的。 – Makoto

+0

你的教授可能指JCL-- Java类库。这听起来像他要求你解决这个问题,而不依赖于标准的Java库 – pb2q

回答

1
  1. 字符串的firstName创建Class called Student

  2. 在类使用instance variable;

    String lastname;

    布尔isTuitionPaid; //布尔原因isPaid将是真或假

    字符串[]课程;

    INT creditHours;

    弦乐等级;

  3. 创建此类采用以下参数在它的参数的一个构造

    学生(字符串FNAME,字符串LNAME,布尔istPaid,字符串[]当然,整数cHours,字符串GR)

  4. When you read the data of a student from a file, store it in the appropriate data type,如构造函数中所述,然后创建类型为Student的对象

  5. 创建Student object with the data, store it an appropriate Collection后。 ArrayList中,地图等

0

解析文件,创建一个POJO(可以称之为模型),并将其存储在适当的集合(可能列表的实现会做到这一点)。顺便说一句,没有人会解决功课,我相信这是一个政策。

+0

解决?号提示?我没有看到这个问题。 – Makoto

+1

@ bela-vizer要了解更多关于作业问题的信息,请参阅[作业标签wiki](http://stackoverflow.com/tags/homework/info)。另外[这篇文章](http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions)。 – pb2q

+0

没问题,编程任务比这个方面更复杂,而且我也不需要一个解决方案,因为在这一点上,我真的无法想到开始这样做的正确方法。但是,我不知道如何创建一个列表,所以你的回答对我来说有点困惑。我使用扫描仪解析文件并使用Scanner inputStream.nextInt()或inputStream.next()逐字逐句扫描,但如果我不确切知道每个学生将有多少课程,则无法正常工作。 – AlphaOmegaStrife

-1

也许这将有助于:

public static void analyzeData() 
{ 
    try 
    { 
     Scanner inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt")); 
     String str = inputStream.next(); 
     String[] s = str.split(" "); 
     int numberOfStudents = Integer.parseInt(s[0]); 
     int tuitionPerHour = Integer.parseInt(s[1]); 
     System.out.println("Number of students: " + numberOfStudents); 
     System.out.println("Tuition per hour: " + tuitionPerHour + "\n\n"); 
     for(int i = 0; i<numberOfStudents; i++) 
     { 
      String str1 = inputStream.next(); 
      String[] s1 = str1.split(" "); 
      String firstName = s1[0]; 
      String lastName = s1[1]; 
      int rollNo = Integer.parseInt(s1[2]); 
      String isTuitionPaid = s1[3]; 
      int numberOfCourses = Integer.parseInt(s1[4]); 
      System.out.println("Details of student number " + (i+1)); 
      System.out.println("Name: " + firstName + " " + lastName); 
      System.out.println("Roll No: " + rollNo); 
      System.out.println("Is Tuition paid: " + (isTuitionPaid == "Y" ? "Yes" : "No")); 
      System.out.println("Number of Courses taken: " + numberOfcourses + "\n"); 
      for(int j = 0; j<numberOfCourses; j++) 
      { 
       System.out.println("Details of course no " + (j+1)); 
       String str2 = inputStream.next(); 
       String[] s2 = str2.split(" "); 
       String courseName = s2[0]; 
       String courseNumber = s2[1]; 
       int creditHours = Integer.parseInt(s2[2]); 
       String grade = s2[3]; 
       System.out.println("Course Name: " + courseName); 
       System.out.println("Course Number: " + courseNumber); 
       System.out.println("Credit Hours: " + creditHours); 
       System.out.println("Grade: " + grade + "\n"); 
      } 
     } 
    } 

    catch (FileNotFoundException e) 
    { 
     System.out.println("File Programming Assignment 1 Data.txt could not be found or opened."); 
     e.printStackTrace(); 
     System.exit(0);   
    } 
    catch(IOException ioe) 
    { 
     System.out.err("IOException has occurred"); 
     ioe.printStackTrace(); 
     System.exit(0); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

-1。请参阅[如何提问和回答作业问题?](http://meta.stackexchange.com/q/10811/175248) – Makoto

相关问题