The constructor Scanner is undefined [duplicate] - java

This question already has answers here:
How to get the user input in Java?
(29 answers)
Closed 5 years ago.
I'm very very new to Java. I got stuck in this error where it states:
The constructor Scanner() is undefined
and
The method nextInt(int) in the type Scanner is not applicable for the arguments (InputStream).
import java.util.Random;
import java.util.Scanner;
public class NumberGenerator
{
public static void main(String[] args)
{
Scanner input = new Scanner();
Random randomNumber = new Random();
System.out.println("Please enter the maximum value: ");
int maxValue = input.nextInt(System.in);
for (int counter = 1; counter <= 1; counter++)
{
int number = randomNumber.nextInt(maxValue);
System.out.println("Your random number is: " + number);
}
}
}
As you may be able to see, I'm very new and I really appreciate your help.

You need to specify what the scanner is supposed to read from. I assume you want it to read from the console, in which case you would want to write:
Scanner input = new Scanner(System.in);
Also, nextInt() does not take parameters. Change it to:
int maxValue = input.nextInt();

The answer to both of your problems is here https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html. The Scanner class has only constructors that require arguments and the nextInt method either takes no argument or an int.
Advice: Googling " javadoc" is a good habit.

Related

Java scanner.nextDouble() gives an exception [duplicate]

This question already has answers here:
nextDouble() throws an InputMismatchException when I enter a double
(2 answers)
Closed 5 months ago.
I am learning Java and i have met some problems with scanner.nextDouble and I can`t find any response for me.
import java.util.Scanner;
public class Hypotenuse {
public static void main(String[] args){
double a;
double b;
double c;
Scanner scanner = new Scanner(System.in);
System.out.println("Type first side: ");
a = scanner.nextDouble();
System.out.println("Type second side: ");
b = scanner.nextDouble();
c = Math.sqrt((a*a) + (b*b));
System.out.println("The c side is: " + c);
scanner.close();
}
}
The problem is when I`m trying to type number with dot like 1.2 for example which is double type. The exception code is:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:943)
at java.base/java.util.Scanner.next(Scanner.java:1598)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2569)
at Hypotenuse.main(Hypotenuse.java:10)
How do I can fix it ? Thanks for help
It depends on your system. But if you really want to use the dot, you can change the Locale to make a Scanner read dots in this way:
new Scanner(System.in).useLocale(Locale.US);
For example:
Scanner scanner = new Scanner(System.in).useLocale(Local.US); will
use the dot
Scanner scanner = new Scanner(System.in).useLocale(Local.ITALY); will use the comma.

Programs continually prints rather than waiting for input [duplicate]

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 5 years ago.
When I run this, int x = 0 and 1 is added to x to each time it runs. The program keeps on printing out "Round 1", not allowing any time in between to input anything. This is not about reading input, it's about being able to input something without the program going crazy.
while (!broken) {
int pointcount = 0;
System.out.println("Round " + x);
Scanner jumpOption = new Scanner(System.in);
if (jumpOption.equals("W")) {
pointcount += 1;
Random rand = new Random();
if (rand.nextInt(100) == 0) {
broken = true;
You're creating a Scanner object, but not using it for anything. To get any input from the Scanner you have to use one of its methods, like Scanner.nextLine()
Scanner myScanner = new Scanner(System.in);
// Here's where we get the actual input!
if (myScanner.nextLine().equals("Hello")
You have to actually read from the scanner with the next() method
if (jumpOption.next().equals("W")) {

Whats the difference between two multiple user input programs? [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
import java.util.Scanner;
public class HelloWorld{
public static void main(String args[]){
Scanner dd = new Scanner(System.in);
System.out.println("Enter name");
String b = dd.nextLine();
System.out.println("Enter num");
int num = dd.nextInt();
}
}
And
import java.util.Scanner;
public class HelloWorld{
public static void main(String args[]){
Scanner dd = new Scanner(System.in);
System.out.println("Enter num");
int num = dd.nextInt();
System.out.println("Enter name");
String b = dd.nextLine();
}
}
Why the latter doesn't work peoperly(doesn't let me enter name), while the first one does?
I have made up a new version without that annoying "Scanner scan = new Scanner".
And what about this solution? What cons may it have?
import java.util.Scanner;
public class HelloWorld{
public static void main(String args[]){
System.out.println("Enter num");
int i = new Scanner(System.in).nextInt();
System.out.println("Enter name");
String b = new Scanner(System.in).nextLine();
}
}
The second program expects an Int first and then a name. And hence might be erroring out when a name is entered.
In the second case, nextInt() does not scan over the newline character that the user inputs when pressing the Return key.
In the first case, nextLine() is encountered first so the problem doesn't manifest itself.
The moral of the story is to always use nextLine() and parse the resulting string accordingly. Use something like Integer#parseInt to convert a string to an integer.

Java String automatically assigned null value [duplicate]

This question already has answers here:
Java calculator not executing if-statement [duplicate]
(3 answers)
Closed 9 years ago.
In the code below, in the first iteration of the first for loop, boxes[a] is automatically assigned a null value.
The remainder of the iterations are fine (user input is accepted). Only the first has the issue where a null value is automatically assigned.
Does anyone know why this may be? Thank you.
package testing;
import java.util.Scanner;
public class Testing {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Scanner in2 = new Scanner(System.in);
int boxNumber;
boxNumber = in.nextInt();
String[] boxes = new String[boxNumber];
System.out.println(boxNumber);
for(int a=0; a <= boxes.length - 1; a++){
boxes[a] = in.nextLine();
System.out.println(boxes[a]);
}
int packageNumber;
packageNumber = in2.nextInt();
String[] packages = new String[packageNumber];
System.out.println(packageNumber);
for(int n=0; n <= packageNumber - 1; n++){
packages[n] = in.nextLine();
System.out.println(packages[n]);
}
}
}
The scenario fitting the description of what occurs is when you type in a number on the first line, then the rest of the lines are strings for the boxes.
But the nextInt() method doesn't advance past the first newline character, so the first time you call nextLine(), it matches on the rest of the line until the first newline character, "" ( not null).
After the call to nextInt(), insert a call to newLine() before the for loop to bypass the first newline character.
String firstNewLine = in.nextLine();
for(int a=0; a <= boxes.length - 1; a++){
when you did hit enter after entring the first number you also have and empty line that's why nextLine() return empty string, you can use this boxNumber = in2.nextInt(); instead but I would suggest to think of another way, normally you don't need two Scanner instances

int not initialized compilation error [duplicate]

This question already has answers here:
Java: Why am I required to initialize a primitive local variable?
(5 answers)
Closed 9 years ago.
Ok, so I searched for a similar thread like this one but couldn't find the answer I'm looking for.
I'm trying to program something that generates random numbers and turns them into a question.
I'm not even sure it's even properly written but I'm having a compilation error.
"Variable answer1 might not have been initialized"
Here's the code :
import java.util.Scanner;
import java.util.Random;
public class random{
public static void main (String [] args){
System.out.println("Random number generated");
Random obj= new Random();
Scanner scan = new Scanner(System.in);
int answer1;
int rgen= obj.nextInt(100);
int rgen1= obj.nextInt(1000);
System.out.println(rgen + " + " + rgen1 + " = ? ");
scan.nextInt();
if (answer1 == rgen + rgen1)
System.out.println("Correct");
else
System.out.println("Wrong");
}
}
Define answer1 to be some initial value.
int answer1 = 0;
You can't use a variable that you haven't initialized, which is what you attempt to do with if (answer1 == rgen + rgen1).
It'd likely be that you want to read in the next integer, so you could also do this:
int answer1 = scan.nextInt();
Or, before you hit your if block, you can change the statement of scan to put the value into the variable instead:
answer1 = scan.nextInt();
You probably want this:
answer1 = scan.nextInt();
Otherwise, the call of scan.nextInt() reads and discards the value, while answer1 remains uninitialized. You should probably combine initialization with declaration, too: remove
int answer1;
and replace it with
int answer1 = scan.nextInt();
on the line where you read the int from the Scanner.
The code
int answer1;
reserves space on the stack for that variable. It doesn't put anything in there, so whatever is in that variable is whatever was in memory at that location at the time.
It hasn't been initialized because you haven't put anything in there yet. The warning will go away if you change it to
int answer1 = 0;
But honestly I'm not sure what your code is trying to do.

Categories

Resources