aspose file tools*
The moose likes Beginning Java and the fly likes Brand Newbie having Scanner Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Brand Newbie having Scanner Problem" Watch "Brand Newbie having Scanner Problem" New topic
Author

Brand Newbie having Scanner Problem

Matt Edmondson
Greenhorn

Joined: Dec 02, 2004
Posts: 4
This is the first time I've been here, saw the url in Java 2 for dummies, I plan on spending A LOT of time here

I'm just copying the programs line from line from Java 2 for dummies (using the netbeans ide i downloaded from Sun's site) and having no problems until I get to the scanner class. I've tried 2 diffrent programs and it always gives me the same error.

Here is the code (Short, I promise, its only the first part)

package javaapplication2;

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;

class guessinggame {

public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);


out.print("Enter an int from 1 to 10: ");

int inputNumber = myScanner.nextInt();
int randomNumber = new Random().nextInt(10) + 1;

if (inputNumber == randomNumber) {


And here is the error

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\MEDMONDS\JavaApplication2\build\classes
compile-single:
run-single:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at javaapplication2.guessinggame.main(guessinggame.java:26)
Enter an int from 1 to 10:
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

and it highlights my <<<int inputNumber = myScanner.nextInt();>>> line red.

Sorry for the long-windedness, any ideas or help would be greatly appreciated.

Thank you for your time,

Matt
Matt Edmondson
Greenhorn

Joined: Dec 02, 2004
Posts: 4
Quick update, the exact same code, cut and paste, runs fine on JCreator, but still will not work on netbeans, I like the netbeans IDE, and would like to figure out how to make it work.

Thanks,

Matt
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Take a look at this line of code for a minute:

Notice that myScanner is created from an object referenced by System.in. This object basically wraps standard input (stdin) from a console. When you run such a program in a GUI IDE, the behavior differs between IDE's. I'm not familiar with JCreator, but I use NetBeans under Linux regularly. However, I don't think I've tried to run an application in NetBeans that takes input from stdin.

I suspect that NetBeans might not let you type any input for stdin as this program expects. That's why you are getting this error. Unfortunately, I'm not at my own machine to test this hypothesis myself. My only suggestion is that you run this program from the command line. Unfortunately, there is a little bit of work to get things to run smoothly this way. You will need to set your system's PATH variable in order to locate the javac and java programs needed to compile and run your program. You may also need to set the CLASSPATH, too.

At the moment, I can't help you much with this last little bit since I don't have enough information yet. What operating system are you using? Also, what is the full path for your JDK installation? Answering these questions will help us be able to help you along your way.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
saw the url in Java 2 for dummies

Thank you Barry Burd!

You will need to set your system's PATH ... and ... may also need to set the CLASSPATH

For help on setting these values, take a look at our FAQ on How To Set The Classpath. You also might like to take a look at the first couple of lessons of Sun's Java Tutorial. Of course, I suspect that Barry's book covers these topics.

If you're dead set on doing things only through NetBeans, and you'd like further help on figuring out getting NetBeans to cooperate, I recommend moseying on over to the IDEs and Other Tools forum and starting a new thread.
[ December 02, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Matt Edmondson
Greenhorn

Joined: Dec 02, 2004
Posts: 4
Thanks so much for all help and adivce. I tried using netbeans both from home (xp pro) and from work (win 2000 pro). both times, same results.

I'm not NEARLY experianced enough to say if the netbeans IDE is good or bad, if you have an idea for a better freeware IDE, id love to hear it, for right now, I'll try setting my paths and seeing if that works, if it doesnt, I guess it's back to jcreator.

Thanks agian for your time,

Matt
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Matt,

I think NetBeans is OK, also maybe take a look at Eclipse. However, if you are a complete newbie maybe you should consider just using a text editor and the command prompt for a little while so you can concentrate on the Java rather than the IDE.


SCJP 1.4, www.gsi3d.org.uk
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I think it's a good thing to know how things work without a fancy IDE. I also think an IDE like NetBeans can be a useful tool when learning and developing. As you've discovered, sometimes getting the fancy tool to do what you want or expect doesn't work out. So, learn the basics. Learn how things work and about what's happening when using the fancy tool.
Matt Edmondson
Greenhorn

Joined: Dec 02, 2004
Posts: 4
I went back to JCreator for now, one because it is so plain, and 2 because it's what my book Java 2 for dummies is using. I also just ordered (from Amazon through this site) Head First Java due to the positive review on this site.

Thanks again for all this great advice, this page has me more confident then ever that I can learn Java if I stick with it.

Matt
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I used JCreator for the first few months that I learned Java programming.
 
Don't get me started about those stupid light bulbs.
 
subject: Brand Newbie having Scanner Problem