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.
Related
This question already has answers here:
Scanner double value - InputMismatchException
(2 answers)
Closed 2 years ago.
I'm rather new to Java and I was making a simple calculator. Problem is when I my input number is for example "3.1" it gives an exception error, but when writing "3,1" it works just fine.
My friend, however, has a slightly more advanced calculator (with string parsing) and when I run his code the opposite happens: 3,1 gives exception error, 3.1 works perfectly.
I was looking forward to know what causes these different behaviors.
I made this simple sum just now and the same happens, I'll edit and put his calculator code in a few minutes
import java.util.Scanner;
public class Tutorial_7 {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double num1, num2;
System.out.println("Introduza os dois números");
System.out.println("1º: ");
num1 = scan.nextDouble();
System.out.println("2º: ");
num2 = scan.nextDouble();
System.out.println((num1 + num2));
scan.close();
}
}
Final edit: He does use Double.parseDouble(). Got it, the difference is indeed in where it is localized. Should have looked for it but never heard of this concept before.
Thank you
Because you are using difference Local for that one can scan it with a dot . and another with a comma , to fix it you should to fix one for your Scanner like this :
Scanner scan = new Scanner(System.in).useLocale(Locale.US);
For example:
If you are using Local.US you should to scan your double with a .
like 6.6
If you are using Locale.FRENCH you should to scan your double with
a , like 6,6
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Close a Scanner linked to System.in
(5 answers)
Closed 4 years ago.
I had setup a method to check if the input matches the type int and doesn't return until it does.
It worked fine for a couple of projects so far but when I used it in my current one I got this kind of error when going into the method for the second time around.
Exception in thread "main" java.util.NoSuchElementException
Here is the method:
private static int inputHandler() {
Scanner sc = new Scanner(System.in);
int num = 0;
try{
System.out.println("Enter a number:");
num = sc.nextInt();
} catch(InputMismatchException e){
System.out.println("Input must match type int");
num = inputHandler();
}
sc.close();
return num;
}
If anyone knows how to fix this issue I would appreciate the help.
This question already has answers here:
Using scanner.nextLine() [duplicate]
(5 answers)
Closed 6 years ago.
Working through a HackerRank tutorial, and I was wondering -- is there a better way to strip off the newline character that comes after reading in the double? It feels really manual and "hacky" to just repeat a nextLine()
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String b = scan.nextLine();
String s = scan.nextLine();
}
}
Note: code works as-is, just asking if there is a less hacky way to go about this
Operate on lines at a time, then you don't need to worry about the nextDouble() (or nextInt()) calls leaving a trailing newline. Like,
Scanner scan = new Scanner(System.in);
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();
If, you want to allow the int and double to be on the same line then you could do
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();
But without knowing your input format, that may or not be helpful. I would prefer the version most readable for the problem at hand (parsing the input).
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I am new to programming, so just consider my mistakes. I am writing a program where the user gives two input numbers in one line separated by whitespace. I have to assign the first input to an integer variable and second to a double and have to perform some mathematics and show the result. Following is my code:
import java.util.Scanner;
public class foo{
public static void main(String[] args){
String b = null;
Scanner sc = new Scanner(System.in);
b = sc.next();
String[] split = b.split(" ");
int i = Integer.parseInt(split[0]);
double d = Double.parseDouble(split[1]);
System.out.println(i+20);
System.out.println(d-1.50);
}
}
And following is the error i am getting while running it.
F:\java\work\codechef>javac foo.java
F:\java\work\codechef>java foo
20 300.50
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at foo.main(foo.java:9)
First I tried making it with b=sc.readLine(); but there i got the following error while compiling:
error: cannot find symbol
b = sc.readLine();
^
symbol: method readLine()
location: variable sc of type Scanner
1 error
Why I am getting these errors and how to solve the above problem.
You used sc.next() which returns the next token (a token is something that had delimiter before and/or after) so it contains only the 20 or 300.50 in your case.
You should use sc.nextLine() to use split later on, this will return the full line.
Or use:
int i = Integer.parseInt(sc.next());
double d = Double.parseDouble(sc.next());
And get rid of lines:
b = sc.next();
String[] split = b.split(" ");
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.