java.util.scanner

Using the asterisk-character as a Java Scanner Delimiter

こ雲淡風輕ζ 提交于 2019-12-02 05:55:22
问题 Hey Everyone, This question seems really silly to me, but I can't for the life of me find the answer anywhere. All I'm trying to do, is scan a string that is delimited with an asterisk (* ). However, when I try foo.useDelimiter("*");, Java interprets the asterisk as a wildcard, and uses EVERY character as a delimiter... This is obviously not what I want. I've tried using a backslash as an escape character, but that gives me the compiler error "illegal escape character". This is probably very

Java Scanner input with if else statement

不想你离开。 提交于 2019-12-02 04:31:31
问题 Hi I'm new to java and trying to make a Quiz to practice. I wanna make a Question where the user has to combine words from to categories to pairs. Like A1 B4 C3 D2. What I did now is using an if else statement to check if the input is the correct answer, but it only works with 1A. For the others I can do 6 inputs, which is not what I want, and even if there's a correct one I don't get a point. public class HelloWorld { public static void main(String[] args) { Scanner walther = new Scanner

how to interrupt a scanner.nextline() call

你说的曾经没有我的故事 提交于 2019-12-02 02:12:30
问题 There are many threads on SO about interrupting reading the system.in but what I am looking for here is some kind of advice as to how to best code what I am trying to achieve. I have a getlogin() method that needs to do the following: ask a user to input the desired login environnement details, if after 6 seconds user have not input a valid value ("live" or "test") then set userlogin variable to "test" and return it to the caller. I have taken the following approach for the getlogin()

Java Scanner to print previous and next lines

江枫思渺然 提交于 2019-12-02 02:11:18
I am using 'java.util.Scanner' to read and scan for keywords and want to print the previous 5 lines and next 5 lines of the encountered keyword, below is my code ArrayList<String> keywords = new ArrayList<String>(); keywords.add("ERROR"); keywords.add("EXCEPTION"); java.io.File file = new java.io.File(LOG_FILE); Scanner input = null; try { input = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } int count = 0; String previousLine = null; while(input.hasNext()){ String line = input.nextLine(); for(String keyword : keywords){ if(line.contains(keyword)){ //print prev

Why is Scanner skipping inputs from the user

僤鯓⒐⒋嵵緔 提交于 2019-12-02 01:45:57
问题 I have a small task that allows the user to enter the regions and their neighbors of any country. I did everything and I just have a small problem which is when I run my code and the program asks the user to enter the number of regions, if the user enters 13 or and number greater than 10, the system will consider that number is like two inputs and it will not allow the user to enter anything for the second question and it will prompt him with the third question immediately. Why? I think the

Splitting up data file in Java Scanner

我们两清 提交于 2019-12-02 00:55:30
I have the following data which I want to split up. (1,167,2,'LT2A',45,'Weekly','1,2,3,4,5,6,7,8,9,10,11,12,13'), to obtain each of the values: 1 167 2 'LT2A' 45 'Weekly' '1,2,3,4,5,6,7,8,9,10,11,12,13' I am using the Scanner class to do that and with , as the delimiter. But I face problems due to the last string: ('1,2,3,4,5,6,7,8,9,10,11,12,13') . I would hence like some suggestions on how I could split this data. I have also tried using ,' as the delimiter but the string contains data without ''. The question is quite specific to my needs but I would appreciate if someone could give me

Infinite loop on Scanner.hasNext, reading from a file

自作多情 提交于 2019-12-02 00:42:21
I'm apparently facing an infinite loop on while(input.hasNext()) , as in following code File file = new File("data.txt"); Scanner input = new Scanner(file); int sum = 0; while (input.hasNext()) { if (input.hasNextInt()) { sum += input.nextInt(); } } System.out.println(sum); Some related questions explain why it always returns true if input stream is System.in , however I'm scanning through a File . Please let me know where I'm going wrong. I'm attempting to calculate the sum of unique integer values (space delimited where they occur). Edit: Lesson learned, input.hasNext() does not move the

how to interrupt a scanner.nextline() call

[亡魂溺海] 提交于 2019-12-02 00:31:07
There are many threads on SO about interrupting reading the system.in but what I am looking for here is some kind of advice as to how to best code what I am trying to achieve. I have a getlogin() method that needs to do the following: ask a user to input the desired login environnement details, if after 6 seconds user have not input a valid value ("live" or "test") then set userlogin variable to "test" and return it to the caller. I have taken the following approach for the getlogin() implementation: launch two threads which do the following: thread1 creates a scanner object then calls scanner

Why is Scanner skipping inputs from the user

三世轮回 提交于 2019-12-01 23:03:13
I have a small task that allows the user to enter the regions and their neighbors of any country. I did everything and I just have a small problem which is when I run my code and the program asks the user to enter the number of regions, if the user enters 13 or and number greater than 10, the system will consider that number is like two inputs and it will not allow the user to enter anything for the second question and it will prompt him with the third question immediately. Why? I think the problem with the Scanner class in the following command: Scanner kb = new Scanner(System.in); System.out

Remove all vowels in a string with Java

久未见 提交于 2019-12-01 22:24:10
I am doing a homework assignment for my Computer Science course. The task is to get a users input, remove all of the vowels, and then print the new statement. I know I could easily do it with this code: string.replaceAll("[aeiou](?!\\b)", "") But my instructor wants me to use nested if and else if statements to achieve the result. Right now I am using something like this: if(Character.isLetter('a')){ 'do something' }else if(Character.isLetter('e')){ 'do something else' But I am not sure what to do inside the if and else if statements. Should I delete the letter? Or is there a better way to do