java.util.scanner

Scanning text file into array of objects

爷,独闯天下 提交于 2019-12-10 22:40:00
问题 I have a comma separated text file with information in the format: firstname,lastname,meal1,meal2,meal3,meal4 ....with each new student on a new line. I have the following student object. public class Student { private String first = null; private String last = null; public Student (String first, String last){ this.first = first; this.last = last; } I need a method that is to be used from another class to populate an Array of student objects. I am unsure how to do this with the Scanner as I

Scanner's nextLine(), Only fetching partial

最后都变了- 提交于 2019-12-10 21:20:18
问题 So, using something like: for (int i = 0; i < files.length; i++) { if (!files[i].isDirectory() && files[i].canRead()) { try { Scanner scan = new Scanner(files[i]); System.out.println("Generating Categories for " + files[i].toPath()); while (scan.hasNextLine()) { count++; String line = scan.nextLine(); System.out.println(" ->" + line); line = line.split("\t", 2)[1]; System.out.println("!- " + line); JsonParser parser = new JsonParser(); JsonObject object = parser.parse(line).getAsJsonObject();

Reading text with Java Scanner next(Pattern pattern)

旧巷老猫 提交于 2019-12-10 20:55:44
问题 I am trying to use the Scanner class to read a line using the next(Pattern pattern) method to capture the text before the colon and then after the colon so that s1 = textbeforecolon and s2 = textaftercolon. The line looks like this: something:somethingelse 回答1: There are two ways of doing this, depending on specifically what you want. If you want to split the entire input by colons, then you can use the useDelimiter() method, like others have pointed out: // You could also say "scanner

Reading a text file using BufferedReader and Scanner

雨燕双飞 提交于 2019-12-10 19:04:39
问题 I need to read the first n lines of a text file as lines (each line may or may not contain whitespace). The remainder of the text file contains an unknown number N of tokens that are whitespace-delimited (delimiters are a mixture of space, tab, and newline characters, all to be treated identically as delimiters). I know how to read lines using BufferedReader. I know how to read tokens using Scanner. But how do I combine these two different modes of reading for a single text file, in the above

Does a Java Scanner implicitly create a buffer even if you do not pass it one?

我的梦境 提交于 2019-12-10 17:35:15
问题 If I have the following example file where each number is represents a byte (123 has bytes 1, 2, and 3): 123456789 Let's say I create a FileInputStream. This reads in the binary byte by byte. So .read() returns 1, then 2, etc. Now let's say I create a buffer. The initial chunk it reads in (if I understand buffers correctly) is 1-5. This allows it to not only read in byte by byte, but in the case of characters whole lines, etc. But if I hit .read() again, I start at 6, and NOT where the

In Java, should I use a different Scanner instance for different types of input?

你。 提交于 2019-12-10 16:36:00
问题 I need to get input from the user. They get to create a new flower. The user tells me the flower's name (String), the flower's color (String), the number of thorns on the flower (int) and the flower's smell (String). I was using a single Scanner named input to get all of this. However, it wouldn't work properly. After it got the number of thorns, the program would ask the user what the flower smells like but it wouldn't let me input an answer. However, I created a second Scanner named input2

How to read all the lines of a file using java code?

自古美人都是妖i 提交于 2019-12-10 16:09:26
问题 I have a strange problem where I have a log file called transactionHandler.log.It is a very big file having 17102 lines.This i obtain when i do the following in the linux machine: wc -l transactionHandler.log 17102 transactionHandler.log But when i run the following java code and print the number of lines i get 2040 as the o/p. import java.io.*; import java.util.Scanner; import java.util.Vector; public class Reader { public static void main(String[] args) throws IOException { int counter = 0;

Getting unicode values from System.in

别来无恙 提交于 2019-12-10 14:45:54
问题 I have created a Scanner that gets input from System.in so that I can get input from the console. Scanner scanner = new Scanner(System.in, "UTF-8"); When I do String s = scanner.next(); and then input Слово דבר in the console, the value of the string becomes ???? ??? . The console is able to display Unicode characters, but why can't I read them? 回答1: It's not safe to assume System.in is UTF-8 encoded. See this question for some workarounds. 回答2: This is because System.in returns text in

Input using scanner class in java

夙愿已清 提交于 2019-12-10 13:44:09
问题 I was making a program to reduce given integers to their simplest ratio.But an error is occurring while taking inputs through Scanner class in a sub-method of program.Here is the code : package CodeMania; import java.util.Scanner; public class Question5 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int T=sc.nextInt();// number of test cases sc.close(); if(T<1) { System.out.println("Out of range"); System.exit(0); } for(int i=0;i<T;i++) { ratio();//line 19 } }

is this bad programming ? scanner as global variable [closed]

被刻印的时光 ゝ 提交于 2019-12-10 13:19:04
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . Is it considered bad programming practice to have an input scanner (such as a keyboard ) declared as a global var for a class? such like: private static Scanner input = new Scanner(System.in); Im working with alot of input from various methods, and just seems alot easier then