+ 1
Please help me..where is the bug?
https://sololearn.com/compiler-playground/c6W9WW6qqc5Z/?ref=app
16 Antworten
+ 3
Well, this may be a bit complex to explain, but I try my best:
Your code is a program that accepts 5 user inputs (required). The first one must be an Integer, second and third one are floating point numbers, fourth one a string, and the last one must be a boolean on a new line! So your input must be something like that (example):
12 22.2 44.5 Hi everyone!
true
If you don't write them all, the Scanner throws NoSuchElementException
If you don't write them in order, your Scanner throws InputMismatchException.
Note that I wrote the boolean type after a line break, that's because nextLine() reads the whole line, and if I wouldn't have used the line break, the boolean (true/false) would be considered as string rather than a boolean type.
Hope I was helpful.
+ 1
Wong Hei Ming, nothing is strange. Read my answer to see what's going on, I completely explained.
+ 1
🇮🇱 Radin Masiha 🇮🇱
Oh... I thought it in Python.
However with the input you provided the string will have a leading space if System.out.println(w); is added.
+ 1
Wong Hei Ming, yes I know, and I don't know why I guess it is Sololearn editor problem, I should test it somewhere out of here, anyway it is not a big problem.
+ 1
Wong Hei Ming, inputs should be separated using space. Otherwise nextLine() will be meaningless
+ 1
Wong Hei Ming, I see some compilers treat different, but most of the times, you should enter all inputs at once when you are using Scanner class.
+ 1
Hi Nassim, I can't open your link, I tried but it seems unavailable. Could you please insert the code using the "plus" sign?
+ 1
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// User input:
int x = sc.nextInt();
float y = sc.nextFloat();
double z = sc.nextDouble();
sc.nextLine(); // Consume the newline character
String w = sc.next(); // Use next() to read the string
boolean c = sc.nextBoolean();
// Rest of your code...
}
}
0
Yazan Alhallack ,
Shouldn't this be one line?
Scanner sc = new
Scanner(System.in);
0
between string and boolean input there should be enter (as end of the string)
1 1.0 1.0 string
true
0
That is strange.
To test the code, I added a System.out.println(c); at the end, and then comment out all scans except Boolean.
I tested it with 4 different inputs, True, true both returns true, and False, false both returns false.
It seems working fine.
And then I changed Boolean c to boolean c, it works the same.
Then I uncomment previously commented code one by one, start from bottom.
With a String, the code works fine.
Then I uncomment double, it gives me an error java.util.InputMismatchException, and the full message suggest it is from Scanner.nextBoolean.
I made further test, I add only one variable and Boolean / boolean (e.g. int + boolean, float + boolean, etc.), and the code runs fine.
If there are 2 or more other variable plus boolean, the code fails again with the same error message.
0
🇮🇱 Radin Masiha 🇮🇱
I don't know but I guess we want a string without leading space.
If we put 5 lines of println for each variable, the normal way to enter inputs in playground should be
42
42.44
42.123456
Hello
true
But it doesn't work for me.
0
🇮🇱 Radin Masiha 🇮🇱
I think in a console environment, we will prompt the user what kind of value is needed to input, thus resulting in follow code.
System.out.println("Please enter an integer value");
int x= sc.nextInt();
System.out.println("Please enter a float value");
float y= sc.nextFloat();
System.out.println("Please enter a double value");
double z= sc.nextDouble();
System.out.println("Please enter a string");
String w= sc.nextLine();
System.out.println("Please enter a boolean value");
Boolean c= sc.nextBoolean();
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(w);
System.out.println(c);
Actually, I don't understand why this input works, since each input is separated by a println().
42 42.22, 42.1234 Hello
true
Maybe it is a playground environment behavior?
I tested in a console environment using openjdk, after entering a double it prints "Please enter a string" and "Please enter a boolean value" follow immediately, not a chance to input a string value.
0
in this case add one more .nextLine() after double input to read CR (end of input)
0
Hi;
This work better : https://www.sololearn.com/en/compiler-playground/cUFPoo0ICzft
Provide those entries as inputs and you'll see the result :
1234
0.965
-2365.898
BlaBlaBla
true
0
U did not print them so there is no output