0
Why would not my code run :/ ?
21 Antworten
+ 4
omer ozmen
Scanner scan = new Scanner(System.in);
scan is now your Scanner object.
int age = scan.nextInt();
int money = scan.nextInt();
+ 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 ;
+ 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 ;)
+ 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");
}
}
}
+ 3
Denise Roßberg What is the use of making multiple Scanner object?
+ 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.
+ 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.
+ 3
Denise Roßberg Thanks for your information
+ 2
SOUPTIK NATH i was intend to make it screen the output according to users age and money variables.
+ 2
Remove these two lines from your code
int age=27;
int money=250;
+ 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.
+ 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.
+ 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();
}
}
0
I am not able to understand what are you trying to do.
Scanner is used only for taking input from user.
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();
??
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");
}
}
}
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();
}
}
0
From what I learned. You can only use one scanner.
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.
*/
0
See line no.17 you have added extra semicolon,clean it then compile it