java.util.scanner

Java Scanner Changing String

ε祈祈猫儿з 提交于 2019-12-24 12:24:28
问题 I'm doing some basic file reading from a text file using Scanner. The first 5 entries are this - 0 MR2Spyder 1 Tundra 3 Echo 3 Yaris 4 ScionxB 4 ScionxD I instantiate the scanner normally and then do this - String line = scanner.nextLine(); System.out.println(line); I then get this output - ÿþ0 M R 2 S p y d e r Which doesn't make sense to me- is there some problem with the Scanner class? Should I be using BufferedReader? 回答1: Your file is encoded using UTF-16... the spaces between characters

Cannot find the scanner

风格不统一 提交于 2019-12-24 11:40:12
问题 I have a very bad experienced about the scanner because I am using GUI and JOptionPane . So I am not be able to do the program's interns of scanner because. I am new to it so Please help me, "cannot find the scanner ". This is my code so far . import java.io.*; import java.util.*; class MultiplicationTables{ public static void main(String args[]){ int n, c; System.out.println("Enter an integer to print its multiplication table"); Scanner in = new Scanner(System.in); n = in.nextInt(); System

Java Input Problems - how to compare strings [duplicate]

怎甘沉沦 提交于 2019-12-24 11:09:04
问题 This question already has answers here : How do I compare strings in Java? (23 answers) Closed 4 years ago . This seems to be pretty simple, but I have been stucked here for a couple of hours. I have a doubt when you have to compare two Strings in Java. if I just do something like this: String var1 = "hello"; String var2 = "hello"; and then compare these two words in another function, the result will clearly be true. But the problem is when I have to compare two words that come from an input.

Java app hangs on in.hasNext();

我怕爱的太早我们不能终老 提交于 2019-12-24 10:45:30
问题 I am working on Battleship swing app that communicates through sockets. private ServerSocket server; private Socket connection; private PrintWriter out; private Scanner in; I make a connection and setup output and input streams out = new PrintWriter(connection.getOutputStream(), true); in = new Scanner(connection.getInputStream()); Then user clicks on field that he thinks there is a ship, and coordinates get sent public void sendXY(String cord) { out.print(cord); } Then the method gets called

Take date input from user and save it as date in java

十年热恋 提交于 2019-12-24 07:36:28
问题 I'm trying to take input from user in (dd/mm/yy) format, save it in same format and pass it with getter, setter to another class. I have tried this way: This is what I have tried: public static void main(String[] args) { Scanner input = new Scanner(System.in); SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy"); Guest gstObject = new Guest(); try { System.out.println("Enter check-in date (dd/mm/yy):"); String cindate = input.next(); Date date1 = myFormat.parse(cindate); gstObject

Java, Regular Expression HasNext starts with empty line, multi-platform support

穿精又带淫゛_ 提交于 2019-12-24 06:41:14
问题 I need to process the following file on Unix and Windows: a;b c;d;e;f;g c;d;e;f;g c;d;e;f;g a;b c;d;e;f;g c;d;e;f;g c;d;e;f;g a;b a;b c;d;e;f;g c;d;e;f;g c;d;e;f;g i need to process a;b that contain a block of data underneath. e.g. the third a;b shouldn't be processed. currently i am delimiting by using the following regular expression this type of text in a file using Java scanner: Scanner fileScanner = new Scanner(file); try{ fileScanner.useDelimiter(Pattern.compile("^$", Pattern.MULTILINE)

Inheritance: Proper initialization to store values into a HashMap

╄→尐↘猪︶ㄣ 提交于 2019-12-24 05:30:44
问题 I am working with separate programs, one will read in a .txt file and then that same program will store the read in values into another program containing a HashMap . The program that contains the HashMap , is named Hero.java and is an abstract class . I have a program that extends that program called Cop.java . With that in mind, I would like to know the proper way to transfer the files read in from my GameDriver.java over to the HashMap . To my knowledge, one way of doing this is

Add a line at a time to a String

杀马特。学长 韩版系。学妹 提交于 2019-12-24 03:17:04
问题 I have this program that lets you open a file and it reads it in to a JTextArea all at once using the following code: try{ String fileContents = new Scanner(new File(fileName)).useDelimiter("\\Z").next(); }catch(FileNotFoundException ex){ ex.printStackTrace(); } myTextArea.setText(fileContents); and this works. But my question is how can I read this into my fileContents string and still have it add in the line breaks every time I get a new-line character? Here is what I have, but this puts it

What are the advantages and disadvantages of reading an entire file into a single String as opposed to reading it line by line?

拟墨画扇 提交于 2019-12-24 03:14:52
问题 Specifically, my end goal is to store every comma separated word from the file in a List<String> and I was wondering which approach I should take. Approach 1: String fileContents = new Scanner(new File("filepath")).useDelimiter("\\Z").next(); List<String> list = Arrays.asList(fileContents.split("\\s*,\\s*")); Approach 2: Scanner s = new Scanner(new File("filepath")).useDelimiter(","); List<String> list = new ArrayList<>(); while (s.hasNext()){ list.add(s.next()); } s.close(); 回答1: Approach #1

how to re-try/catch input mismatch exception without getting caught by infinite loop

馋奶兔 提交于 2019-12-24 03:14:40
问题 I'm trying to take input from the user to build a grid for a 2d mine's weeper game, the process goes pretty smooth when I pass valid values to the Scanner, but when I try invalid things it goes through infinite loop even the try block is try with resources which should close the scanner with each a new try, it sounds it doesn't close it when it prints the string on the catch infinitely int gridSize = 0; System.out.println("how much do you want the grid's size"); try (Scanner scanner = new