Results 1 to 20 of 21
- 02-28-2011, 07:15 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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.
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?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()); } } }
- 02-28-2011, 07:20 PM #2
Why don't you post what you have tried so we can see where you're having trouble?
- 02-28-2011, 07:22 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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.What was your way? (btw: the split method will return an array :D)
Originally Posted by Bagzli
- 02-28-2011, 08:01 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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.
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.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++; } }
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.
- 02-28-2011, 08:40 PM #5
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 }
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.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.
- 02-28-2011, 09:39 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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:
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: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; } }
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.
- 03-01-2011, 12:25 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Forgot to say thanks for all your help guys I really appreciate it.
- 03-01-2011, 01:25 AM #8
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..
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:ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); while(hasnext) { b = new ArrayList(); ... while(moretokens) { b.add(token); } a.add(b); }
...and fill it from ArrayList a using the toArray method.Java Code:String[][] infoStorage = new String[a.size()][];
- 03-01-2011, 01:31 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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?
- 03-01-2011, 01:49 AM #10
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.
Edit:Java Code:input = new Scanner(inputFile);
Actually, that won't work. Because ctr gets overwritten everytime. Hm.Last edited by Shoss; 03-01-2011 at 01:55 AM.
- 03-01-2011, 02:05 AM #11
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 -
And in your second while loop, after you define your StringTokenizer, you can say -Java Code:String[][] infoStorage = new String[i][];
Java Code:infoStorage[i] = new String[str.countTokens()];
- 03-01-2011, 02:19 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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?
- 03-01-2011, 02:58 AM #13
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++; }
- 03-01-2011, 06:26 PM #14
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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?
- 03-01-2011, 06:57 PM #15
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.
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.Java Code:infoStorage[i] = new String[str.countTokens()];
- 03-01-2011, 07:21 PM #16
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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?
- 03-01-2011, 07:30 PM #17
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.
- 03-01-2011, 08:29 PM #18
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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
I really am out of ideas how to make it work.Java Code:Object objectName = new Object[i] objectName[i] = ???
- 03-01-2011, 08:30 PM #19
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
- 03-01-2011, 09:08 PM #20
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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 >.<
I want to check if this file exists, if it doesn't create it. userLogId is a variableJava Code:this.fullName = "userlogs/" + this.userLogId; if(!File.exists(this.fullName) { File userLogId = new File("userlogs/" + this.userLogId); }
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.
Similar Threads
-
problem with file reader
By Stormrage in forum New To JavaReplies: 7Last Post: 05-15-2010, 11:48 PM -
what's the token for excel file?
By anthrax in forum New To JavaReplies: 3Last Post: 01-27-2010, 07:19 AM -
Java file reader...?
By prabhurangan in forum New To JavaReplies: 3Last Post: 11-21-2008, 08:19 AM -
[SOLVED] Need help with file reader
By syed.shuvo in forum New To JavaReplies: 6Last Post: 09-27-2008, 07:43 PM -
help with file reader
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks