File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Turning String into int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Turning String into int" Watch "Turning String into int" New topic

Turning String into int

Mike Brooks

Joined: Mar 08, 2006
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

Joined: Feb 07, 2005
Posts: 2398
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

Joined: Mar 08, 2006
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

Joined: Dec 20, 2004
Posts: 45
Pick your version...

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

Joined: Jan 17, 2006
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Turning String into int
It's not a secret anymore!