Re: String of numbers into to array of numbers
bH <bherbst65@hotmail.com> wrote:
My goal is to take a string of numbers separated by commas
then read that string and have the string show the
individual numbers as in an array. This is a small
demo of my problem.
....
import javax.swing.*;
public class Whatsup extends JFrame {
JPanel infoPanel= new JPanel();
int cInt[] = new int [9];
void Whatsup() {
String cStr = "";
cStr = "84,104,101,32,67,108,111,99,10";
System.out.print(" The string is = "+ cStr);
for(int i = 0;i<9;i++) {
//cInt[i] = Integer.getInteger(cStr); //does not work
Did you read the Javadocs for that method?
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#getInteger(java.lang.String)>
Determines the integer value of the system property with the specified name.
You were attempting to find the value of system property named
"84,104,101,32,67,108,111,99,10". I'm guessing there was no such property
defined on your system.
//cInt[i] = Integer.valueOf(cStr);//does not work
Or that one?
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#valueOf(java.lang.String)>
Returns an Integer object holding the value of the specified String.
Note that it returns an 'Integer' object, not an 'int'. Autounboxing does the
rest if the target is an 'int'.
However, if you read the Javadocs you will see why the method cannot determine
the 'Integer' value of "84,104,101,32,67,108,111,99,10".
Steve W. Jackson wrote:
You might start by looking at the split method of String. It will take
that initial String (BTW, no need to declare cStr on one line and
initialize separately) and create from it a String array.
Then you can create a second int array whose size is determined by
inquiring of the length of the String array and populate it using
Integer.parseInt.
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)>
Really, bH, you should seriously consider reading the Javadocs for an API
method before popping it into your code.
--
Lew