Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: String[] to String

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default String[] to String

    Hi, I would like to convert a String array (String[]) to a String which I can print. I don't know how to do this as there's no simple method like String s = stringArray.toString() or something as far as I know. What I'm trying to do is read a text file which has multiple lines and store the entire text file as one variable which I can then include in a JOptionPane statement.

    Here is some relevant code:

    FileInputStream fis = null;
                BufferedInputStream bis = null;
                DataInputStream dis = null;
                FileInputStream fis2 = null;
                BufferedInputStream bis2 = null;
                DataInputStream dis2 = null;
    			try {
    				//Reading file
    				fis = new FileInputStream(rfile);
                    bis = new BufferedInputStream(fis);
                    dis = new DataInputStream(bis);
                    fis2 = new FileInputStream(rfile);
                    bis2 = new BufferedInputStream(fis);
                    dis2 = new DataInputStream(bis);
                    String aLine;
                    int num = 0;
                    while(dis.readLine() != null)
                    {
                    	num++;
                    }
                    String[] notesarr = new String[num];
                    for(int i=0; i < num; i++)
                    {
                    	notesarr[i] = dis2.readLine();
                    }
                    //Display notes
        			JOptionPane.showMessageDialog(null, "Notes: \n"+notesarr,"Notes",JOptionPane.INFORMATION_MESSAGE);
        			System.exit(0);
    			} catch (IOException e) {}

    This doesn't work though because when I print notesarr in the JOptionPane statement it just comes out as "[Ljava.lang.String;@affc70".


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String[] to String

    Use a StringBuilder or StringBuffer, appending the lines as you loop through the array (or read the file). Then just convert to a string
    StringBuffer sb = new StringBuffer();
    String[] lines = {"test1", "test2"};
    for ( int i = 0; i < lines.length; i++ ){
         sb.append(lines[i]);
    }
    String mystring = sb.toString();

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: String[] to String

    For some reason, when I do this, it prints out: "nullnull". I have no idea why.

    StringBuffer sb = new StringBuffer();
                    String[] notesarr = new String[num];
                    for(int i=0; i < num; i++)
                    {
                    	notesarr[i] = dis2.readLine();
                    	sb.append(notesarr[i]);
                    }
                    notes = sb.toString();
                    //Display notes
        			JOptionPane.showMessageDialog(null, "Notes: \n"+notes,"Notes",JOptionPane.INFORMATION_MESSAGE);
        			System.exit(0);

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String[] to String

    You may have to post more code...where does the variable num come from? If you've read the file already then readLine returns null (and this nulls are appended to the StringBuffer)

  5. #5
    Junior Member srikrish85's Avatar
    Join Date
    Feb 2011
    Location
    Chennai
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String[] to String

    Use StringBuilder to do the task

  6. #6
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: String[] to String

    Quote Originally Posted by copeg View Post
    You may have to post more code...where does the variable num come from? If you've read the file already then readLine returns null (and this nulls are appended to the StringBuffer)
    Hmm, technically I did read the file once, to get the value of num (which is just the number of lines in the file). However, I used two different readers to do this (see the code in my first post).

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: String[] to String

    Wouldn't it clean things up quite a bit to do this:
    BufferedReader in = new BufferedReader(new FileReader(rfile));
    StringBuilder text = new StringBuilder();
    try {
       while (in.ready()) {
          text.append(in.readLine());
       }
       JOptionPane(...);
    } catch (IOException e) {
       System.err.println(e.getMessage());
    }  finally {
       in.close();
    }
    I see no need even to create an array of strings, and I don't really like this build-up of objects for reading files. BufferedReader() does everything you need imo.

  8. The Following User Says Thank You to aisthesis For This Useful Post:

    The_Mexican (February 19th, 2011)

  9. #8
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: String[] to String

    That makes it work. However, when I print the text, it strings all the lines in the text together, with no enters to separate lines.

    This is the text file:

    One
    Two
    Three
    Four
    Five

    When I run this line:
    JOptionPane.showMessageDialog(null, "Notes: \n"+text,"Notes",JOptionPane.INFORMATION_MESSAGE);

    The output is:

    Notes:
    OneTwoThreeFourFive

  10. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: String[] to String

    technically I did read the file once, to get the value of num (which is just the number of lines in the file). However, I used two different readers to do this (see the code in my first post).
    There shouldn't be a need to read the file twice.
    That makes it work. However, when I print the text, it strings all the lines in the text together, with no enters to separate lines.
    Append a new line
    while ( //reading ){
        text.append(in.readLine());
        text.append("\n");
    }

  11. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (February 19th, 2011)

  12. #10
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: String[] to String

    Thanks, it works now.

  13. #11
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: String[] to String

    You can also use Apache Lang library:
    org.apache.commons.lang.StringUtils:
    method join.
    So:
    StringUtils.join(array, "");
    does the job.

  14. The Following User Says Thank You to codesmuggler For This Useful Post:

    javapenguin (February 19th, 2011)

Similar Threads

  1. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  2. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  3. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 09:03 AM
  4. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 11:16 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM