Results 1 to 14 of 14
  1. #1
    ...eclipse is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Stringtokenizer and Arrays

    Hi fellow programmers

    I have got the following assignment to do,thus it is not my choice which methods/classes etc I have to use.

    Use a parallel array to show the data in the textfile.
    Stringtokenizer and Arrays-2.png

    This is what I did so far ...

    Java Code:
    import java.io.*;
    import java.util.StringTokenizer;
    
    public class divers
    {
    	public static void main (String[] args) throws IOException
    		{
    		BufferedReader br = new BufferedReader(new FileReader(results.txt));
    		String aLine = br.readLine();
    		String [] item = aLine.split("#");
    		}
    Could somebody please help me on how to go to work on the array part ?

    Thanks
    Last edited by ...eclipse; 10-30-2012 at 05:02 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default Re: Stringtokenizer and Arrays

    What does that data represent?
    Since it's the same number of things (a name followed by three numbers) then looks like 4 arrays? One for the name, and 3 for whatever those three number mean.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ...eclipse is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stringtokenizer and Arrays

    Oops sorry mistake from my side ... It is 5 divers,and each number is a score out of 10 for a round.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default Re: Stringtokenizer and Arrays

    So 4 arrays, or two if one is a 2-dimensional array, but I don't know what restrictions you have.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ...eclipse is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stringtokenizer and Arrays

    4 arrays yes. I must be able to calculate the average score of each round for every diver,and print the results to a textfile,aswell as display the winner. This I will be able to do,I am just having problems continuing from stringtokenizer to a parallel array

  6. #6
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Stringtokenizer and Arrays

    Quote Originally Posted by ...eclipse View Post
    4 arrays yes. I must be able to calculate the average score of each round for every diver,and print the results to a textfile,aswell as display the winner. This I will be able to do,I am just having problems continuing from stringtokenizer to a parallel array
    StringTokenizer (Java 2 Platform SE v1.4.2)

    There you can find out how a StringTokenizer works.

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Stringtokenizer and Arrays

    I think StringTokenizer (Java Platform SE 7 ) is more current, since it's from Java 7. Also, unless your teacher told you to use StringTokenizer specifically:

    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

  8. #8
    ...eclipse is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stringtokenizer and Arrays

    Thank you,the website did help. Yes we have to use stringtokenizer specifically.

  9. #9
    ...eclipse is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stringtokenizer and Arrays

    I still need help using the data I read from the textfile in an array ?

  10. #10
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Stringtokenizer and Arrays

    If you know beforehand how many entries there are, create the five arrays with that length. Then use a for-loop or a while-loop to read the entries and stuff the values into the correct arrays.

    If you don't know how many entries there are, create empty arrays and extend them as needed. You'd need to recreate the arrays as you go though, so it's a much more complicated method. Or, you could create an array that's much bigger than you think you'll need and just hope there aren't more entries (although from a programming standpoint, that's not a very good solution). If you choose to extend the arrays as new data comes in, System (Java Platform SE 6) might be of help.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default Re: Stringtokenizer and Arrays

    So, create the arrays.
    You will need to know (first off) how many people there are, since you need to know how big the arrays need to be (unless you are going to write code to dynamically resize them).
    Then you will need to populate them.
    You've already got code to read a line and split() it into its components, so now you need to write code to take that data and add it to the arrays.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default Re: Stringtokenizer and Arrays

    It helps if you indent your code properly.
    That way you can ensure brackets match up.

    Also, what line of code is it complaining about?

    Ah, hang on...is it the missing quotes around 'divers.txt' causing the problem?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    ...eclipse is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stringtokenizer and Arrays

    Okay this is what I have

    Java Code:
    import java.io.*;
    import java.util.*;
    	
    public class divers
    {
        public static void main (String[] args) throws IOException
            {
            BufferedReader br = new BufferedReader(new FileReader("results.txt"));
            String aLine = br.readLine();
            StringTokenizer strTokToken = new StringTokenizer(aLine, "#", false);
     
    String[] arrs1 = new String[5];
    int i = 0;
     while (strTokToken.hasMoreTokens())
                {
               arrs1[i] = strTokToken.nextToken();
                i++;
                }
            }
    }
    and it gives me the name and result of the first person,how do I get it to read the next line ?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default Re: Stringtokenizer and Arrays

    You need to loop.
    The normal structure is:
    Java Code:
    while ((aLine = br.readLine()) != null) {
    ... process the line in here
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Need help with StringTokenizer bad
    By Metastar in forum New To Java
    Replies: 5
    Last Post: 10-26-2010, 09:42 PM
  2. Help regarding StringTokenizer
    By mahesh19nov in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 09:25 AM
  3. Help with StringTokenizer!
    By ookie833 in forum New To Java
    Replies: 13
    Last Post: 12-14-2008, 04:09 PM
  4. StringTokenizer
    By carderne in forum New To Java
    Replies: 1
    Last Post: 01-26-2008, 08:19 PM
  5. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •