java.util.scanner

Read utf-8 using Scanner [closed]

ぃ、小莉子 提交于 2019-12-01 03:26:21
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am having trouble with UTF-8 encoding when using Scanner . Example two lines of my data file: 000001 Mėlynas Tadas 63210309683 V 2003/03/17 2016/03/17 000002 Raudonas Tomas 65505023282 V 2006/01/26 2018/01/26 Currently I am using Scanner to read the text separately instead of the whole line as this is more

Scanner.nextLine() blocks when using InputStream from Socket

核能气质少年 提交于 2019-12-01 03:13:29
问题 When I receive data using Socket.getInputStream() directly (without some kind of interface like Scanner), it doesn't block. But, when I try to use a Scanner (similar to how we receive Strings from System.in ), it does. I was wondering the reason for this, and how the InputStream that a connected Socket supplies to you is different from the InputStream in in System . The Client used for testing (used for both servers) The code that hangs: public class Server { public static void main(String[]

How to determine the end of a line with a Scanner?

不想你离开。 提交于 2019-12-01 01:35:35
问题 I have a scanner in my program that reads in parts of the file and formats them for HTML. When I am reading my file, I need to know how to make the scanner know that it is at the end of a line and start writing to the next line. Here is the relevant part of my code, let me know if I left anything out : //scanner object to read the input file Scanner sc = new Scanner(file); //filewriter object for writing to the output file FileWriter fWrite = new FileWriter(outFile); //Reads in the input file

Tokenising a String containing empty tokens

ぐ巨炮叔叔 提交于 2019-11-30 15:13:43
问题 I have a seemingly simple problem of splitting a comma separated String into tokens, whereby the output should include empty tokens in cases where: The first character in the String is a comma. The last character in the String is a comma. Two consecutive commas occur. For example, for the String : ",abd,def,,ghi," should yield the output: {"", "abd", "def", "", "ghi", ""} . I have tried using String.split , Scanner and StringTokenizer for this but each gives a different undesired output

Tokenising a String containing empty tokens

家住魔仙堡 提交于 2019-11-30 13:55:41
I have a seemingly simple problem of splitting a comma separated String into tokens, whereby the output should include empty tokens in cases where: The first character in the String is a comma. The last character in the String is a comma. Two consecutive commas occur. For example, for the String : ",abd,def,,ghi," should yield the output: {"", "abd", "def", "", "ghi", ""} . I have tried using String.split , Scanner and StringTokenizer for this but each gives a different undesired output (examples below). Can anyone suggest an elegant solution for this, preferably using JDK classes? Obviously I

How to check the end of line using Scanner?

社会主义新天地 提交于 2019-11-30 13:20:01
问题 I have searched similar questions, but none helped. Consider a file : hi how are you? where were you? I want to do few operations after the end of every line. If I use next() it wont tell me when I have reached the end of the first line. Also I have seen hasNextLine() but it only tells me if there exists another line or not. 回答1: Consider using more than one Scanner, one to get each line, and the other to scan through each line after you've received it. The only caveat I must give is that you

What does scanner.close() do?

孤街醉人 提交于 2019-11-30 12:31:27
Say I have the following example code: Scanner scan1 = new Scanner(System.in); // declaring new Scanner called scan1 int x = scan1.nextInt(); // scan for user input and set it to x System.out.println(x); // print the value of x scan1.close(); // closes the scanner (I don't know exactly what this does) Scanner scan2 = new Scanner(System.in); // declaring new Scanner called scan1 int y = scan2.nextInt(); // scan for user input and set it to y System.out.println(y); // print the value of y I read the Oracle documentation on the Scanner class and came across this: When a Scanner is closed, it will

How to use Scanner to read silently from STDIN in Java?

好久不见. 提交于 2019-11-30 12:23:56
I want to make a Java program that reads a Password from STDIN silently. I mean, without outputting any pressed chars to the terminal and keeping it hidden from commandline history and the operating system processlist ps . The class java.io.Console may be useful: System.console().readPassword(); This reads a sequence of chars from the console, without echoing anything. Note that it only works when you launch your java application with a real console. Otherwise, System.console() returns null. A less secure option to get the password via STDIN that works with background jobs, virtual consoles,

Creating a Java Program to Search a File for a Specific Word

百般思念 提交于 2019-11-30 10:24:11
I am just learning that language and was wondering what a more experience Java programmer would do in the following situation? I would like to create a java program that will search a specified file for all instanced for a specific word. How would you go about this, does that Java API come with a class that provides file scanning capabilities or would i have to write my own class to do this? Thanks for any input, Dom. The java API does offer the java.util.Scanner class which will allow you to scan across an input file. Depending on how you intend to use this, however, this might not be the

Scanner No Line Found Exception

假装没事ソ 提交于 2019-11-30 10:01:19
问题 I am getting the following exception. java.util.NoSuchElementException: No line found I got this error while writing a larger program which needed to read from a text file, and so decided to do a test. Scanner scan = new Scanner(new File("restrictions.txt"); String s1 = scan.nextLine(); System.out.println(s1); And I still get the exception. I have a text file in the same folder as the class called restrictions.txt which has text in it. What am I doing wrong? 回答1: new File("restrictions.txt")