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();
//*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));
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 ]
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.
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
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.