5 Replies - 444 Views - Last Post: 16 November 2012 - 04:47 AM Rate Topic: -----

#1 Scrufeh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-November 12

Java.util.Scanner userDelimiter() Pattern Problem

Posted 14 November 2012 - 09:07 PM



I have a method that is called when my program recieves messages from a server.
It is given a string(the message)

I am attempting to seperate the string with "+" characters in order to transmit more efficient messages.

Scanner scan = new Scanner(stringVar);
scan.useDelimiter(" + ")



I then proceed to print out each seperated string on a new line.

if(scan.hasNext())
{
    do
    {
        System.out.println(scan.Next());
    }
    while(scan.hasNext())
}
else
{
    scan.close();
}



For some reason, either of these processes is broken as the whole input string is printed in one line!

What i want:
Line1
Line2
Line3
Line4

What happens:
Line1 + Line2 + Line3 + Line4


Is there any thing that i am doing wrong?
Also if there is a more efficient way of doing this...

Is This A Good Question/Topic? 0
  • +

Replies To: Java.util.Scanner userDelimiter() Pattern Problem

#2 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Java.util.Scanner userDelimiter() Pattern Problem

Posted 14 November 2012 - 10:16 PM

Can you show us the whole example?
Was This Post Helpful? 0
  • +
  • -

#3 Scrufeh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-November 12

Re: Java.util.Scanner userDelimiter() Pattern Problem

Posted 14 November 2012 - 11:28 PM

View PostWolverineX, on 14 November 2012 - 10:16 PM, said:

Can you show us the whole example?


Sure

import java.util.Scanner;

public class incomingFromServerHandler 
{
	public static void actionPerformed(String string) 
	{
		Scanner scan = new Scanner(string);
		scan.useDelimiter(" + ");
		
		if(scan.hasNext())
		{
			do
			{
				System.out.println(scan.next());
			}
			while(scan.hasNext());
		}
		else
		{
			scan.close();
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Java.util.Scanner userDelimiter() Pattern Problem

Posted 15 November 2012 - 08:13 AM

Why not simply using:
string.split("\\+")
or string.split(" \\+ ") if you want the spaces
on the received line ?

Don't forget the \\ because + is a meta character in regex

This post has been edited by pbl: 15 November 2012 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 Scrufeh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-November 12

Re: Java.util.Scanner userDelimiter() Pattern Problem

Posted 15 November 2012 - 05:42 PM

View Postpbl, on 15 November 2012 - 08:13 AM, said:

Why not simply using:
string.split("\\+")
or string.split(" \\+ ") if you want the spaces
on the received line ?

Don't forget the \\ because + is a meta character in regex



Would this split is every time a "+" appears?
Also how would i then access the different sections of the string?

Thanks though
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Java.util.Scanner userDelimiter() Pattern Problem

Posted 16 November 2012 - 04:47 AM

http://docs.oracle.c...va.lang.String)
String[] token = string.split("\\+");
for(int i = 0; i < token.length; ++i) {
   .. access here each token[i] one after the other


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1