Infantry001
2007-08-15 13:41:56
I am starting to learn java (from sean greasly :D), and I encountered a problem in this script. Can anyone show me how to fix it?
package helloworld;
import java.util.*;
/**
*
* @author Kevin
*/
public class Arrays {
public static void main(String[] args) {
int[] studentAge = new int[5];
Scanner theInput = new Scanner(System.in);
for (int x = 0; x < studentAge.length; x++) {
System.out.print("Enter age for student " + (x + 1) + ": ");
studentAge[x] = theInput.nextInt();
}
System.out.println("The student ages are: ");
for (int x = 0; x < studentAge.length; x++) {
if (x == studentAge.length) {
System.out.println(studentAge);
} else {
System.out.print(studentAge + ",");
}
}
theInput.close();
}
}
Jason2gs
2007-08-15 15:12:17
What error are you getting? I see nothing wrong with it.
I think he needs a try-catch clause around his Scanner object methods/declaration. I forget if you need it, but I'm pretty sure it can throw an IOException.
Infantry001
2007-08-15 16:45:58
Haha, I didnt either, Jason.
Well, after I start to run it, the script asks me for an input, before anything is appears. Then, if I input anything, it either
a). Terminates and gives a bunch of scanner errors.
b). Continues with the script after 5 inputs, but doesn't allow me to input anything when it asks for the ages.
I probably do need a try catch, but I wouldn't know how to do that :D
Lol @ sean greasley :rolleyes:
Hm, I can't see anything wrong with it.. and no errors for me either.
Killerdou
2007-08-16 08:21:43
try using eclipse, its really usefull:)
Infantry001
2007-08-16 10:30:23
Im using netbeans. Maybe i should try running it w/ command prompt?
Hm, try that. I suggest eclipse though, pwns netbeans anytime anywhere.
R0b0t1
2007-08-17 14:10:54
Meh? Whats the difference between the two?
Anyway, I didn't see any errors… But thats me, the newb.
From: GoF
Hm, try that. I suggest eclipse though, pwns netbeans anytime anywhere.
You should probably know that IDEs are user preferred. I'd say Eclipse can lick my grundle because IntelliJ IDEA owns it, but some people wouldn't think so.
Anyways, try using batch scripts to run it; perhaps you'll get different results.
Miitchyy
2007-09-13 04:28:01
I just use Textpad, I don't like IDE's
If I wanna Jar something I'll do it myself.
Oh, Btw:
if (x == studentAge.length - 1) {
and
studentAge[x]
Since you had studentAge which is an object so print/println would use toString and that would return a hex
EDIT: Oops sorry, I didn't see that this topic was old
Infantry001
2007-09-13 10:24:41
thanks :D
Nah, its no problem, no one posts in these forums anyways lol
Yakman
2007-09-13 11:38:58
replace new Scanner(System.in) with this
in = new BufferedReader(new InputStreamReader(System.in));
then call
in.readLine() to get the user to type in something and press enter
n3ss3s
2007-09-14 04:20:20
I also noticed a little thing in your for loop?
for (int x = 0; x < studentAge.length; x++) {
Im pretty sure you dont want to make x bigger until it is smaller than studentAge.length?
Wizzup?
2007-09-14 04:58:26
From: Infintry001
Im using netbeans. Maybe i should try running it w/ command prompt?
Netbeans rocks.
From: n3ss3s
I also noticed a little thing in your for loop?
Im pretty sure you dont want to make x bigger until it is smaller than studentAge.length?
For (int i = 0; i < 10; i++) does the loop 10 times.
Set/Int Number; WhileCommand, increasecommand
Starblaster100
2007-09-14 05:04:44
From: n3ss3s
I also noticed a little thing in your for loop?
Im pretty sure you dont want to make x bigger until it is smaller than studentAge.length?
Thats the same as saying
[php]
for x := 0 to Length(StudentAge)-1 do
begin
end;
[/php]
in SCAR