java.util.scanner

how to catch blank input with scanner class in java

前提是你 提交于 2019-12-12 09:43:45
问题 I am using the scanner class to capture user input from the command line (strings only), as an alternative to my previous question. The following seems to work fine, except the blank lines are not caught as they should by the second conditional. For example when I press enter, this should be captured as a blank line and the second conditional should be true. However a new blank line is displayed on the console everytime, with the entire console "scrolling" upward if I keep hitting enter,

Basic Syntax for passing a Scanner object as a Parameter in a Function

倾然丶 夕夏残阳落幕 提交于 2019-12-12 09:08:48
问题 Here is what I wrote which is pretty basic : import java.util.Scanner; public class Projet { /** * @param args * @param Scanner */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Enter a digit"); Scanner in = new Scanner(System.in); getChoice(Scanner); in.close(); } public static int getChoice(Scanner n){ n = in.nextInt(); return n; } } What seems to be wrong here ? I had it working earlier, I had to pass the Scanner type and argument name as a

How to read data from a file and create an object and assign it to an array?

霸气de小男生 提交于 2019-12-12 04:48:53
问题 GeometricObjectsData.txt “CIRCLE”, 1, “blue”, true “RECTANGLE”, 1, 2, “blue”, true “RECTANGLE”, 10, 2, “red”, true “CIRCLE”, 2, “green” “RECTANGLE” “CIRCLE” Driver: I'm a bit confused on how to transfer the above information into an object and then assign it to an array. import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class Driver { public static void main(String[] args) throws FileNotFoundException { Scanner input = new

Scanner(System.in) - infinite loop

℡╲_俬逩灬. 提交于 2019-12-12 04:25:48
问题 Why I'm getting infinite loop in recursion method, without a chance to input any symbol to break it? class Test { int key=0; void meth(){ System.out.println("Enter the number here: "); try(Scanner scan = new Scanner(System.in)) { key = scan.nextInt(); System.out.println(key+1); } catch(Exception e) { System.out.println("Error"); meth(); } } } class Demo { main method { Test t = new Test(); t.meth(); } } If you try to create an error (putting string value in key and then try to add to it a

Scanner is skipping nextLine() after using next() or nextFoo()?

别等时光非礼了梦想. 提交于 2019-12-12 04:13:33
问题 I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this: System.out.println("Enter numerical value"); int option; option = input.nextInt(); // Read numerical value from input System.out.println("Enter 1st string"); String string1 = input.nextLine(); // Read 1st string (this is skipped) System.out.println("Enter 2nd string"); String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value) The problem is that after

Java: How to get Scanner to match the first occurence only and skip if it has been matched before

心不动则不痛 提交于 2019-12-12 04:09:47
问题 I have a list of strings consisting of tokens separated by spaces stored in an ArrayList. I need to scan if the tokens in string 1 are present in string 2. I managed to use Scanner to scan for the tokens. However, the Scanner doesn't do what I wanted. I need suggestions/advice on 1. [EDITED] Example: there's one NN token in String 1 but there's two NN tokens in String 2. So, the Scanner should scan String 2 for NN token. With the code I gave, the Scanner will search for all NN tokens,

Trying to write a method that checks user input in JAVA, getting NoSuchElementException [duplicate]

本小妞迷上赌 提交于 2019-12-12 03:37:58
问题 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) Closed 4 years ago . I am trying to write a method to handle all my input from the user in a console application. I call this method a total of 5 times. The first time, the condition is a) the number must be positive and real-valued (double) the next 4 times the condition is b) the number must be greater than 1 This is my method: private static double

Cannot create a Java JSONArray from String

久未见 提交于 2019-12-12 02:56:34
问题 I get a JSON string from a File like this: "[ { "id": "eVgoHOU0000000011", "network": { "networkName": "EVgo" }, "status": "Available", "plugscore": 9, "stationname": "Wholefoods Market", "description": "Very dependable station next to store.", "location": { "latitude": 30.596481, "longitude": -96.353554, "address": { "address1": "Golden Acres", "address2": "second floor", "city": "College Station", "state": "TX", "zipcode": "77029" }, "description": "Map marker location is next to gas

Non-terminating Java program

大城市里の小女人 提交于 2019-12-12 02:45:28
问题 I have this Java input-related problem. I'm solving some cases in UVAToolkit, but there are some problems that the line input requires from the System.in . Basing from these codes below, how could I terminate the problem once I've pressed key? The sample input/output are displayed below. Scanner scanner = new Scanner(System.in); String line; while((line = scanner.nextLine()) != null) { System.out.println(line); } System.out.println("done"); Sample Input: 1 10 10 100 100 1000 Sample Output: 1

Getting error while using .nextInt()

痞子三分冷 提交于 2019-12-12 02:42:44
问题 I am getting this error: "cannot make a static reference to the nonstatic field" everytime I try to execute a line of code with .nextInt() in it. Here are the lines of code that could affect (that I can think of): private Scanner input = new Scanner(System.in); int priceLocation = input.nextInt(); 回答1: This is most likely because you're trying to access input in a static method, which I'm assuming it to be the main() method. Something like this private Scanner input = new Scanner(System.in);