Results 1 to 9 of 9
- 02-22-2011, 11:33 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Proplem with reading txt-file into array //null
I found a closed string here on how to sort the 'not reading the first line' -problem with removing the 'while' .
Then comes the next problem, how do i stop the reading when there is nothing more ro read?
This is what I have at the moment
And in the txt page i have:PHP Code:import java.io.*; class FileRead { public static void main(String args[]) { try{ FileInputStream fstream = new FileInputStream("textfile.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; // while ((strLine = br.readLine()) != null) { String[] store; store = new String[10]; for (int j = 0; j < store.length; j++){ store[j] = br.readLine(); System.out.println(store[j]); } // } in.close(); }catch (Exception e){ System.err.println("Error: " + e.getMessage()); } } }
one
two
three
four five
...
The General Output says:
--------------------Configuration: <Default>--------------------
one
two
three
four five
...
null
null
null
null
null
Process completed.
//How do i stop at the '...'?
Please help me
Thanks
/Zen
- 02-22-2011, 12:20 PM #2
First, you are iterating over your array of size 10 and printing everything on console. You text file contains only 4 elements, so rest of the array will be null by default.
Instead of printing everything, why don't you check whether the array element is null or not? Like,
If it's not null, then only print it.Java Code:if (store[j] != null) { System.out.println(store[j]); }
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-23-2011, 10:15 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
wow
Great !
Such a simple and great solution, I'll try that. Thank you !!!
- 02-23-2011, 10:39 AM #4
Goldest,
where did you find your avatar ??Rakesh Mehta
- 02-23-2011, 10:43 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Next issue...
That worked great, but now i want to remove the println and replace it with return. Reason being i want to use the read lines as choices in a combobox in another file.
when I write:
if (store[j] != null){
return store[j];
i get this message:
cannot return a value from method whose result type is void
return store[j];
^
Is there a way around or do i have to do it differently all together?
The final output is a GUI where u make choices that will be forwarded. BUT... the choices are supposed to be changeable with the txt-files. So that U don't need programming skills to do them.
... i'm way in over my head, but I want to do this and I'm determined to learn as I work on this!!!
Any suggestions?
Thanks
Zen
- 02-23-2011, 10:56 AM #6
Currently you are playing around in main method only. Any method with return type as void doesn't return anything. You need to make sure that your mathod signature mentions the type that you are going to return.
Here you are trying to return a String value, which means that your method should have String as your return type.
Here is a small example,
Java Code:public String getValue() { String test = "MyValue"; return test; }
I would advice you reading this: Returning A Value From A Method
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-23-2011, 10:57 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
try looking for "joker" on google images....
- 02-23-2011, 11:12 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
But... First of all; I love you!! My native language is Swedish, but your explanations are the first ones that I can actually understand, THANK U.
Back to the but... if i do as u showed in the example i only get one return value... I need to get the same amount of values as there happen to be in the textfile at that particular moment... And I also need a name for that value for further usage later... thats why 'm using the array. I'm planning to change the static array to a ArrayList as soon a i can figure it out.
My life is sooo full of questions at the moment:confused::confused::confused:
I think I don't really understand the basic foundation of the java-lagnuage yet... the methods are at bit blurry... the way the methods are mixed sometimes, and sometimes not....
/ZenLast edited by zenobiten; 02-23-2011 at 11:16 AM.
- 02-23-2011, 12:10 PM #9
Yes, you can use the ArrayList of Strings and use the add method to store every line as soon as you read it. Use the while loop that you have commented out in your original post. Then once done, return it, the same way you are returning the String in current code.
If you are interested, here is a good link to get started : Learning the Java Language
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
Reading csv file into 2D array - HELP!!!
By mikeg in forum New To JavaReplies: 17Last Post: 04-12-2011, 08:36 AM -
Methods for reading file with an array
By johnmergene in forum New To JavaReplies: 5Last Post: 01-26-2011, 04:20 AM -
Help with reading from file into an array
By Trad in forum New To JavaReplies: 3Last Post: 10-22-2010, 12:16 PM -
Is it better to set array elements to null before setting the arrayreference to null?
By kreyszig in forum Advanced JavaReplies: 6Last Post: 10-18-2010, 10:40 AM -
Reading text file into an array
By Mahesh_ps in forum New To JavaReplies: 1Last Post: 10-09-2009, 03:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks