Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Stroring into Arrays reading from .txt file (Streamtokenizer) help...

Thread view: 
michael.rygh@gmail.com - 14 Dec 2005 17:25 GMT
I want to read from a file called scores.txt this is all it contains

Kevin 10
Jack 05

I want to store them into 2 arrays (name array and score array).

Then I will compare the users score earlier in the program to the
scores in the array, then sort them and display the scores in order.

I am just having problems trying to load/read the files into the array.
Can someone look through the code and tell me what you think is wrong.
Its just causing the program to lock up therefor I have to close it.
Any help would be great! Thanks..

        FileReader file = null;
        //tokens in a line of the text file

            try
    {
        System.out.println("doing high scores");
        file = new FileReader("scores.txt");
        String [] name1 = new String[1];
        int[] nScore = new int[1];

        StreamTokenizer inputStream = new StreamTokenizer(file);
        inputStream.wordChars(0x20,0x7f);

        int tokenType;

        tokenType = inputStream.nextToken();
        while (tokenType != StreamTokenizer.TT_EOF)
        {

        for(index9 =0; index < 2; index++)
        {

        name1[index9] = inputStream.sval;
        inputStream.nextToken();
        nScore[index9] = (int)inputStream.nval;
        //inputStream.nextToken();
        tokenType = inputStream.nextToken();

        }

        file.close();

        for(int index10 = 0; index <2; index++)
        {
            System.out.println(name1[index10]);
            System.out.println(nScore[index10]);
        }

        }
    }
    catch (Exception e)

    {
        if (e instanceof  FileNotFoundException)
        {
            System.out.println("FILENAME "+"scores.txt" + " NOT FOUND");
        }

        if(e instanceof ArrayIndexOutOfBoundsException)
        {
            System.out.println("WRONG NUMBER OF FILES SPECIFIED "+" IN COMMAND
LINE");
        }
        System.exit(1);
       

    }
Brandon McCombs - 15 Dec 2005 01:07 GMT
> I want to read from a file called scores.txt this is all it contains
>
[quoted text clipped - 68 lines]
>
>     }

When i had to do something like this for homework I just used the
split() method while reading one whole line at a time.

public String nflStats[][] = new String[numPlayers][numAttributes];
NFLStats() {   
    BufferedReader input = null;
    int plyrcnt = 0;
    try {
        input = new BufferedReader(new FileReader("nfl.csv"));
        }
        catch (FileNotFoundException e) {                
            System.out.println("File nfl.csv not found");
        }
       
    //Parse file, split each line using a , as the field separator.
    //Each token gets put into the nflStats array by player
    //Fields in file assumed to be comma delimited and in the        
    //following order:
    //number, Lname, Fname, position, years exp., school attended, team
    try {
        String lineFromFile = null;
        while (plyrcnt < nflStats.length) {
            lineFromFile = input.readLine();
            String[] playerData = lineFromFile.split(",");
            for (int attribute = 0; attribute <
                numAttributes; attribute++) {
                nflStats[plyrcnt][attribute] =    
                playerData[attribute];
            }
                plyrcnt++;
            }
    } catch (IOException IOex) {    
        System.out.println(IOex.getMessage());
    }
} //end NFLStats()


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.