java.util.scanner

Is it mandatory to close a Scanner?

家住魔仙堡 提交于 2020-01-14 09:39:08
问题 With the below code I'm able to yield the same output with/without the scanner close method. import java.util.Scanner; class CheckPassFail { //Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise. static int k; static String str; public static void main(String arg[]) { System.out.println("Enter the marks : "); Scanner typein = new Scanner(System.in); str = typein.nextLine(); typein.close(); //Scanner Close

Java read text file in lines, but seperate words in lines into array

耗尽温柔 提交于 2020-01-14 06:05:51
问题 I am trying to write a code that looks at a text file, and does a while loop so that it reads each line, but I need each word per line to be in an array so I can carry out some if statements. The problem I am having at the moment is that my array is currently storing all the words in the file in an array.. instead of all the words per line in array. Here some of my current code: public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("test.txt")); List

Java read text file in lines, but seperate words in lines into array

╄→гoц情女王★ 提交于 2020-01-14 06:05:23
问题 I am trying to write a code that looks at a text file, and does a while loop so that it reads each line, but I need each word per line to be in an array so I can carry out some if statements. The problem I am having at the moment is that my array is currently storing all the words in the file in an array.. instead of all the words per line in array. Here some of my current code: public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("test.txt")); List

Fastest way of processing Java IO using ASCII lines

别来无恙 提交于 2020-01-14 04:14:22
问题 I'm working with an ASCII input/output stream over a Socket and speed is critical. I've heard using the right Java technique really makes a difference. I have a textbook that says using Buffers is the best way, but also suggests chaining with DataInputStreamReader. For output I'm using a BufferedOutputStream with OutputStreamWriter which seems to be fine. But I am unsure on what to use for the input stream. I'm working on new lines, so would Scanner be of any use? Speed is critical, I need to

Read Japanese Character using Scanner

牧云@^-^@ 提交于 2020-01-13 19:26:34
问题 Possible duplicate: How can I read Chinese characters correctly using Scanner in Java? My input file name may have japanese characters and I am trying to read the file name using Scanner. Scanner sc = new Scanner(System.in,"utf-8"); System.out.println("Encoding is :" + Charset.defaultCharset()); System.out.println("Enter the path:"); inputFilePath = sc.nextLine(); and if my input is for eg - 漢字 When I print the file name my output is Encoding is :UTF-8 Input File Path:漢字 I also tried,

Why does Scanner implement Iterator<String>?

做~自己de王妃 提交于 2020-01-12 14:18:41
问题 I was just wondering why java.util.Scanner implements java.util.Iterator? Scanner implements the remove method and throws an UnsupportedOperationException. But shouldn't a class, when implementing an interface, fulfill the contract of the interface? What is the use of implementing iterator and adding a method that throws an exception? Why not just avoid the implementation of the interface and keep it simple? One can argue that it is defined so that the class which might extend Scanner could

Why does Scanner implement Iterator<String>?

断了今生、忘了曾经 提交于 2020-01-12 14:16:25
问题 I was just wondering why java.util.Scanner implements java.util.Iterator? Scanner implements the remove method and throws an UnsupportedOperationException. But shouldn't a class, when implementing an interface, fulfill the contract of the interface? What is the use of implementing iterator and adding a method that throws an exception? Why not just avoid the implementation of the interface and keep it simple? One can argue that it is defined so that the class which might extend Scanner could

Java Scanner String input

情到浓时终转凉″ 提交于 2020-01-11 13:19:30
问题 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()

How to use comma and dot as delimiter in addition with Java default delimiter

怎甘沉沦 提交于 2020-01-11 06:26:32
问题 I have a text file which contains lot of permutations and combinations of special characters, white space and data. I am storing the content of this file into an array list, and if i am not using useDelimiter() function, Java is reading my text perfectly. The only issue is that its not accepting comma ( , ) and dot ( . ) as delimiter. I know I can use input.useDelimiter(",|.| |\n") to use comma , dot , space as delimiter and others options as well, but then the results I get are not correct

How to use comma and dot as delimiter in addition with Java default delimiter

拥有回忆 提交于 2020-01-11 06:26:15
问题 I have a text file which contains lot of permutations and combinations of special characters, white space and data. I am storing the content of this file into an array list, and if i am not using useDelimiter() function, Java is reading my text perfectly. The only issue is that its not accepting comma ( , ) and dot ( . ) as delimiter. I know I can use input.useDelimiter(",|.| |\n") to use comma , dot , space as delimiter and others options as well, but then the results I get are not correct