inputmismatchexception

InputMismatchException when using Sacnner nextLine for String [duplicate]

别说谁变了你拦得住时间么 提交于 2020-06-13 05:55:11
问题 This question already has answers here : Scanner is skipping nextLine() after using next() or nextFoo()? (19 answers) Closed 3 years ago . This is my code import java.io.*; import java.util.*; class student { String name; int age; float cgpa; } public class getdata { public static void main(String args[]) throws IOException { Scanner in=new Scanner(System.in); int n; n=in.nextInt(); student[] s=new student[n]; for(int i=0;i<n;i++) { try { s[i]=new student(); s[i].name=in.nextLine(); in

why is it giving me an java.util.InputMismatchException when i run this code when I use a text file with the text (shown underneath)

自闭症网瘾萝莉.ら 提交于 2020-03-24 00:19:38
问题 import java.util.*; public void readToolData(String fileName) throws FileNotFoundException { File dataFile = new File(fileName); Scanner scanner = new Scanner(dataFile); scanner.useDelimiter(","); while( scanner.hasNext() ) { String toolName = scanner.next(); String itemCode = scanner.next(); int timesBorrowed = scanner.nextInt(); boolean onLoan = scanner.nextBoolean(); int cost = scanner.nextInt(); int weight = scanner.nextInt(); storeTool(new Tool(toolName, itemCode, timesBorrowed, onLoan,

Why get i InputMismatchException when i use scanner sc.nextDouble()

穿精又带淫゛_ 提交于 2019-12-31 04:36:13
问题 I'd like to read data from a txt file, but I get InputMismatchException when I call nextDouble() method. Even though I am using the useLocale method, but it doesn't work. The txt file first line is: 1;forname;1.9 public class SimpleFileReader { public static void main(String[] args){ readFromFile(); } public static void readFromFile(){ try { int x = 0; File file = new File("read.txt"); Scanner sc = new Scanner(file).useDelimiter(";|\\n"); sc.useLocale(Locale.FRENCH); while (sc.hasNext()){

Trying to prompt the user to re-enter from the catch block, but the catch block terminates?

好久不见. 提交于 2019-12-25 00:53:11
问题 I am trying to write a program to ask a user to enter their age and prompt them to re-enter if they enter an improper value (such as a negative number, older than 120, an age with special characters or letters, an out of range number etc...) I tried writing a try/catch to ask the user to re-enter their age: System.out.println("Enter your age (a positive integer): "); int num; try { num = in.nextInt(); while (num < 0 || num > 120) { System.out.println("Bad age. Re-enter your age (a positive

Q : Doing multiple loops and multiple if-statements and if-else-statements || RENTAL CAR CALCULATOR PROJECT

ぃ、小莉子 提交于 2019-12-24 10:18:37
问题 my instructions on the project were as followed: Instructions: Use a sentinel value loop. To create a basic Rental Car Calculator Ask each user for: Type of vehicle (May use something other than strings, such as: 1 for an economy, 2 for a sedan, etc.) Days rented Calculate the (For each customer): Rental cost, Taxes, Total Due. There are three different rental options with separate rates: Economy @ 31.76, sedan @ 40.32, SUV @ 47.56. [Note: only whole day units to be considered (no hourly

Scanner double value - InputMismatchException

為{幸葍}努か 提交于 2019-12-16 20:05:12
问题 I tried use scanner at easiest way: Code: double gas, efficiency, distance, cost; Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of gallons of gas in the tank: "); gas = scanner.nextDouble(); System.out.print("Enter the fuel efficiency: "); efficiency = scanner.nextDouble(); But after first input 5.1 it throws: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530)

Input mismatch error

断了今生、忘了曾经 提交于 2019-12-14 03:59:33
问题 So, I have this little program here and I've been getting the input mismatch error. I'll post the error message below the code. To be honest, I have 0 clue why the error appears. I've checked that all the required variables have correct type assigned. If I understand correctly, the said error happens when you try to input something that is not expected, right? ( double instead of int or whatever). I probably did some stupid mistake somewhere that I cant see for some reason, so I'd appreciate

java.util.InputMismatchException by reading a double

独自空忆成欢 提交于 2019-12-13 07:12:30
问题 I got the an java.util.InputMismatchException when I try to read 0.6 here is a part of the code. As you can see i try to reimplement a SkipList for a exercise sheet. public static void main(String[] args) { Scanner scan = new Scanner(System.in); double f = scan.nextDouble(); impl_with_errors list = new impl_with_errors(f); int n = scan.nextInt(); public class impl_with_errors { public static double chance; public Node list0; public Node list1; public Node list2; public Node list3; /** * the

Reading from file double value using Scanner - InputMismatchException?

走远了吗. 提交于 2019-12-13 04:31:16
问题 I tried read from file double values and using Scanner with this aim. It throws InputMismatchException : "input.txt" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextDouble(Scanner.java:2456) And I can't understand why this happen? Code: public class Largest { public static void main(String[] args) throws FileNotFoundException { String filename = "input.txt"; Scanner in = new Scanner(filename

Exception Handling for no user input in Java

别说谁变了你拦得住时间么 提交于 2019-12-12 00:48:48
问题 I am trying to get my program to exception handle for if the user inputs nothing so they will get an error message of "Error, enter a dollar amount greater than 0" or "Error, Enter a 1, 2 or 3". As of now, the program does nothing if the user just hits "enter" with no input.... import java.util.Scanner; import java.util.*; import java.text.DecimalFormat; public class Candleline { public static void main(String[] args) { //initiate scanner Scanner input = new Scanner(System.in); System.out