java.util.scanner

How to make a while to run until scanner get input?

守給你的承諾、 提交于 2019-12-19 07:28:53
问题 I'm trying to write a loop which runs until I type a specific text in console where the application is running. Something like: while (true) { try { System.out.println("Waiting for input..."); Thread.currentThread(); Thread.sleep(2000); if (input_is_equal_to_STOP){ // if user type STOP in terminal break; } } catch (InterruptedException ie) { // If this thread was intrrupted by nother thread }} And I want it to write a line each time it pass through so I do not want it to stop within the while

Rock, Paper, Scissors Game Java [closed]

混江龙づ霸主 提交于 2019-12-19 05:08:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I'm new to programming and I'm trying to write a very simple Rock, Paper, Scissors game in Java. It will compile and run fine, but I am looking to say something like "Invalid move. Try again." or something along those lines for when the user (personPlay) does not enter a correct

How to read non-english characters with Scanner java?

笑着哭i 提交于 2019-12-19 04:55:16
问题 there I'm making this app to change subtitle files. when I was testing it I faced a strange problem, when I was testing it on non-english (persian for instance) the program wouldn't read the file. this is how I read subtitles in my program: Scanner sub = null; try { sub = new Scanner(new File(address)); } catch (FileNotFoundException ex) { ex.printStackTrace(); } while(sub.hasNext()){ String sentence = sub.nextLine(); //some magical stuff here :) } where address is a String keeping place of

How to read comma separated integer inputs in java

风格不统一 提交于 2019-12-19 03:08:50
问题 import java.io.*; import java.util.*; class usingDelimiters { public static void main(String args[]) { Scanner dis=new Scanner(System.in); int a,b,c; a=dis.nextInt(); b=dis.nextInt(); c=dis.nextInt(); System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); } } This program is working fine when my input is 1 2 3 (separated by space) But, how to modify my program when my input is 1,2,3 (separated by commas) 回答1: you can use the nextLine method to read a String and

How do I ensure that Scanner hasNextInt() asks for new input?

萝らか妹 提交于 2019-12-18 07:04:13
问题 New programmer here. This is probably a really basic question, but it's stumping me nevertheless. What I'm trying to do is write a method that supplies only one integer input so I can use that input in my main program without having to mess around with non-integer inputs. However, even writing the method to do that in its own method seems to be problematic. public static int goodInput () { Scanner input = new Scanner (System.in); //construct scanner boolean test = input.hasNextInt(); //set a

How to get line number using scanner

情到浓时终转凉″ 提交于 2019-12-17 20:45:54
问题 I'm using scanner to read a text file line by line but then how to get line number since scanner iterates through each input?My program is something like this: s = new Scanner(new BufferedReader(new FileReader("input.txt"))); while (s.hasNext()) { System.out.print(s.next()); This works fine but for example: 1,2,3 3,4,5 I want to know line number of it which mean 1,2,3 is in line 1 and 3,4,5 is in line 2.How do I get that? 回答1: You could use a LineNumberReader in place of the BufferedReader to

Scanner reading large file

 ̄綄美尐妖づ 提交于 2019-12-17 20:09:27
问题 I am playing around with the Scanner class for learning purposes and i use it to read a very large file (60.000 lines aprox) without using the Reader class , and it stops reading after approximately 400 lines. Do i have to use a Bufferedreader inside the Scanner's constructor or the problem is something else? I want to know why this is happening. Thanks. My code is the usual code to output all the lines. File file1 = new File("file1"); Scanner in= new Scanner(file1); while (scan.hasNextLine()

Java Scanner doesn't wait for user input [duplicate]

血红的双手。 提交于 2019-12-17 16:34:32
问题 This question already has answers here : Scanner is skipping nextLine() after using next() or nextFoo()? (16 answers) Closed 2 years ago . I am using Java's Scanner to read user input. If I use nextLine only once, it works OK. With two nextLine first one doesnt wait for user to enter the string(second does). Output: X: Y: (wait for input) My code System.out.print("X: "); x = scanner.nextLine(); System.out.print("Y: "); y = scanner.nextLine(); Any ideas why could this happen? Thanks 回答1: It's

Eclipse character encoding

陌路散爱 提交于 2019-12-17 16:27:46
问题 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

Read line with Scanner

末鹿安然 提交于 2019-12-17 15:54:26
问题 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