Getting runtime error [duplicate] - java

This question already has answers here:
NoSuchElementException with Java.Util.Scanner
(10 answers)
Closed 6 years ago.
I am trying to build a very basic program in java to print all the unique characters from the string but I am getting runtime error.
Input - amanda
output -amnd
import java.util.*;
class uniquechars {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("Enter a string:");
String str = inp.nextLine(); // input from user
String res="";
for (int i=0;i<str.length();i++){
int count=0;
for(int j=0;j<res.length();j++){
if(str.charAt(i)==res.charAt(j)){
count++;
}
}
if(count==0){
res = res+str.charAt(i);
}
}
System.out.println("Output string with only unique characters:"+res);
}
}
Error
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at uniquechars.main(Main.java:6)

If you are using any online tool to test your code, be sure to provide input to the program.
My guess is you are forgetting to give the input to the program while running it on an online tool.

It works on codechef.com/ide, you just have to select your programming language from the dropdown list. as shown here.

Related

NoSuchElementException : No line found

I am working on a simple project, I took it from a website that gives some challenge to improve my coding skill in java.
import java.util.Scanner;
public class Test1 {
public void test() {
Scanner in = new Scanner(System.in);
System.out.println("enter something :");
String str = in.nextLine();
StringBuilder sb = new StringBuilder(str);
if (str.isEmpty()) {
System.out.println("you should write something");
}
if(str.length()<=30){
System.out.println("reverse : "+sb.reverse());
}else {
System.out.println("Error");
}
System.out.println("----------------------");
}
public static void main(String[] args) {
Test1 c = new Test1 ();
for (int i = 1; i <= 10 ; i++) {
System.out.println("case number : " +i);
c.test();
}
}
}
case number : 1
enter something : ayoub
reverse : buoya
----------------------
...loop continue ..
My code works like I want in terminal of eclipse, but when I put it into the "code editor" of the web site, this last one gives me a runtime error that says:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Test1.test(Test1.java:10)
at Test1.main(Test1.java:31)
I tried to search on StackOverflow for some solutions but I didn't find it.
You are probably using an online java code editor/compiler which does not have an stdin input.
As stated by arcy, you are probably using an IDE with a built-in console window which allows you to pass standard inputs to your program.
The following online editor will allow you to add inputs.
You should take care of the way you are using the nextLine method of Scanner:
The exception you are getting is the result of the scanner not getting any inputs, as can be seen here. I suggest you refactor your loop on the scanner using the method hasNextLine of Scanner.

Can use Scanner only once [duplicate]

This question already has answers here:
Close a Scanner linked to System.in
(5 answers)
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 5 years ago.
I'm kinda new to java and used to write in Python. So when it comes to getting User Input I'm allways annoyed by how many lines of Code I need to perform such task. So I tried to make my own class that simplifies that process. I wanted to perform something like this:
input("This is written in the console: ")
Inside the Console:
This is written in the console: |
Here is the code so far:
public static String input(String text) {
Scanner scanner = new Scanner(System.in);
System.out.print(text);
String x = scanner.nextLine();
scanner.close();
return x;
}
When I use the class once, everything works just fine, but when I try to use it again, I get an Exception:
public static void main(String[] args) {
input("Input: ");
input("Input 2: ");
}
Output:
Input: blaaa
Input 2: Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at NumberConverter.input(NumberConverter.java:124)
at NumberConverter.main(NumberConverter.java:7)
I really don't know why that keeps happening. Please help me, thanks.
Thanks to "SomeJavaGuy" I finally got it to work:
public class NumberConverter {
private static Scanner scanner = new Scanner(System.in);
public static String input(String text) {
System.out.print(text);
String x = scanner.nextLine();
return x;
}
public static void main(String[] args) {
input("Write your Input: ");
input("Write another Input: ");
scanner.close();
}

How do I solve this error with Scanner class: Exception in thread "main"...? [duplicate]

This question already has an answer here:
java.lang.IllegalStateException: Scanner closed
(1 answer)
Closed 4 years ago.
import java.util.Scanner;
public class Main {
public static void main(String[]args) {
double num = 0;
double counter = 0;
double ncot = 0;
Scanner scan = new Scanner(System.in);
while (!(num == -1)) {
ncot = scan.nextDouble();
if (ncot == -1) {
System.out.println("The average is: " + (double)(num/counter));
}
else {
num = num+ncot;
counter++;
}
}
scan.close();
}
}
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1465)
at java.util.Scanner.nextDouble(Scanner.java:2413)
The Code it seems to be triggering a error when im running it on Ideone.com but when i run in eclipse it's fine however.
This is really puzzling. According to the published source code for the standard versions of Scanner (Java 6 and Java 8), the only way that you can get that exception at that point is if something has already called close() on the Scanner object.
(Note that if you instantiated a Scanner around an input stream that was already closed, or a dummy, or null, you would get a different exception. This particular exception only happens if the Scanner object's private closed flag is true, and that only happens when you call Scanner.close().)
But with the code you have shown us, that is not possible. The Scanner can only be closed after you exit the loop.
I can think of only two explanations:
This isn't the code that you ran on ideone.
The guys have implemented ideone have tweaked the behavior of the standard Scanner class. In a way that is inexplicable ... to me.

NullPointerException in simple console test application [duplicate]

This question already has answers here:
Trying to read from the console in Java
(4 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I just started learning some Java and I made this simple program but I dont know whats wrong with my code.
This is what the error I see when I run:
Exception in thread "main" java.lang.NullPointerException
at treehousejava.Main.main(Main.java:9)
This is my code:
package treehousejava;
import java.io.Console;
public class Main {
#SuppressWarnings("unused")
public static void main (String[] args){
Console console = System.console();
String q = console.readLine("Hi there! what's your name? ");
console.printf("hi there %s ! my name is console! ");
}
}
I suspect you want to use System.in and System.out for this type of learning program. System.console() might not be available on your platform and the call will return null, hence the NullPointException. Standard input and output will always be available.
Scanner scanner = new Scanner(System.in);
System.out.println("Hi there! what's your name? ");
String q = scanner.nextLine();
System.out.printf("hi there %s ! my name is console! ", q);

How do I check what the user wrote in Java? [duplicate]

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 7 years ago.
I'm trying to make it so when the user writes Start the program does something, but I'm unsure of how to what the user actually wrote.
This was my first attempt at it:
import java.util.Scanner;
public class Suhwag {
public static void main (String args[]){
Scanner scanNer = new Scanner (System.in);
System.out.println("Please write \"Start\" to begin.");
String stinky = "Start";
if (stinky == scanNer);
But with this, I got the error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Incompatible operand types String and Scanner
After I saw the error, I tried to convert scanNer to a string as seen here:
import java.util.Scanner;
public class Suhwag {
public static void main (String args[]){
Scanner scanNer = new Scanner (System.in);
System.out.println("Please write \"Start\" to begin.");
String stinky = "Start";
String input = scanNer.nextLine();
if (stinky == scanNer);
But the same error message still appears. Anyone know what I could do to make it work?
You're trying to compare a Scanner object with a String object. First, you could input the string with the following line:
String myString = scanNer.next()
Then, compare it with "Start":
if ( myString.equals( "Start" ) )
{
...
}
You are comparing a String to a Scanner object.
You should use the equals method to compare String's
No need for the semi-colon after the if (see below)
In reference to your last code snippet:
if (stinky.equals(input)){
//do something
}
in the latter code area you said in your if statement:
stinky == scaNer
it should be
stinky.equals(input)
in your if statement, you compared still your scanner with ur stinky
change your if statment to this
if (input.equals(stinky)){<code here>}
your previous code didnt work because you compare a scanner with a string

Categories