Results 1 to 9 of 9
  1. #1
    zenobiten is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Question 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
    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());
        }
      }
    }
    And in the txt page i have:

    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

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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,

    Java Code:
    if (store[j] != null) {
            System.out.println(store[j]);
            }
    If it's not null, then only print it.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    zenobiten is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default wow

    Great !
    Such a simple and great solution, I'll try that. Thank you !!!

  4. #4
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default

    Goldest,
    where did you find your avatar ??
    Rakesh Mehta

  5. #5
    zenobiten is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default 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

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by zenobiten View Post
    i get this message:
    cannot return a value from method whose result type is void
    return store[j];
    ^
    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,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    zenobiten is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    try looking for "joker" on google images....

  8. #8
    zenobiten is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    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,

    Goldest
    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....


    /Zen
    Last edited by zenobiten; 02-23-2011 at 11:16 AM.

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by zenobiten View Post
    I'm planning to change the static array to a ArrayList as soon a i can figure it out
    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.

    Quote Originally Posted by zenobiten View Post
    I think I don't really understand the basic foundation of the java-lagnuage yet...
    If you are interested, here is a good link to get started : Learning the Java Language

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Reading csv file into 2D array - HELP!!!
    By mikeg in forum New To Java
    Replies: 17
    Last Post: 04-12-2011, 08:36 AM
  2. Methods for reading file with an array
    By johnmergene in forum New To Java
    Replies: 5
    Last Post: 01-26-2011, 04:20 AM
  3. Help with reading from file into an array
    By Trad in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 12:16 PM
  4. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  5. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 PM

Posting Permissions

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