java.util.scanner

Multiple delimiters in Scanner class of Java

假装没事ソ 提交于 2019-11-28 00:46:05
How do I use the useDelimiter() method of the Scanner class to use both the comma (,) and the new line character (\n) as delimiters? I am parsing some text from a csv file. Scanner s = new Scanner("hello, world \n hello world"); s.useDelimiter(",|\\n"); while(s.hasNext()){ System.out.println(s.next()); } Output hello world hello world JavaDoc How about useDelimiter(",|\\n"); useDelimiter takes a regex pattern, so, it would be something like ",|\n" Jigar is absolutely correct. But if it doesn't work, try ",|\\r" since most text files have \r\n instead of just \n Using Scan Delimiters for Excel

Search a file for a String and return that String if found

╄→гoц情女王★ 提交于 2019-11-28 00:33:38
问题 How can you search through a txt file for a String that the user inputs and then return that String to the console. I've written some code that doesn't work below, but I hope it can illustrate my point... public static void main(String[] args) { searchforName(); } private static void searchForName() throws FileNotFoundException { File file = new File("leaders.txt"); Scanner kb = new Scanner(System.in); Scanner input = new Scanner(file); System.out.println("Please enter the name you would like

What does Scanner input = new Scanner(System.in) actually mean?

大憨熊 提交于 2019-11-28 00:30:55
问题 Scanner input = new Scanner(System.in); Could you give me a detailed explanation on what the code above is doing step by step? I don't really understand how it works and how it links to me later being able to do this statement: int i = input.nextInt() 回答1: Alright, let's elaborate with some simplified explanation about the Scanner class. It is a standard Oracle class which you can use by calling the import java.util.Scanner . So let's make a basic example of the class: class Scanner{

how to take user input in Array using java?

*爱你&永不变心* 提交于 2019-11-27 23:14:40
how to take user input in Array using Java? i.e we are not initializing it by ourself in our program but the user is going to give its value.. please guide!! polygenelubricants Here's a simple code that reads strings from stdin , adds them into List<String> , and then uses toArray to convert it to String[] (if you really need to work with arrays). import java.util.*; public class UserInput { public static void main(String[] args) { List<String> list = new ArrayList<String>(); Scanner stdin = new Scanner(System.in); do { System.out.println("Current list is " + list); System.out.println("Add

Eclipse character encoding

一曲冷凌霜 提交于 2019-11-27 22:59:00
I am using Scanner to scan a .txt document in Java. However, when I open the .txt document in Eclipse, I notice some characters are not being recognized, and they are replaced with something that looks like this: � These characters won't even let me scan the file as while(scan.hasNext) automatically returns false (if these characters are not present, then I can scan the document just fine). So, how do I get Eclipse to recognize these characters so I can scan? I can't manually remove them because the document is quite large. Thanks. Juned Ahsan The file you are reading must be containing UTF-8

Java Scanner class reading strings

ⅰ亾dé卋堺 提交于 2019-11-27 20:28:24
I got the following code: int nnames; String names[]; System.out.print("How many names are you going to save: "); Scanner in = new Scanner(System.in); nnames = in.nextInt(); names = new String[nnames]; for (int i = 0; i < names.length; i++){ System.out.print("Type a name: "); names[i] = in.nextLine(); } And the output for that code is the following: How many names are you going to save:3 Type a name: Type a name: John Doe Type a name: John Lennon Notice how it skipped the first name entry?? It skipped it and went straight for the second name entry. I have tried looking what causes this but I

Read line with Scanner

余生长醉 提交于 2019-11-27 20:28:19
EDIT for further readers: the problem was that my input file was corrupted. I don't understand what I'm doing wrong : I was using this code : File f = new File("C:\\Temp\\dico.txt"); BufferedReader r = null; try { r = new BufferedReader(new FileReader(f)); String scan; while((scan=r.readLine())!=null) { if(scan.length()==0) {continue;} //treatment } } catch (FileNotFoundException ex) { Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex); } finally { if(r!=null) try { r.close(); }

Java Scanner String input

谁都会走 提交于 2019-11-27 20:09:18
I'm writing a program that uses an Event class, which has in it an instance of a calendar, and a description of type String. The method to create an event uses a Scanner to take in a month, day, year, hour, minute, and a description. The problem I'm having is that the Scanner.next() method only returns the first word before a space. So if the input is "My Birthday", the description of that instance of an Event is simply "My". I did some research and found that people used Scanner.nextLine() for this issue, but when I try this, it just skips past where the input should go. Here is what a

Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()

て烟熏妆下的殇ゞ 提交于 2019-11-27 19:56:32
My professor tends to do the following to get a number from the user: Scanner scanner = new Scanner(System.in); Integer.parseInt(scanner.nextLine()); What are the benefits as opposed to simply doing scanner.nextInt() ? java.util.Scanner.java has the following in it: public int nextInt() { return nextInt(defaultRadix); } public int nextInt(int radix) { // Check cached result if ((typeCache != null) && (typeCache instanceof Integer) && this.radix == radix) { int val = ((Integer)typeCache).intValue(); useTypeCache(); return val; } setRadix(radix); clearCaches(); // Search for next int try {

Is it safe not to close a Java Scanner, provided I close the underlying readable?

天大地大妈咪最大 提交于 2019-11-27 19:39:51
If I have a method that takes a reader and I want to operate on the reader with a Scanner like so: Scanner scanner = new Scanner(reader); while(scanner.hasNext()) { //blah blah blah } Is it safe not to close scanner ? Documentation says that it "closes this scanner" and then talks about closing the underlying readable. Suppose I don't want to close the readable and instead want the caller to close reader when ready. Is it safe not to close scanner here? It depends what you want to be safe against. If you are just trying to ensure that the underlying stream is closed, then either approach is