Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Turning String into int

Mike Brooks
Greenhorn
Posts: 21
I'm trying to have 4 integer values that will be x1, x2, y1, y2 that I will later on use to calculate distance. They will be read in from the user as a single String and then convert into a numerical value using a wrapper class. The problem i am having is actually getting them converted into int. So far, with using jbuilder....

String sentence;
int x1, x2, y1, y2;

System.out.print("Enter the first x and y coordinate and then the second " + "x and y coordinate, with a space between each entry: ");
entence = scan.nextLine();

// System.out.println(sentence.indexOf('0'));

//*This is where I get stumped from, I know the below code
is not right, but here is where I need help. From the java help I found out this so far and thought it would take in the index of the entered string and turn it into an int, but it not.
*//
x1 = Integer.parseInt(sentence.indexOf(2));

Keith Lynn
Ranch Hand
Posts: 2409
Instead of using indexOf which tell you where that character is in the String, you should use the split method to break the String into tokens.

Mike Brooks
Greenhorn
Posts: 21
Ok, got somewhere but still don't understand it.

String[] temp = sentence.split(" ");
x1 = Integer.parseInt(temp[0]);
y1 = Integer.parseInt(temp[1]);
x2 = Integer.parseInt(temp[2]);
y2 = Integer.parseInt(temp[3]);

I understand that it breaks it into parts by the space, but what does temp, mean?

Right now im just using the java help built in jbuilder, which is alright. But where can I go to actually get info on code, like what each step actually does for something like "String[] temp = sentence.split(" ");" and so on.
[ March 08, 2006: Message edited by: Jon Martin ]

Martin Mathis
Ranch Hand
Posts: 45

Follow that link for the javadocs. In this case, temp[] is just the place the results of the split operation (an array of strings) are stored. In this case, split breaks it into pieces by spaces, and each piece is an element in the array.

Garrett Rowe
Ranch Hand
Posts: 1296
temp is just the local variable name for the array of String "parts". It could actually be called anything (tokens, parts, nums). It's a way of identifying which object you're talking about.

You may want to check out the API documentation. Here is a link to the tutorial on Arrays and here is a short tuorital on the String.split() method from another forum I visit from time to time.
[ March 08, 2006: Message edited by: Garrett Rowe ]

Layne Lund
Ranch Hand
Posts: 3061
temp is a local variable just like sentence, x1, x2, etc. in your original code. There is no documentation that will describe what it means since the name was made up for the example you gave. On the other hand, the API documentation WILL describe what the split() method does since this is part of the String class. Similarly, the API docs gives information about the Integer class including a description of the parseInt() method.

So one skill you need to develop as you learn Java is recognizing which names are part of the standard API and which names are variables and classes that you (or some other random programmer) wrote.

Layne