6 Replies - 1288 Views - Last Post: 25 March 2012 - 06:25 PM Rate Topic: -----

#1 thePhantom  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 63
  • Joined: 28-April 10

Using string.split to split string, but print string on same line

Posted 25 March 2012 - 01:38 PM

Hey

So I read in a text file, for example:
	3 apples pears
	1 cats dogs
	4 bricks stones
	2 men women



But and when I use string.split:
        	String []array=strLine.split(" ");
        	for(String s: array)
        	{
        		System.out.println(s);
        	}



It prints out the strings one underneath each other, but I actually want it to print each whole row of strings on a new line, because I want to use:
        Arrays.sort(array);
       for (String s: array)
       {
    	   System.out.print(s);
       }


to print each string in ascending order, i.e. :
        1 cats dogs
	2 men women
	3 apples pears
	4 bricks stones



Any suggestions?

This post has been edited by thePhantom: 25 March 2012 - 01:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using string.split to split string, but print string on same line

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using string.split to split string, but print string on same line

Posted 25 March 2012 - 01:41 PM

use System.out.print() instead of System.out.println() :)
Was This Post Helpful? 0
  • +
  • -

#3 thePhantom  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 63
  • Joined: 28-April 10

Re: Using string.split to split string, but print string on same line

Posted 25 March 2012 - 01:51 PM

I did try that, but then it prints everything on one line without spaces, i.e.:

1catsdogs2menwomen3applespears4bricksstones



:/

Also, some of the words aren't alphabetically, but I don't want to sort that out... The strings must only be re-arranged based on their key/int values

This post has been edited by thePhantom: 25 March 2012 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using string.split to split string, but print string on same line

Posted 25 March 2012 - 01:59 PM

You will have to think a bit by yourself

System.out.print(s + " ");
Was This Post Helpful? 2
  • +
  • -

#5 thePhantom  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 63
  • Joined: 28-April 10

Re: Using string.split to split string, but print string on same line

Posted 25 March 2012 - 02:28 PM

Ok, so I managed to come up with:
        	String []array=strLine.split("\n");
        	for(String s: array)
        	{
        		System.out.println(s);
        	}


...which seems to work. Any hints on how I could sort the ints (with attached strings) in ascending order, without changing the order of the strings :)?

I am actually required to use a BST, and although I've read through some tutorials, I still can't seem to understand the application of it...

This post has been edited by thePhantom: 25 March 2012 - 02:31 PM

Was This Post Helpful? -1
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Using string.split to split string, but print string on same line

Posted 25 March 2012 - 02:55 PM

Read the file into a List<String> then use a Comparator to sort it based on the first int

http://technojeeves....ava-7-and-above

This post has been edited by g00se: 25 March 2012 - 02:55 PM

Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using string.split to split string, but print string on same line

Posted 25 March 2012 - 06:25 PM

View PostthePhantom, on 25 March 2012 - 05:28 PM, said:

Ok, so I managed to come up with:
        	String []array=strLine.split("\n");
        	for(String s: array)
        	{
        		System.out.println(s);
        	}


...which seems to work. Any hints on how I could sort the ints (with attached strings) in ascending order, without changing the order of the strings :)?

I am actually required to use a BST, and although I've read through some tutorials, I still can't seem to understand the application of it...

What the heck related to your previous post ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1