Why would not my code run :/ ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
6th May 2020, 2:19 PM
omer ozmen
omer ozmen - avatar
21 Answers
+ 4
omer ozmen Scanner scan = new Scanner(System.in); scan is now your Scanner object. int age = scan.nextInt(); int money = scan.nextInt();
6th May 2020, 2:54 PM
Denise Roßberg
Denise Roßberg - avatar
+ 8
Hello omer ozmen Sololearn does not support multiple scanner. Remove one and use the other one to get your input. Note also, that here on sololearn you have to enter all input at the beginning. Btw: In line 17 you have a unnecessary ;
6th May 2020, 2:26 PM
Denise Roßberg
Denise Roßberg - avatar
+ 5
SOUPTIK NATH After some research I only could find reasons why you should NOT use more than one Scanner object. All Scanner objects shares the same stream because all instances access the same buffer. If scanner 1 has read the stream and you let scanner 2 run afterwards, it has nothing to read because the buffer is already empty. And there also some security risks. Just avoid it to use more than one scanner ;)
6th May 2020, 6:13 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
SOUPTIK NATH thank you:) Denise Roßberg i was able to edit and correct errors. Thanks a lot to you, too. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int age=scan.nextInt(); int money=scan.nextInt(); if(age<25 && money<250){ System.out.println("no pass"); } else {System.out.println("welcome"); } } }
6th May 2020, 3:09 PM
omer ozmen
omer ozmen - avatar
+ 3
Denise Roßberg What is the use of making multiple Scanner object?
6th May 2020, 2:27 PM
Souptik Nath
Souptik Nath - avatar
+ 3
SOUPTIK NATH I am not sure why you would need more than one scanner object. But java allows it. Just sololearn doesn't. But I can do some research.
6th May 2020, 2:30 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
omer ozmen You have multiple errors. Just check my code: https://code.sololearn.com/c3tC0FJUJW40/?ref=app Btw: You can declare a variable only once. int age = 0; After that just write age. Not int age again.
6th May 2020, 3:07 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Denise Roßberg Thanks for your information
6th May 2020, 6:48 PM
Souptik Nath
Souptik Nath - avatar
+ 2
SOUPTIK NATH i was intend to make it screen the output according to users age and money variables.
6th May 2020, 2:46 PM
omer ozmen
omer ozmen - avatar
+ 2
Remove these two lines from your code int age=27; int money=250;
6th May 2020, 3:04 PM
Souptik Nath
Souptik Nath - avatar
+ 2
SOUPTIK NATH If I have time I can try to create one example where it happens. I am not really sure if ypu get an error message or other weird troubles.
6th May 2020, 7:27 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Denise Roßberg Actually this does not happen cause say I want to take two inputs from user.I use scanner1 to take the first input and the scanner 2 takes the second input.This does not throw any error.I have used this personally.
6th May 2020, 6:54 PM
Souptik Nath
Souptik Nath - avatar
+ 1
Create a method to avoid cluttering the main method. You can do this: import java.util.Scanner; public class Program{ public static void main(String[] args) { validator(); } public static void validator(){ Scanner scan = new Scanner(System.in); int age = scan.nextInt(); int money = scan.nextInt(); if(age<25 && money<250){ System.out.println("no pass"); } else { System.out.println("welcome"); } scan.close(); } }
8th May 2020, 7:05 AM
Evseev Anton
Evseev Anton - avatar
0
I am not able to understand what are you trying to do. Scanner is used only for taking input from user.
6th May 2020, 2:24 PM
Souptik Nath
Souptik Nath - avatar
0
Denise Roßberg Using only one scanner, how would I define the money variable to the Scanner? Would it be like; Scanner ageScan=new Scanner(System.in); age=ageScan.nextInt(); Money=moneyScan.nextInt(); ??
6th May 2020, 2:50 PM
omer ozmen
omer ozmen - avatar
0
Still not working aargh :( import java.util.Scanner; public class Program public static void main(String[] args) { int age= 27; int money= 250; Scanner scan=new Scanner(System.in); int age=scan.nextInt(); int money=scan.nextInt(); if(age<25 && money<250){ System.out.println("no pass"); } else {System.out.println("welcome"); } } }
6th May 2020, 3:02 PM
omer ozmen
omer ozmen - avatar
0
import java. Util. Scanner; public class Program{ public static void main(String[] args) { Scanner scan=new Scanner(System.in); int age=scan.nextInt(); int money=scan.nextInt(); if(age<25 && money<250){ System.out.println("no pass"); } else { System.out.println("welcome"); } scan.close(); } }
7th May 2020, 11:47 AM
Evseev Anton
Evseev Anton - avatar
0
From what I learned. You can only use one scanner.
8th May 2020, 4:17 AM
Epsilon ︻╦̵̵͇̿̿̿̿╤──
Epsilon ︻╦̵̵͇̿̿̿̿╤── - avatar
0
import java.util.Scanner; public class Program{ public static void main(String[] args) { int age; int money; Scanner scan=new Scanner(System.in); age=scan.nextInt(); money=scan.nextInt(); if(age<25 && money<250){ System.out.println("no pass");} else {System.out.println("welcome"); } } } /* 1. Use only one object in the scanner class and use only that to take as many inputs you want. 2. As your main motive is to take input so don't initialize the variables. Pardon me for any mistakes and do let me know if I have done any. */
8th May 2020, 5:52 AM
soop
soop - avatar
0
See line no.17 you have added extra semicolon,clean it then compile it
8th May 2020, 6:42 AM
Onkar Ambuse
Onkar Ambuse - avatar