Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how to save input from a text file in to array
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
supahsain08




PostPosted: Wed Mar 26, 2008 5:13 pm   Post subject: how to save input from a text file in to array

I got this assignment
This is what i am suppose to do
-----------------------------------------------------------------------------------------------------------
5. The program must store the following data about each student in this Talent Show:
* The student's name (store as a String)
* The student's average time score [only for the events they competed in] (store as a Double)

Note: Some of the data comes directly from the text file, and some of the data must be calculated from the data that is read from the file.

6. The program must search through all of the student's scores to find the student who has the minumum average time of all students.
7. You must calculate how many events each student played. Zero values are not counted.
8. You must then generate a report on the screen that prints out
* the total number of students in the competition,
* the winning student's ...
o name
o times
o average time (rounded to one decimal places)

Sample output
Posted Image, might have been reduced in size. Click Image to view fullscreen.
btw this is the data file
Bart Simpson
7.5
12.3
7.8
19.2
9.9
5.3
8.5
11.8
2.2
4.6
Stewie Griffin
9.5
29.7
7.8
22.5
9.9
12.6
8.5
0
8.2
0
Pinky
2.5
0
1.8
0
3.9
0
6.5
0
5.2
12.1
Rocky N Bullwinkle
10.0
22.2
9.5
17.5
9.9
1.5
8.7
23.7
9.2
11.0
Angelica Pickles
5.5
11.1
6.8
12.2
7.9
13.3
8.1
5.1
7.2
7.9
Pink Panther
8.5
5.5
8.8
6.6
8.9
7.7
9.9
8.8
2.2
9.9
Josie
9.5
0
8.8
12.2
9.9
0
8.5
0
9.2
5.3
Rogue
8.5
1.1
7.8
2.2
7.9
3.3
7.5
4.4
8.2
5.5
Usagi Tsukino
8.5
15.5
8.8
30.1
9.9
19.7
9.5
11.0
8.2
8.6
Yosemite Sam
0
15.2
0
29.5
3.9
0
0
16.0
0
7.7
----------------------------------------------------------------------------------------------------
My code so far

code:
import java.io.*;

public class test
{
    public static void main (String [] args)
    {

        FileInputStream File1;
        DataInputStream In;
        String fileInput = "";

        try
        {
           File1 = new FileInputStream ("data.txt");


            In = new DataInputStream (File1);


            while (In.available () > 0)
            {
                fileInput = In.readLine ();
                System.out.println (fileInput);
            }

            In.close ();
        }

        catch (FileNotFoundException e)
        {
            System.out.println ("Error - this file does not exist");
        }

        catch (IOException e)
        {
            System.out.println ("error=" + e.toString ());
        }

    }
}


My question, how do i save the data in to an array, and how do i seperatly save names in to different array, and their scores in to different arrays

btw he said you can't use scanner class

Thanks
Sponsor
Sponsor
Sponsor
sponsor
Vermette




PostPosted: Wed Mar 26, 2008 7:55 pm   Post subject: Re: how to save input from a text file in to array

Are you allowed to use BufferedReader? That would allow you to pull in one line at a time, then you can test if the line is a number or a name.

Something like the following:

code:

BufferedReader br = new BufferedReader(new FileReader(new File("yourfilepath")));


Here is the javadoc link, which will give you info on all the methods available in the class:
BufferedReader
syntax_error




PostPosted: Wed Mar 26, 2008 11:36 pm   Post subject: Re: how to save input from a text file in to array

a) your class name should have the first char in it CAP

b) think on how you would input data from the user using the scanner class into an array; then use the same method but you read the line first instead of having the user enter it first.

I hope that helps? if not heres how you would do it with the scanner class apply it to buffer
code:

    double[][] marks = new double [STUDENTS][TESTS];
    for (int i = 0; i < STUDENTS; i++){
   
      System.out.println ("For student #" + (i + 1));
     
      for (int j = 0; j < TESTS; j++){
     
        System.out.print (" and test # " + (j + 1) + " the mark was: ");
        marks[i][j] = sc.nextDouble();
 
      }
wtd




PostPosted: Thu Mar 27, 2008 10:25 am   Post subject: RE:how to save input from a text file in to array

Can we please use Java 1.5's printf a bit to clean up code in examples?

code:
    double[][] marks = new double [STUDENTS][TESTS];
    for (int i = 0; i < STUDENTS; i++){
   
      System.out.printf("For student #%d\n", i + 1);
     
      for (int j = 0; j < TESTS; j++){
     
        System.out.printf(" and test #%d the mark was: ", i + 1);
        marks[i][j] = sc.nextDouble();
 
      }
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: