The Artima Developer Community
Sponsored Link

Java Answers Forum
String Tokenizer Output

1 reply. Most recent reply: Nov 12, 2002 11:37 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 1 reply on 1 page
Azrul Azhar

Posts: 9
Nickname: bllizard
Registered: Jun, 2002

String Tokenizer Output Posted: Nov 10, 2002 11:51 PM
Reply to this message Reply
Hi,

I would like to ask how to manipulate the token that are taken from string tokenizer. For example :

input = a b 1 f 986

supposed output:
a
b
1
f
986

How to make the output look like this?
Output = (a,b,f),(1,986)

Below is the source code of Algebra.java
___________________________________________________________
import java.io.*;
import java.util.*;

/**
* Read and print, using BufferedReader from System.in, onto System.out
*/
public class Algebra {

public static void main(String[] av) {

try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
System.out.print("Input =");
while ((inputLine = is.readLine()) != null) {
System.out.println("Output="+inputLine);
StringTokenizer st =
new StringTokenizer(inputLine, ", |");

while (st.hasMoreElements())

System.out.println("Token: " + st.nextElement());
break;
}
is.close();
} catch (IOException e) {
System.out.println("IOException: " + e);

}
}
}
------------------------------------------------------------

Thank You


Re: String Tokenizer Output Posted: Nov 12, 2002 11:37 AM
Reply to this message Reply
Posted by: Charles Bell    Posts: 519 / Nickname: charles / Registered: Feb, 2002
import java.io.*;
import java.util.*;
 
/**
* Read and print, using BufferedReader from System.in, onto System.out
*/
public class Algebra {
 
	public static void main(String[] av) {
 
		try {
			BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
			String inputLine = "";
			System.out.print("Input =");
			while ((inputLine = is.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(inputLine);
				int number = st.countTokens();
				String[] array = new String[number];
				int index = 0;
				while (st.hasMoreElements()){
					array[index] = st.nextToken();
					index++;
				}
				System.out.println("Output = (" + array[0] + "," + array[1] + "," + array[3] + "),(" + array[2]  + "," + array[4]  + ")");
				System.out.print("Input =");
			}
			is.close();
		} catch (IOException e) {
			System.out.println("IOException: " + e);
		}
	}
	
 
}
 


Topic: NEWBIE ALERT: Problem with displaying shape on my GUI Previous Topic   Next Topic Topic: MySQL Help!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use