java.util.scanner

Java Scanner - Delimit by spaces unless quotation marks are present?

耗尽温柔 提交于 2020-01-24 11:03:54
问题 I'm trying to use the Scanner class in Java to get data from a configuration file. The file's elements are delimited by whitespace. However, if a phrase or element should be interpreted as a string literal (including whitespace), then double or single-quotes are places around the element. This gives files that look like this: > R 120 Something AWord > P 160 SomethingElse "A string literal" When using the Java Scanner class, it delimits by just whitespace by default. The Scanner class has the

Java Scanner - Delimit by spaces unless quotation marks are present?

爷,独闯天下 提交于 2020-01-24 11:03:49
问题 I'm trying to use the Scanner class in Java to get data from a configuration file. The file's elements are delimited by whitespace. However, if a phrase or element should be interpreted as a string literal (including whitespace), then double or single-quotes are places around the element. This gives files that look like this: > R 120 Something AWord > P 160 SomethingElse "A string literal" When using the Java Scanner class, it delimits by just whitespace by default. The Scanner class has the

How is GUI code being invoked before my Scanner?

雨燕双飞 提交于 2020-01-21 12:40:40
问题 I'm having some trouble with getting input from the command line before opening a GUI window. I asked this question previously on Apple Exchange but was sent here after we determined it to be a Programming problem. Basically I'm running a Scanner to get user input before I open up a window but it starts the program, switching spaces on my Mac, and then I have to switch back to the workspace with the terminal in it to answer the question. Here's a link to the original question. https://apple

Calling Scanner.close() throws nosuchelement exception

ぐ巨炮叔叔 提交于 2020-01-21 12:11:05
问题 I have a simple method that prints a command to the screen, scan's a user's input, and returns it as a String. If the user's input is invalid, it notifies the user and asks again. This method worked perfectly but my instructor mentioned that we should always close resources, so I went back and added in the close method and now I'm getting a NoSuchElementException every time the method is called, regardless of user input. Here is the code... private String getUserString(String userCommand) {

Help please, while loop and tokenizer and reading files

牧云@^-^@ 提交于 2020-01-17 04:54:06
问题 I need help, obviously. Our assignment is to retrieve a file and categorize it and display it in another file. Last name first name then grade. I am having trouble with getting a loop going because of the error "java.util.NoSuchElementException" This only happens when I change the currently existing while I loop I have. I also have a problem of displaying the result. The result I display is all in one line, which I can't let happen. We are not allowed to use arraylist, just Bufferedreader,

In java Scanner.next()

邮差的信 提交于 2020-01-17 04:50:29
问题 public class Calculator { public static void main(String[] args) { boolean isValid = false; Scanner myScanner = new Scanner(System.in); String customerType = null; System.out.print("Customer Type? (C/R) "); customerType = myScanner.next(); while (isValid == false) { System.out.print("Enter Subtotal: "); if (myScanner.hasNextDouble()) { double sobTotal = myScanner.nextDouble(); isValid = true; } else { System.out .println("Hay! Entry error please enter a valid number"); } myScanner.nextLine();

How to prevent storing of text coming after special characters — in arraylist in java

眉间皱痕 提交于 2020-01-17 01:22:12
问题 I am reading a pl/sql code from a text file and storing all words of it into array list from below code : Scanner in1 = new Scanner(file1); ArrayList<String> Code1 = new ArrayList<String>(); in1.useDelimiter("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|[\\p{javaWhitespace}\\.|,]+"); while (in1.hasNext()) { Code1.add(in1.next().toLowerCase()); } Everything is working fine but i am facing issue whenever there is a comments section in code written in after special character -- Like below: select * from

java Scanner.hasNext() usage

岁酱吖の 提交于 2020-01-16 13:13:16
问题 public static void main(String args[]){ Scanner in = new Scanner(System.in); String a = in.next(); if (in.hasNext()) { System.out.println("OK") } else { System.out.println("error"); } } What I want is: if the user type in a String with more than one word, print "OK". if the user type in a String with only one word, print "error". However, it doesn't work well. When I type a single word as an input, it doesn't print "error" and I don't know why. 回答1: Read a line and then check whether there

Java Scanner Headache

半城伤御伤魂 提交于 2020-01-15 11:18:20
问题 I have a text file which looks like: name1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 name2 1 0 1 0 1 0 0 1 1 0 0 0 0 0 1 i.e., a plaintext label followed by a few rows with 1/0 separated by spaces. The number of rows of 1/0 is variable, but each row between any two particular labels should have the same number of 1/0s (though might potentially not). How do I grab each name+rows chunk with a scanner? Is there any elegant way to enforce the consistency on the number of rows (and provide some sort of

How to scan a file in a different directory in java?

馋奶兔 提交于 2020-01-14 14:59:10
问题 How do you scan a file with java that isn't in the directory the java file is in? For example: The java file is located at "C:\Files\JavaFiles\test.java" However, the file I want to scan is located at "C:\Data\DataPacket99\data.txt" Note: I've already tried putting another java file in the "C:\Data" directory and using the test.java file as a class, but it doesn't work. It still tries to scan from the "C:\Files\JavaFiles" Directory. 回答1: By using an absolute path instead of a relative. File