Taking an int out from a string

Hi,I got a scanner reading in a string.eg: 1+2*3I need to read out the numbers out from the string so i can store them into an array. How do i do so? Many thanks.
[190 byte] By [javaPOTATOa] at [2007-11-26 20:20:06]
# 1
I forgot to add, it has no spacing between the operands and operatorsso scanner.nextInt() does not work.
javaPOTATOa at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 2

Try

package testing;

import java.util.Scanner;

public class Test

{

public static void main(String[] args)

{

String s = "10+2*3=16";

Scanner scan = new Scanner(s).useDelimiter("\\D"); // <- \\D for non-digit

while (scan.hasNextInt())

System.out.println(scan.nextInt());

scan.close();

}

}

#

duckbilla at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi, the code doesnt work for the standard input channel. It doesnt display anything.I have a text file and input it in the command prompt like..java Prog <data1.txt>
javaPOTATOa at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 4

> Hi, the code doesnt work for the standard input

> channel. It doesnt display anything.

>

> I have a text file and input it in the command prompt

> like..

> java Prog <data1.txt

Then use the command line arguments (String[] args) instead of the String s. Look at the Scanner class to see if there's a constructor which take a file:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html>

prometheuzza at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 5
My instructions insists i use the scanner class as there's a difference in the format of inputting files between using args[] and scanner class.Is there other ways I can use the delimiter on a string instead of a scanner?
javaPOTATOa at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 6

import java.util.*;

public class StringToken

{

public static void main(String args[])

{

String str="1+2*3";

StringTokenizer stz=new StringTokenizer(str,"+-*/");

String str1=new String[stz.countTokens()];

int i=0;

System.out.println(stz.countTokens());

while(stz.hasMoreTokens())

{

try

{

str1[i]=stz.nextToken();

System.out.println(str1[i]);

i++;

}

catch(Exception e)

{

}

}

}

}

Message was edited by:

qUesT_foR_knOwLeDge

qUesT_foR_knOwLeDgea at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 7

> My instructions insists i use the scanner class as

> there's a difference in the format of inputting files

> between using args[] and scanner class.

>

> Is there other ways I can use the delimiter on a

> string instead of a scanner?

No, you misunderstood. If you dojava Prog data1.txtthen the String array called args will hold the String "data1.txt" at it's first (and only) index. Soargs[0] -> "data1.txt"

.

You can create a Scanner object from the command line arguments:// ...

Scanner scan = new Scanner(new File(args[0]));

// ...

prometheuzza at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 8

Try this for ideas

Scanner scan = new Scanner(new File("filename"));

while (scan.hasNextLine())

{

String[] array = scan.nextLine().split("\\D");

for (int i = 0; i < array.length; ++i)

System.out.print(array[i] + (i == array.length - 1 ? "\n" : " "));

}

scan.close();

#

duckbilla at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 9
Error: cannot find symbolsymbol : class File when i try to use new File
javaPOTATOa at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 10
> Error: cannot find symbol> symbol : class File > when i try to use new Fileimport the java.io.File packageAdd this to the codeimport java.io.*;
qUesT_foR_knOwLeDgea at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 11

ok guys, the delimiter works now.

unfortunately, when i pass this value in 1+2 <2 white spaces> *3-4/2+5*6

It appears to take in as "1+2-3/4+2*5"

Is the white spaces in the equation causing the - and / sign to mix up?

Message was edited by:

javaPOTATO

javaPOTATOa at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 12
There are too many solutions here for me to guess which one you have employed.
qUesT_foR_knOwLeDgea at 2007-7-10 0:44:16 > top of Java-index,Java Essentials,Java Programming...
# 13

Sorry. This is what i did.

public static void cuthemintopieces(String Line){

Scanner token = new Scanner(Line).useDelimiter("\\s");

Scanner operands = new Scanner(Line).useDelimiter("\\D");

Vector<String> aLine = new Vector<String>();

while(token.hasNext()){

String line = token.next();

for(int i=0; i<line.length(); i++){ //to cycle through char by char in each token

char sent = line.charAt(i);

switch(sent){

case '+': ....//add to vectorlist

case '-': .....all the way to case '/'

default: String num = operands.next(); //add numbers>

javaPOTATOa at 2007-7-10 0:44:17 > top of Java-index,Java Essentials,Java Programming...
# 14
Got the solution by placing the scanner operand inside the loop.Thanks all for helping.Will give duckbill the stars because the solving point of my initial question was useDelimiter("\\D")
javaPOTATOa at 2007-7-10 0:44:17 > top of Java-index,Java Essentials,Java Programming...