java.util.scanner

Java: try(Scanner scan = new Scanner(System.in) { } causing an exception

六月ゝ 毕业季﹏ 提交于 2019-11-27 08:43:56
问题 Using try(Scanner scan = new Scanner(System.in)) { } is causing Exception in thread "main" java.util.NoSuchElementException When I try to debug it says that Variable information not available, source compiled without -g option. and shows the below code public Scanner(InputStream source) { this(new InputStreamReader(source), WHITESPACE_PATTERN); } One of my methods that uses this line: protected String loginName(){ String username; String password; try (Scanner scan = new Scanner(System.in)) {

How to delete a line from a text file with java using scanner

我们两清 提交于 2019-11-27 08:40:16
问题 I have text file called BookingDetails.txt inside the file there's a line of records Abid Akmal 18/11/2013 0122010875 Grooming Zalman 5 125.0 Dog It goes from First name: Abid, Last name: Akmal, Date, phone number, type of services, pet name, days of stay, cost, and type of pet. I want to create a user input function in which when the user enters the first name and the last name, the whole line is deleted. But note that it will only affect that particular line as they will be more booking

Must type multiple times, before scanner reads input

不打扰是莪最后的温柔 提交于 2019-11-27 08:40:15
问题 If I run this code Scanner sc = new Scanner(); while (true) { if (sc.next().equals("1")) System.out.println("--1--"); else if (sc.next().equals("2")) System.out.println("--2--"); else if (sc.next().equals("3")) System.out.println("--3--"); else if (sc.next().equals("4")) System.out.println("--4--"); else if (sc.next().equals("help")) System.out.println("--help--"); } It will not read the first time I type enter. I have to type 2-4 times before it reads the input. A session could look like

Scanner keeps skipping input whist using nextInt() and loops

回眸只為那壹抹淺笑 提交于 2019-11-27 08:37:55
问题 I am using a while loop to make sure that the value entered to a scanner object is an integer as such: while (!capacityCheck) { try { System.out.println("Capacity"); capacity = scan.nextInt(); capacityCheck = true; } catch (InputMismatchException e) { System.out.println("Capacity must be an integer"); } } however, if the user does not enter an integer, when it should go back and take another input it just repeatedly prints "Capacity" followed by the output in the catch without asking for more

How to capture enter key, using scanner as console input?

夙愿已清 提交于 2019-11-27 08:33:01
问题 I want to capture enter character in console. I am inputting 2 strings. Case 1. removestudent(pressing enter) removes all the students from array list. Case 2. removestudent student1 removes students1 from array list. Scanner in=new Scanner(); type_op=in.next(); param=in.next(); if (type_op.equals("removestudent")) { //Calling remove student function and passing param over here. } Now the case 2 works fine. However, for Case 1, I want param value to be null when user presses enter. I will

Scanner, useDelimiter

血红的双手。 提交于 2019-11-27 08:29:18
问题 I encounter some problem when using useDelimiter from the Scanner class. Scanner sc = new Scanner(System.in).useDelimiter("-"); while(sc.hasNext()) { System.out.println(sc.next()); } if I have this input A-B-C the output will be A B and wait until I type in another "-" for it to print out the last character However if I instead of having user input data, and insert a String to the Scanner instead the code will work. What's the reason for it, and how do I fix it? I don't want to use

Java - Scanner - Skips over my last nextLine() request

為{幸葍}努か 提交于 2019-11-27 08:24:14
问题 So I instantiate the Scanner scan a lot earlier but it skips right over my second scan.nextLine() after scan.nextInt() . I don't understand why it skips over it? System.out.println("Something: "); String name = scan.nextLine(); System.out.println("Something?: "); int number = scan.nextInt(); System.out.println("Something?: "); String insurer = scan.nextLine(); System.out.println("Something?: "); String another = scan.nextLine(); 回答1: because when you enter a number int number = scan.nextInt()

How do I use a dot as a delimiter?

最后都变了- 提交于 2019-11-27 08:21:12
问题 import java.util.Scanner; public class Test{ public static void main(String[] args){ Scanner input = new Scanner(System.in); input.useDelimiter("."); String given = input.next(); System.out.println(given); } } When I run the above code and type in asdf. then enter, I get nothing. It works fine with "," ";" "\"" "\\\\" or whatever, but just not with "." ... So is there something about a dot or is it just a problem with Eclipse IDE or whatever? 回答1: Scanner is using regular expression (regex)

How to put a Scanner input into an array… for example a couple of numbers

℡╲_俬逩灬. 提交于 2019-11-27 08:06:21
Scanner scan = new Scanner(System.in); double numbers = scan.nextDouble(); double[] avg =..???? You could try something like this: public static void main (String[] args) { Scanner input = new Scanner(System.in); double[] numbers = new double[5]; for (int i = 0; i < numbers.length; i++) { System.out.println("Please enter number"); numbers[i] = input.nextDouble(); } } It seems pretty basic stuff unless I am misunderstanding you You can get all the doubles with this code: List<Double> numbers = new ArrayList<Double>(); while (scan.hasNextDouble()) { numbers.add(scan.nextDouble()); } Jossel M.

using java.util.Scanner to read a file byte by byte

感情迁移 提交于 2019-11-27 07:52:16
问题 I'm trying to read a one line file character by character using java.util.Scanner. However I'm getting this exception": Exception in thread "main" java.util.InputMismatchException: For input string: "contents of my file" at java.util.Scanner.nextByte(Scanner.java:1861) at java.util.Scanner.nextByte(Scanner.java:1814) at p008.main(p008.java:18) <-- line where I do scanner.nextByte() Here's my code: public static void main(String[] args) throws FileNotFoundException { File source = new File(