java.util.scanner

How to insist that a users input is an int?

孤街醉人 提交于 2019-11-27 03:42:04
问题 Basic problem here.. I will start off by asking that you please not respond with any code, as that likely will only confuse me further (programming noob). I am looking for a clear explanation on how to solve this issue that I'm having. I have a scanner that reads input from the user. The user is prompted to enter an int value between 1 to 150 (whole numbers only). I obtain the value as follows: Scanner scan = new Scanner(System.in); int input = scan.nextInt(); And continue on with my program,

Restrict string input from user to alphabetic and numerical values [duplicate]

元气小坏坏 提交于 2019-11-27 03:38:26
问题 This question already has an answer here: How to use java.util.Scanner to correctly read user input from System.in and act on it? 1 answer Basically, my situation requires me to check to see if the String that is defined by user input from the keyboard is only alphabetical characters in one case and only digits in another case. This is written in Java. my current code: switch (studentMenu) { case 1: // Change all four fields System.out.println("Please enter in a first name: "); String

Java: CSV file read & write

前提是你 提交于 2019-11-27 03:34:26
问题 I'm reading 2 csv files: store_inventory & new_acquisitions . I want to be able to compare the store_inventory csv file with new_acquisitions . 1) If the item names match just update the quantity in store_inventory. 2) If new_acquisitions has a new item that does not exist in store_inventory , then add it to the store_inventory . Here is what i have done so far but its not very good. I added comments where i need to add taks 1 & 2 . Any advice or code to do the above tasks would be great!

Java - Using multiple delimiters in a scanner

爱⌒轻易说出口 提交于 2019-11-27 02:44:39
问题 I'm using a scanner to take input and, hopefully, split it into chunks. I want it to split it up using whole word delimiters. So right now I have: Scanner scanner = new Scanner("1 imported bottle of perfume at 27.99"); scanner.useDelimiter("\\sdelimitOne\\s"); So with input "word word delimitOne word word delimitTwo word word" I get output: word word word word delimitTwo word word I was hoping scanner.useDelimiter("\\sdelimitOne\\s\\sdelimitTwo\\s"); might work, but alas not. How do I go

Java Scanner(File) misbehaving, but Scanner(FIleInputStream) always works with the same file

不羁的心 提交于 2019-11-27 02:41:53
问题 I am having weird behavior with Scanner. It will work with a particular set of files I am using when I use the Scanner(FileInputStream) constructor, but it won't with the Scanner(File) constructor. Case 1: Scanner(File) Scanner s = new Scanner(new File("file")); while(s.hasNextLine()) { System.out.println(s.nextLine()); } Result: no output Case 2: Scanner(FileInputStream) Scanner s = new Scanner(new FileInputStream(new File("file"))); while(s.hasNextLine()) { System.out.println(s.nextLine());

Why is hasNext() False, but hasNextLine() is True?

浪子不回头ぞ 提交于 2019-11-27 02:28:54
问题 Question How is it that for a scanner object the hasNextLine() method returns true while the hasNext() method returns false? Note: Based on the input file, the hasNext() method is returning the result as expected; the hasNextLine() does not seem to be returning the correct result. Code Here's the code I'm running that's creating the results below: public void ScannerTest(Reader fileReaderObject){ Scanner scannerObj = new Scanner(fileReaderObject); for(int i = 1; scannerObj.hasNext(); i++){

Scanner input validation in while loop

杀马特。学长 韩版系。学妹 提交于 2019-11-27 02:14:34
I've got to show Scanner inputs in a while loop: the user has to insert inputs until he writes "quit". So, I've got to validate each input to check if he writes "quit". How can I do that? while (!scanner.nextLine().equals("quit")) { System.out.println("Insert question code:"); String question = scanner.nextLine(); System.out.println("Insert answer code:"); String answer = scanner.nextLine(); service.storeResults(question, answer); // This stores given inputs on db } This doesn't work. How can I validate each user input? Ruchira Gayan Ranaweera The problem is that nextLine() "Advances this

hasNext() - when does it block and why?

狂风中的少年 提交于 2019-11-27 02:11:01
I'm trying to read commands via a Scanner Object. For checking the Input Syntax I use sc.hasNext() (for the case of missing commands). It did work fine for many cases already, but now I have the case that's described in the JavaAPI as "MAY block and wait for Input". When does the hasNext() method block and how can I control it? The funny Thing is that it work's perfectly fine with 3 cases before the block. Also the JavaAPI describes hasNext() as the proper method for checking wether there is another Input or not so that the Method next() doesn't produce an Exception . Here is the code I did

nextDouble() throws an InputMismatchException when I enter a double

倾然丶 夕夏残阳落幕 提交于 2019-11-27 02:10:33
import java.util.*; class Averager { public static double unlimited() { int count = 0; double sum = 0; Scanner scan = new Scanner(System.in); while(scan.hasNext()) { double d = scan.nextDouble(); sum += d; count++; } double ave = sum/count; return ave; } public static void main(String[] args) { System.out.println(unlimited()+"\n"); } } There is no error when I use integers but if I use numbers with point in it a error appears. $ javac Averager.java; java Averager 0.5 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner

Getting User input with Scanner

好久不见. 提交于 2019-11-27 02:10:02
I am trying to have a scanner take input in a loop. Once the user wants to finish he can exit this loop. I have tried many different ways to do it but there is always some problem. This is the code: private void inputEntries() { Scanner sc = new Scanner(System.in); System.out.println("Continue?[Y/N]"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here System.out.println("Enter first name"); String name = sc.nextLine(); System.out.println("Enter surname"); String surname = sc.nextLine(); System.out.println("Enter number"); int number = sc.nextInt(); Student student =