• Advertisement
Sign in to follow this  

[java] Beginner Netbeans Problem

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, for some reason, in netbeans..the simple beginner app..doesn't work..
public class Main 
{

  
     public static void main(String[] args) 
     {
        String message;
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter a line of text:");
        
        message = scan.nextLine();
        
        System.out.println("You Entered : \"" + message + "\"");
    }
    
}
this for some reason throws this.. Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1471) at Main.main(Main.java:25) Java Result: 1 Anyone know how to fix this?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
System.util.Scanner#nextLine()
nextLine

public String nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Returns:
the line that was skipped
Throws:
NoSuchElementException - if no line was found
IllegalStateException - if this scanner is closed


Sounds like you have no input!
When you are reading from the keyboard, and you haven't typed anything yet, then there is no input on the input stream. So, nextLine will return NoSuchElementException because it can't find the nextLine of input (it doesn't exist yet).

I've never used the Scanner class, so I don't know the specifics on a fix. You should probably check the "hasNext()" method before trying to read input.

I just use BufferedReaders

import java.io.*;
class MyClass{
static BufferedReader bread = new BufferedReader(new InputStreamReader(System.in));
static String readLine(){
try{
return bread.readLine();
}
catch(IOException e){
return null;
}
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There is nothing wrong with your use of the Scanner class in your code. The problem is a bug in NetBeans.

This is a known bug in Netbeans 4.0 or 4.1 and they have posted a workaround that looks something like this:

# Issue #47708

Description:

Cannot send text input to Ant tasks via the console or the IDE's Output window.

Workaround:

If this occurs, run the Java app in unforked mode by opening build-impl.xml, copying and paste it into build.xml. Next, edit fork="true" to be fork="false". Note that you should ensure that all your files have been saved prior to attempting this because even though normal exceptions should only cause the build to fail cleanly, some critical bugs (e.g. AWT-related deadlocks) in your app could cause the IDE to hang or crash.


What this means, is that you have to modify some build configuration file (build-impl.xml) to allow a component of Netbeans (running Ant) to accept text input (which is what your Scanner class needs when you say System.in).

The above Description was cut and pasted from a Columbia University computer Science website.

Mr. Booka

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This workaround doesn't seem to work. The new build.xml has errors like "set src directory"..etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement