PDA

View Full Version : Starting java; script problems



Infantry001
08-15-2007, 05:41 PM
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
08-15-2007, 07:12 PM
What error are you getting? I see nothing wrong with it.

Pwnd
08-15-2007, 07:56 PM
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
08-15-2007, 08:45 PM
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

GoF
08-16-2007, 12:14 PM
Lol @ sean greasley :rolleyes:

Hm, I can't see anything wrong with it.. and no errors for me either.

Killerdou
08-16-2007, 12:21 PM
try using eclipse, its really usefull:)

Infantry001
08-16-2007, 02:30 PM
Im using netbeans. Maybe i should try running it w/ command prompt?

GoF
08-16-2007, 03:33 PM
Hm, try that. I suggest eclipse though, pwns netbeans anytime anywhere.

R0b0t1
08-17-2007, 06:10 PM
Meh? Whats the difference between the two?

Anyway, I didn't see any errors... But thats me, the newb.

Pwnd
08-17-2007, 10:05 PM
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
09-13-2007, 08:28 AM
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
09-13-2007, 02:24 PM
thanks :D

Nah, its no problem, no one posts in these forums anyways lol

Yakman
09-13-2007, 03:38 PM
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
09-14-2007, 08:20 AM
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?
09-14-2007, 08:58 AM
Im using netbeans. Maybe i should try running it w/ command prompt?

Netbeans rocks.


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
09-14-2007, 09:04 AM
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



for x := 0 to Length(StudentAge)-1 do
begin
end;


in SCAR

Wizzup?
09-14-2007, 12:59 PM
Sorry for double post!!

n3ss3s
09-14-2007, 02:07 PM
My bad pl0x