Results 1 to 3 of 3
  1. #1
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default Manipulating String Tokenizer

    I have a text file that reads

    1 0 0 0 0 1 0 2 0 0 0 0 1 0 3 0 0 0 0 1 1 4 0 0 0 0 1 1 5 0 1 0 0 1 1...etc
    My code is this atm:

    Java Code:
    public class calcStats
    	{
    		public static void main(String[] args) 
    		{
    			int[][] jobBonusList = new int[70][7];
    			
    			try
    			{
    				URL jobBonusURL = new URL("location of file");
    				BufferedReader jobBonusInput = new BufferedReader(new InputStreamReader(jobBonusURL.openStream()));
    			   	String jobLine = jobBonusInput.readLine();
    				StringTokenizer st = new StringTokenizer(jobLine); 
    				
    				while (st.hasMoreTokens()) 
    				{ 						
    					for(int i = 0; i < 70; i++)
    					{
    						for(int j = 0; j < 7; j++)
    						{
    							String s = st.nextToken();
    							int number = Integer.parseInt(s);
    							jobBonusList[i][j] = number;
    							System.out.println("i is " + i + " and j is " + j + " and the value is " + jobBonusList[i][j]);
    						}
    					}
    				}
    			}
    			catch(IOException e)
    			{
    				System.err.println("Error: " + e);
    			}
    		}
    	}
    My code works fine as is but is there a way so that it can do the same thing using this file?

    1 0 0 0 0 1 0
    2 0 0 0 0 1 0
    3 0 0 0 0 1 1
    4 0 0 0 0 1 1
    5 0 1 0 0 1 1
    ...etc
    The print statment in the loop shows this:

    i is 0 and j is 0 and the value is 1
    i is 0 and j is 1 and the value is 0
    i is 0 and j is 2 and the value is 0
    i is 0 and j is 3 and the value is 0
    i is 0 and j is 4 and the value is 0
    i is 0 and j is 5 and the value is 1
    i is 0 and j is 6 and the value is 0
    i is 1 and j is 0 and the value is 2
    i is 1 and j is 1 and the value is 0
    i is 1 and j is 2 and the value is 0
    i is 1 and j is 3 and the value is 0
    i is 1 and j is 4 and the value is 0
    i is 1 and j is 5 and the value is 1
    i is 1 and j is 6 and the value is 0
    ...etc

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    There are various ways to skin this cat. A couple of ways are:

    (1) Read the whole file into a big buffer, changing line breaks into spaces as you go. So for each readLine(), append the line to a StringBuilder, and append a space after it.

    (2) Again, working on the whole file, use String.split() instead of a StringTokenizer. (StringTokenizer is pretty much deprecated nowadays anyway.) This way, you can say you want the split to be on a line break or a space, or indeed any non-numeric character. For example, calling split("\\s+") on a string containing the whole file will return you the tokens split on spaces or line breaks (or indeed, will split it on just spaces if you've first used method (1)).

    (3) Still go line by line, but have a wrapper method getNextInt() which automatically reads the next line and gets the next lot of tokens when it gets to the end of a line. So off t'top of my head, something like:

    Java Code:
    Integer getNextInt() {
      String tok;
      do {
        if (currentStringTokenizer == null) {
          currentStringTokenizer = nextTokenizedLine();
          if (currentStringTokenizer == null) {
            return null; // no more numbers
          }
        }
        if (currentStringTokenizer.hasMoreTokens()) {
          tok = currentStringTokenizer.nextToken();
        } else {
          currentStringTokenizer = null;
        }
      } while (tok == null);
      return Integer.parseInt(tok);
    }

  3. #3
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Thank you for the help. Using the tips you gave me, I came up with this and it seems to work fine.

    Input file:
    1 0 0 0 0 1 0
    2 0 0 0 0 1 0
    3 0 0 0 0 1 1
    4 0 0 0 0 1 1
    5 0 1 0 0 1 1
    etc...
    Java Code:
    public class calcStat
    	{
    		public static void main(String[] args) 
    		{
    			int[][] jobBonusList = new int[70][7];
    			String words[] = new String[70];
    			String jobLine;
    			
    			try
    			{
    				URL jobBonusURL = new URL("location of file");
    				BufferedReader jobBonusInput = new BufferedReader(new InputStreamReader(jobBonusURL.openStream()));
    			   	
    				for(int i = 0; i < 70; i++)
    				{
    					jobLine = jobBonusInput.readLine();
    					words[i] = jobLine;
    						
    					StringTokenizer st = new StringTokenizer(jobLine); 
    					
    					while (st.hasMoreTokens()) 
    					{
    						for(int j = 0; j < 7; j++)
    						{	
    							String s = st.nextToken();
    							int number = Integer.parseInt(s);
    							jobBonusList[i][j] = number;
    						}
    					}
    				}
    			}
    			catch(IOException e)
    			{
    				System.err.println("Error: " + e);
    			}
    			
    			for(int i = 0; i < 70; i++)
    			{
    				for(int j = 0; j < 7; j++)
    				{
    					System.out.print(jobBonusList[i][j] + " ");
    				}
    				System.out.println("");
    			}
    		}
    	}

    Output of print statement
    1 0 0 0 0 1 0
    2 0 0 0 0 1 0
    3 0 0 0 0 1 1
    4 0 0 0 0 1 1
    5 0 1 0 0 1 1
    etc....

    Since the jobBonusList array is an int, I am assuming that all the extra spaces were removed. I am going to put this into my main code to make sure it works properly.

Similar Threads

  1. manipulating a string (Urgent)
    By ariz in forum New To Java
    Replies: 4
    Last Post: 03-31-2009, 05:00 AM
  2. string tokenizer
    By twinytwo in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 02:10 PM
  3. Problem with string tokenizer
    By twinytwo in forum AWT / Swing
    Replies: 2
    Last Post: 03-26-2009, 11:27 AM
  4. String Tokenizer And Arraya
    By everlast88az in forum Advanced Java
    Replies: 2
    Last Post: 11-06-2008, 11:20 PM
  5. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05:00 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
  •