java.util.scanner

How to type a string to end a integer Scanning process in Java

天大地大妈咪最大 提交于 2021-02-05 07:59:06
问题 I would like to use Scanner to scan any number of integer then get the average. Before I stop, I would like to type "END". The code below has: Exception in thread "main" java.util.InputMismatchException error. That is because I scan a string rather than int type. How should I solve this problem? Thanks public static int scanaverage() { System.out.println("Enter any number, type 'END' to exit"); Scanner input = new Scanner(System.in); int total=0; int count = 0; while (!(input.nextLine()

Why does input.nextint method have a \n as a leftover?

安稳与你 提交于 2021-02-05 06:43:06
问题 In the answer to this question I don't understand why the input.nextInt has a newline character as a leftover. Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()? 回答1: The answer is quite simple: Scanner#nextInt() reads the next integer but not the newline ( ENTER ) the user presses to submit the integer. To understand this you must know that everything you type will be written to a buffer, from which the Scanner methods try to read from. Regard the following

Why does input.nextint method have a \n as a leftover?

こ雲淡風輕ζ 提交于 2021-02-05 06:43:05
问题 In the answer to this question I don't understand why the input.nextInt has a newline character as a leftover. Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()? 回答1: The answer is quite simple: Scanner#nextInt() reads the next integer but not the newline ( ENTER ) the user presses to submit the integer. To understand this you must know that everything you type will be written to a buffer, from which the Scanner methods try to read from. Regard the following

FileNotFoundException when creating a Scanner in Eclipse with Java

♀尐吖头ヾ 提交于 2021-02-05 05:43:46
问题 I'm getting a FileNotFoundException when running the following Java 6 code on Eclipse (Indigo) on Snow Leopard: import java.io.*; import java.util.*; public class readFile { public static void main(String[] args) { Scanner s = new Scanner(new FileReader("/Users/daniel/pr/java/readFile/myfile.txt")); // Line 9 } } The exception is Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type FileNotFoundException at readFile.main(readFile.java:9) My

Scanner in Java not working

时光总嘲笑我的痴心妄想 提交于 2021-02-04 13:45:20
问题 I'm trying to write a very simple number guessing game (code is below). After 1 round is finished, the user is supposed to be able to decide whether he/she wants to play another round or not. Problem is, the program always skips the last question (never letting the user answer 'y' or otherwise. What am I missing here? Is there something about java.util.Scanner I don't know about? import java.util.Random; import java.util.Scanner; public class GuessNum { public GuessNum() { int numRandom = 0;

Scanner in Java not working

冷暖自知 提交于 2021-02-04 13:44:04
问题 I'm trying to write a very simple number guessing game (code is below). After 1 round is finished, the user is supposed to be able to decide whether he/she wants to play another round or not. Problem is, the program always skips the last question (never letting the user answer 'y' or otherwise. What am I missing here? Is there something about java.util.Scanner I don't know about? import java.util.Random; import java.util.Scanner; public class GuessNum { public GuessNum() { int numRandom = 0;

Cannot use multiple Scanner objects in Java

∥☆過路亽.° 提交于 2021-02-02 09:30:57
问题 I have started learning Java recently. I was learning to take user input using Scanner class when I Started getting an error. Here is the code: Scanner userInput1 = new Scanner(System.in); String name = userInput1.nextLine(); System.out.println("Hi "+ name); userInput1.close(); Scanner userInput2 = new Scanner(System.in); int age = userInput2.nextInt(); System.out.println(age); I get the following error when I enter "Deadboy" as input: Hi Deadboy Exception in thread "main" java.util

Cannot use multiple Scanner objects in Java

荒凉一梦 提交于 2021-02-02 09:30:53
问题 I have started learning Java recently. I was learning to take user input using Scanner class when I Started getting an error. Here is the code: Scanner userInput1 = new Scanner(System.in); String name = userInput1.nextLine(); System.out.println("Hi "+ name); userInput1.close(); Scanner userInput2 = new Scanner(System.in); int age = userInput2.nextInt(); System.out.println(age); I get the following error when I enter "Deadboy" as input: Hi Deadboy Exception in thread "main" java.util

Cannot use multiple Scanner objects in Java

こ雲淡風輕ζ 提交于 2021-02-02 09:29:27
问题 I have started learning Java recently. I was learning to take user input using Scanner class when I Started getting an error. Here is the code: Scanner userInput1 = new Scanner(System.in); String name = userInput1.nextLine(); System.out.println("Hi "+ name); userInput1.close(); Scanner userInput2 = new Scanner(System.in); int age = userInput2.nextInt(); System.out.println(age); I get the following error when I enter "Deadboy" as input: Hi Deadboy Exception in thread "main" java.util

Accessing Scanner objects from subclass in a superclass?

怎甘沉沦 提交于 2021-01-28 22:26:00
问题 I am a very new, relatively inexperienced Java programmer. The project I am working is just a test of my current skills, and my goal is to write as efficient a program as possible. In essence, I have three classes: A, B, and C. B extends A and C extends B, but I want a Scanner object in C to be used in a switch statement (part of a larger method) in A. The reason I want this is because I do not want to overload the method in A (copy-pasting the same code with different parameters is not ideal