PDA

View Full Version : Java Help-Problem


Kura_kai
05-15-2005, 02:16 AM
I am having a problem with this java program i am making. So far it is suppose to just get a command and breakit down into a list where each part is a word so it is divided by spaces. But i am having problems so far this is what i have.

import java.io.*;

public class Command
{
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private static String uR,con,sub1;
private static[] String test;
private static int a,b;

public static void main(String[] args)
throws Exception
{
while(true)
{
b= -1;
File data = new File("C:/");
System.out.print(data+">");
uR = in.readLine();
System.out.println(uR);
if(uR.equals("exit"))
{
System.exit(0);
}
}
}
}

All i have to figure out is how to break uR up and into test. Any help?

Spookster
05-15-2005, 04:42 AM
I would suggest looking up how the string tokenizer class works. You can use the string tokenizer to parse a string into words.

http://java.sun.com/j2se/1.3/docs/api/java/util/StringTokenizer.html

jkd
05-15-2005, 06:33 AM
A friend of mine points out you could just use .split(" ") on the string. Alternatively, .split("\\s+") might be better if there are several spaces or tab characters you would like to split on.

Pieterman
05-21-2005, 11:26 AM
Use this it might help.

import java.io.*;
import java.lang.String.*;
public class StringSplit
{
public StringSplit ()
{
doit ();
}
public void doit ()
{
try
{
BufferedReader in = new BufferedReader (new FileReader ("new.txt"));
String read = in.readLine ();
String [] temp = null;
temp = read.split (" ");
for (int i = 0 ; i < temp.length ; i++)
{
System.out.println (temp [i]);
}
}
catch (Exception e)
{
}
}
public static void main (String args []) throws Exception
{
new StringSplit ();
}
}

suryad
05-23-2005, 09:37 AM
I agree with Spookster in using the StringTokenizer class though the split way could be used as well. Interesting to see from the link you posted Spookster you are still using JDK 1.3??

Spookster
05-23-2005, 01:37 PM
Interesting to see from the link you posted Spookster you are still using JDK 1.3??

That was me being too lazy to dig up a lnk to latest version. :o