6th Jan 2024, 7:15 PM
Yazan Alhallack
Yazan Alhallack - avatar
16 Rรฉponses
+ 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.
6th Jan 2024, 10:11 PM
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 1
Wong Hei Ming, nothing is strange. Read my answer to see what's going on, I completely explained.
7th Jan 2024, 6:02 AM
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 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.
7th Jan 2024, 6:18 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 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.
7th Jan 2024, 6:48 AM
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 1
Wong Hei Ming, inputs should be separated using space. Otherwise nextLine() will be meaningless
7th Jan 2024, 7:05 AM
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 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.
7th Jan 2024, 9:22 PM
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 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?
7th Jan 2024, 9:24 PM
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ
๐Ÿ‡ฎ๐Ÿ‡ฑ Radin Masiha ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 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... } }
8th Jan 2024, 2:20 AM
๐€๐ฒ๐ž๐ฌ๐ก๐š ๐๐จ๐จ๐ซ
๐€๐ฒ๐ž๐ฌ๐ก๐š ๐๐จ๐จ๐ซ - avatar
0
Yazan Alhallack , Shouldn't this be one line? Scanner sc = new Scanner(System.in);
6th Jan 2024, 8:28 PM
Rain
Rain - avatar
0
between string and boolean input there should be enter (as end of the string) 1 1.0 1.0 string true
6th Jan 2024, 10:16 PM
zemiak
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.
7th Jan 2024, 1:59 AM
Wong Hei Ming
Wong Hei Ming - avatar
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.
7th Jan 2024, 6:51 AM
Wong Hei Ming
Wong Hei Ming - avatar
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.
7th Jan 2024, 8:10 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
in this case add one more .nextLine() after double input to read CR (end of input)
7th Jan 2024, 8:16 AM
zemiak
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
7th Jan 2024, 4:20 PM
Nassim
Nassim - avatar
0
U did not print them so there is no output
8th Jan 2024, 9:16 AM
Harshita Tyagi
Harshita Tyagi - avatar