Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default File Reader/Storing Token - Urgent

    Hello, I am new to forums and i'm doing a school project. I have some troubles understanding tokenziers and file reader. Professor gave us code that will read a file for us and get the information we need the only problem is storing this information in a two dimensional array.

    Here is the code for reading a file and separating them into tokenz. My only problem is storing each word in a two dimensional array. When i have tried my way I keep getting error.

    Java Code:
    public static void main(String args[]) throws Exception
    	{
    		File inputFile = new File("course_list.txt");
    		Scanner input = new Scanner(inputFile);
    
    		while(input.hasNextLine())
    		{
    			String line = input.nextLine();
    			line.substring(0, line.indexOf(","));
    			StringTokenizer str = new StringTokenizer(line, ", ");
    
    			while(str.hasMoreTokens())
    			{
    				System.out.println(str.nextToken().trim());
    			}
    		}
    	}
    When I run the code it prints out each word as it is suppose to I just need to store each word in a two dimensional array so can anyone help me out and provide the code that I require for it to work?

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Why don't you post what you have tried so we can see where you're having trouble?

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Your professor gave you the StringTokenizer code?
    Link him please to the API Doc :)

    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.
    Quote Originally Posted by Bagzli
    When i have tried my way I keep getting error
    What was your way? (btw: the split method will return an array :D)

  4. #4
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Well here it is but this is using only 1 dimensional array because of the errors i keep getting, i tried to make it simpler and only store 1 word per line but this gives me a null value every time. Also if i put the line of code that stores the variable inside the inner loop program crashes.

    Java Code:
    public static void main(String args[]) throws Exception
    	{
    		File inputFile = new File("course_list.txt");
    		Scanner input = new Scanner(inputFile);
    		String[] test = new String[(int) inputFile.length()];
    
    		while(input.hasNextLine())
    		{
    			String line = input.nextLine();
    			line.substring(0, line.indexOf(","));
    			StringTokenizer str = new StringTokenizer(line, ", ");
    			int ctr = 0;
    			test[ctr]=str.nextToken().trim();
    			System.out.println("Test");
    			System.out.println(test[ctr]);
    			while(str.hasMoreTokens())
    			{
    				System.out.println(str.nextToken().trim());
    				
    				
    			}
    			
    			ctr++;
    		}
    	}
    It is bad because i do not understand tokenizers well so can somebody please give me a better way to do this? I have to use what we have learned and that is file I/O reading files and some way of storing the words which he used tokenizers.

    So can a kind soul help a mate in trouble?


    Edit: Is there a way to search for a specific word inside a two dimensional and one dimensional string array?

    Example: I am looking for word "cool" inside a 1D and 2D String array which has all of its values initialized.
    Last edited by Bagzli; 02-28-2011 at 08:07 PM.

  5. #5
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    A few things -

    The reason you're getting null values is because your counter is set to 0 in the while loop. So you're assigning the first token to element 0 each pass through. You'll want to declare int ctr outside the first while loop so it actually increments.

    Here is an overview of what is going on with the tokenizers:
    Java Code:
    while(input.hasNextLine()) // If there is another line in the file, continue
    {
    	String line = input.nextLine(); // Remove the next line from the input stream
    
    	StringTokenizer str = new StringTokenizer(line, ", "); // Gets the line and the String tokenizer divides it up into tokens by the dilimeter - ", "
    
    	while(str.hasMoreTokens()) // Run if there are any tokens left from the line
    	{
    		System.out.println(str.nextToken().trim()); // Gives you the next token and removes it from the list
    	}
    }

    Also if i put the line of code that stores the variable inside the inner loop program crashes.

    This would happen if you had left the System.out.println in the while loop and added the line that stores the variable. When str.nextToken() is called, it pulls the token out of the list. So if there was only one token(word) left, the while loop's hasMoreTokens() would return true, you would remove the token with the println() function, and it would error out if you tried to call str.nextToken() while assigning it to the array b/c there are no tokens left. To get around this you could remove the line which prints it to the screen or do something like this (pseudo-code):

    Java Code:
    while (hasMoreTokens) {
          String temp = nextToken
          println(temp)
          array[counter] = temp
    }

    Is there a way to search for a specific word inside a two dimensional and one dimensional string array?

    Example: I am looking for word "cool" inside a 1D and 2D String array which has all of its values initialized.
    To find a specific word in an array, you'd have to use a for loop and check if each element is what you're looking for. One for loop for a 1D and two nested for loops for 2D.

  6. #6
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    I tested and got code to work like a charm! Thanks so much. I have another question though. I am going to take a file and read information that i'm storing in a 2d variable. Now with this code its working perfectly just the way i want it. The problem that i am encountering is ArrayIndexOutOfBoundsException How can i read from a file how many lines are there are and also how many times my delimiter shows up?

    I have made the File Reader a whole method of its own and since its going to be reading multiple files I need to reuse it therefore its a method on the side that I will be using. Problem is I don't know how to get the right size of my array. Here is the final code of my file reader:
    Java Code:
    package courseChanger;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class FileReader
    {
    	public static String[][] fileReader(int i, int ctr, String file) throws FileNotFoundException
    	{
    		File inputFile = new File(file);
    		Scanner input = new Scanner(inputFile);
    		String[][] infoStorage = new String[i][ctr];
    		i = 0;
    		while(input.hasNextLine())
    		{
    			String line = input.nextLine();
    			line.substring(0, line.indexOf(","));
    			StringTokenizer str = new StringTokenizer(line, ",");
    			ctr = 0;
    
    			while(str.hasMoreTokens())
    			{
    				//System.out.println(str.nextToken().trim());
    				String temp = str.nextToken().trim();
    				System.out.println(temp);
    				infoStorage[i][ctr] = temp; 
    				ctr++;
    			}
    			i++;
    		}
    		return infoStorage;
    	}
    }
    now when i call this method i need to be able to determine the size of the array. Here is sample file of what i am reading.

    Java Code:
    Course Name, Course Id, Time
    Introduction to Philosphy, Phil101, Mon 2:00p-4:00p Wed 9:00a-11a Thu 10:00a-12:00p
    Introduction to Programming 2, Prog201, Wed 9:00a-11:00a Thu 10:00a-12:00p Fri 1:00p-3:00p
    Meaning of Life, Phil411, Mon 9:00a-12:00p Tue 4:00p-7:00p
    Introduction to Administrative Studies, Adms101, Tue 12:00p-2:00p Wed 12:00p-2:00p Thu 12:00p-2:00p
    Last edited by Bagzli; 03-01-2011 at 12:28 AM.

  7. #7
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Forgot to say thanks for all your help guys I really appreciate it.

  8. #8
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Do you have to use a 2d array the entire time? Or could you use an ArrayList? ArrayLists don't have to have a length defined ahead of time. If you have to use a 2d array, the only way i can think of to know the size of the files would be to read through the file twice. First, counting the lines and tokens so you can setup your array. Second, parse through the file to actually pull out the data.

    A better way would be to use ArrayLists. Some thing like..
    Java Code:
    ArrayList a = new ArrayList();
    ArrayList b = new ArrayList();
            
    while(hasnext)
    {
    	b = new ArrayList();
    	...
    
    	while(moretokens)
    	{
                    b.add(token);
    	}
                
            a.add(b);
    }
    So you'd end up with an ArrayList of ArrayLists and after you've pulled all that out you can create your 2d array...
    Java Code:
    String[][] infoStorage = new String[a.size()][];
    ...and fill it from ArrayList a using the toArray method.

  9. #9
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    sadly we did not cover array list and i doubt he would accept it. So i have to use 2d array. and do you have any ideas on how i can code this to count all the lines and tokens first time and then use that to pull out the data?

  10. #10
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Its pretty easy to do with what you've already coded. Basically you'll just have to copy the entire while loop and paste it above where you setup your infoStorage array. Then remove the line that assigns the value to the array, b/c at that point you're just counting. So after that while loop runs, ctr and i have the values you need.

    Since the Scanner has had all it's lines read, you'll also have to reload the file into it between the two outer while loops.
    Java Code:
    input = new Scanner(inputFile);
    Edit:

    Actually, that won't work. Because ctr gets overwritten everytime. Hm.
    Last edited by Shoss; 03-01-2011 at 01:55 AM.

  11. #11
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Ok, so what i said would half work. Except just count the rows with the first while loop and ignore the tokens. Then you can define your array like -
    Java Code:
    String[][] infoStorage = new String[i][];
    And in your second while loop, after you define your StringTokenizer, you can say -
    Java Code:
    infoStorage[i] = new String[str.countTokens()];

  12. #12
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    I'm sorry i tried my best to follow what you said but i'm very lost. could you edit my code and implement yours so i can see better what you are saying?

  13. #13
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Yeah, i sort of typed as i thought. It would be something like this -

    Java Code:
    File inputFile = new File(file);
    Scanner input = new Scanner(inputFile);
    
    //Read through the file once, counting the rows
    while(input.hasNextLine())
    {
    	String line = input.nextLine();
    
    	i++;
    }
    
    //Reload the file
    input = new Scanner(inputFile);
    
    //Define the array now that we know the first dimension
    String[][] infoStorage = new String[i][];
    i = 0;
    while(input.hasNextLine())
    {
    	String line = input.nextLine();
    	line.substring(0, line.indexOf(","));
    	StringTokenizer str = new StringTokenizer(line, ",");
    
            //For each column of tokens, initialize a new array
            //str.countTokens() counts the number of tokens in the tokenizer
    	infoStorage[i] = new String[str.countTokens()];	
    
    	ctr = 0;
    
    	while(str.hasMoreTokens())
    	{
    		//System.out.println(str.nextToken().trim());
    		String temp = str.nextToken().trim();
    		System.out.println(temp);
    		infoStorage[i][ctr] = temp; 
    		ctr++;
    	}
    	i++;
    }

  14. #14
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    so you want me to go with a ragged array. I still have to declare it somewhere though, set its size so how would i know the size of that? That loop will only give me how many lines there are, what about tokens? Could i just take my whole first method and have it keep 2 counters rather than storing tokens and then return me the inner count? and then another method to return me outer count?

  15. #15
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    You could do that, but then you'd have to keep track of which row you are counting each set of tokens for. What i have above should work without counting the tokens ahead of time b/c of this line.
    Java Code:
    infoStorage[i] = new String[str.countTokens()];
    The tokenizer can count how many tokens it contains without pulling them out, so we create each column to store the tokens as we get to them.

  16. #16
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    ok so i see what you are saying now. One thing i forgot to mention is that i will be using this multiple times so its going to end up being a method that i will call. This final array will be stored in another array in a different class. Now if i just use it like this do i have to initialize the second array inner count? Will just doing a line count and then returning a number which i will use to initiate the outer array size be enough?

  17. #17
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Yep, that should be all you need to do. Since it is a jagged array, you don't really have a need to define the second column's length anyway.

  18. #18
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    ok, what about object arrays, i can't seem to get those to work how I want. For example i want to create an array of a custom made objecst and this object takes parameters. How do i accomplish this?

    let say parameters are int i, double d, String s
    Java Code:
    Object objectName = new Object[i]
    objectName[i] = ???
    I really am out of ideas how to make it work.

  19. #19
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Bagzli View Post
    ok, what about object arrays, i can't seem to get those to work how I want. For example i want to create an array of a custom made objecst and this object takes parameters. How do i accomplish this?

    let say parameters are int i, double d, String s
    Java Code:
    Object objectName = new Object[i]
    objectName[i] = ???
    I really am out of ideas how to make it work.
    No, it'd be

    Java Code:
    Object thingie = new WhateverClassThatTakesThreeParameters(i, d, s);
    objectArray[1] = thingie;
    In other words, create the object the way you normally would, then assign it to a slot in the array.

  20. #20
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    I know i'm asking a lot but if I had another choice or way to find out I really wouldn't bother you guys so much, i hope you will bear with me a bit longer cause i got another question >.<

    Java Code:
    this.fullName = "userlogs/" + this.userLogId;
    		if(!File.exists(this.fullName)
    		{
    			File userLogId = new File("userlogs/" + this.userLogId);
    		}
    I want to check if this file exists, if it doesn't create it. userLogId is a variable
    I know this is wrong and i'v tried just about anything i know but it just keeps giving me errors.

    the name of the file will be what is stored in the String fullName variable. I am trying to use relative path to show java where the file is located which is inside userlogs folder.
    Last edited by Bagzli; 03-01-2011 at 10:20 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. problem with file reader
    By Stormrage in forum New To Java
    Replies: 7
    Last Post: 05-15-2010, 11:48 PM
  2. what's the token for excel file?
    By anthrax in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 07:19 AM
  3. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 08:19 AM
  4. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 07:43 PM
  5. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 03:03 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
  •