java.util.scanner

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

青春壹個敷衍的年華 提交于 2019-11-28 14:49:26
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 then pass param as a null value to my remove function and delete all the students in the array list. list1

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

和自甴很熟 提交于 2019-11-28 14:42:20
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)) { // This line is causing the error. System.out.print("Enter Username: "); username = scan.next();

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

ε祈祈猫儿з 提交于 2019-11-28 14:41:25
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 entry in the text file. This is only a part of my program that I don't know how to do. I'm stuck here

Must type multiple times, before scanner reads input

百般思念 提交于 2019-11-28 14:36:07
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 this: 1 1 1 1 --1-- 3 3 --3-- help 2 1 help --help-- No matter what I type, it will only read the last

Scanner keeps skipping input whist using nextInt() and loops

只谈情不闲聊 提交于 2019-11-28 14:25:14
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 input. How do I stop this? scan.nextLine(); Put this piece of code inside your catch block, to consume

How do I use a dot as a delimiter?

不打扰是莪最后的温柔 提交于 2019-11-28 14:12:14
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? Scanner is using regular expression (regex) as delimiter and dot . in regex is special character which represents any character except line separators.

How to read from standard input line by line?

白昼怎懂夜的黑 提交于 2019-11-28 13:46:11
问题 What's the Scala recipe for reading line by line from the standard input ? Something like the equivalent java code : import java.util.Scanner; public class ScannerTest { public static void main(String args[]) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ System.out.println(sc.nextLine()); } } } 回答1: The most straight-forward looking approach will just use readLine() which is part of Predef . however that is rather ugly as you need to check for eventual null value: object

How to get line number using scanner

最后都变了- 提交于 2019-11-28 13:34:59
I'm using scanner to read a text file line by line but then how to get line number since scanner iterates through each input?My program is something like this: s = new Scanner(new BufferedReader(new FileReader("input.txt"))); while (s.hasNext()) { System.out.print(s.next()); This works fine but for example: 1,2,3 3,4,5 I want to know line number of it which mean 1,2,3 is in line 1 and 3,4,5 is in line 2.How do I get that? You could use a LineNumberReader in place of the BufferedReader to keep track of the line number while the scanner does its thing. LineNumberReader r = new LineNumberReader

Having problems reading from and deleting certain lines from a file?

核能气质少年 提交于 2019-11-28 12:56:54
问题 The point of this program is to remove certain sports teams and their members from a text file, then overwrite the original file with the new set of values. This is attempted by reading the values into an array, then looping through the array and deleting the name of the team and the next 2 lines but for some reason it stops going through the array after the index. I'm stuck, so any help would be great Code: private void RemoveSportsTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {

Scanner method opened and closed twice

人盡茶涼 提交于 2019-11-28 12:50:58
In a single class I have two different methods who are using the scanner class. I created a new instance of scanner object for the first method, then closed it at the end of the first method...then created a new instance of an object (with a different name) in the second method to finally close it at the end of this method. Unless I open the scanner once and close it once it won't work and return an error. The double use of the scanner class doesn't seem to work. What am I doing wrong? Here are my two methods which return an error... public void GameSetup(){ //intro to the program and what the