Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2008, 11:11 PM   PM User | #1
supahsain
New to the CF scene

 
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
supahsain is an unknown quantity at this point
how to save input from a text file in to array

I got this assignment, and almost have no clue, and my teacher doesn't even teaches us anything
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

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
supahsain is offline   Reply With Quote
Old 03-27-2008, 05:56 AM   PM User | #2
icm9768
New Coder

 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 2 Times in 2 Posts
icm9768 is an unknown quantity at this point
Now that you're reading the file in okay you have multiple steps to take.

1) The first question I have is if you have to store your data in arrays or if you can use Vectors/ArrayLists. I don't see any specific requirements in the information you gave although you seem to infer that you have to use arrays. Using Vectors in this case would be easier since you don't know how many student's (and student times) that you have. Vectors allow you to dynamically add data while arrays are somewhat more involved when you don't know how many elements you need to add.

2) The next issue is if you have to do all of this work in a static context vs. using an object-oriented approach (e.g. creating a Student class that stores a student name and each of their times).

3) Determining if a line from the file is a name or a time is another key part of your problem. You'll have to do some processing on each line to see if you're dealing with a character String or a double 'disguised' as a String. I would recommend an approach where you use the Double.parseDouble() method on each line. If it is in fact a time, then the method should succeed but if not (i.e. it's a student name) then you can catch the NumberFormatException thrown and you'll know that you have a new Student name.

I think you need to address the first two points before moving forward.
icm9768 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:18 AM.


Advertisement
Log in to turn off these ads.