java.util.scanner

How to stop reading multiple lines from stdin using Scanner?

半世苍凉 提交于 2019-12-04 11:14:21
问题 I'm working on an JAVA assignment should process multiple lines of input. The instructions read "Input is read from stdin." An example of sample input is given: one 1 two 2 three 3 I don't understand what the above sample input "read from stdin" means. Here's a test program I wrote that isolates my confusion: import java.io.*; import java.util.Scanner; class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); while(stdin.hasNextLine()) { String line = stdin

How to read comma separated integer inputs in java

試著忘記壹切 提交于 2019-12-04 06:57:20
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) you can use the nextLine method to read a String and use the method split to separate by comma like this: public static void main(String args[]) { Scanner dis=new

Placing correct discs Connect Four java game with drawing panel

折月煮酒 提交于 2019-12-04 06:36:37
问题 I'm trying to make a Connect Four Game in Java by using user input and drawing panel. Two people switch off turns typing the column they want their piece to go in. I'm having trouble making it so discs already placed stay on the board the next turn (because the a new drawing panel pops up with the updated board each time)(we tried using GUI but failed). I am also having trouble switching off turns and making the color change each time. Thank you so much, here is what I have so far. import

how to use two scanners on one method

非 Y 不嫁゛ 提交于 2019-12-04 06:17:38
问题 earlier today I asked how to re-try/catch input mismatch exception without getting caught by infinite loop but it's two procedures process, at first the game will ask the user for the size of the grid and later after the launch it will ask him either to set up a flag or step over a cell(if mine the game will be over else it will print out the number of surrounding mines), but I get some weird errors the code: int gridSize = 0; try (Scanner scanner = new Scanner(System.in)) { System.out

how to read a text file using scanner in Java?

依然范特西╮ 提交于 2019-12-04 02:35:44
This is my code to read a text file. When I run this code, the output keeps saying "File not found.", which is the message of FileNotFoundException . I'm not sure what is the problem in this code. Apparently this is part of the java. For the whole java file, it requires the user to input something and will create a text file using the input as a name. After that the user should enter the name of the text file created before again (assume the user enters correctly) and then the program should read the text file. I have done other parts of my program correctly, but the problem is when i enter

Different Java Scanner for input of different types

穿精又带淫゛_ 提交于 2019-12-04 02:13:32
问题 Imagine the following scanario: I have a program which ask for an integer input, followed by a String input. int age=0; String name; Scanner sc = new Scanner(System.in); System.out.print("Enter Age: "); age = sc.nextInt(); System.out.print("Enter Name: "); name= sc.nextLine(); With the aobe codes, I was not given a chance to enter the name. So normally I will declare 2 scanner objects as follows: int age=0; String name; Scanner sc = new Scanner(System.in); Scanner sc2 = new Scanner(System.in)

Java Scanner Continuous User input?

て烟熏妆下的殇ゞ 提交于 2019-12-03 20:28:08
问题 For java practice, i am trying to create a program that reads integers from the keyboard until a negative one is entered. and it prints the maximum and minimum of the integer ignoring the negative. Is there a way to have continuous input in the same program once it runs? I have to keep running the program each time to enter a number. Any help would be appreciated public class CS { public static void main(String []args) { Scanner keys = new Scanner(System.in); System.out.println("Enter a

Is the scanner in java not thread safe?

橙三吉。 提交于 2019-12-03 18:12:08
问题 I'm interested in using java.util.Scanner . I was reading the docs and saw a line saying A Scanner is not safe for multi threaded use without external synchronization. Can I confirm that this means that two separate Scanner objects in two separate threads operating on two separate files could interfere with each other? Can anyone help me to synchronise scanner object externally to use for safe thread operation? 回答1: If you use the same instance of Scanner in two threads you will have trouble

How can I get this switch statement to work using a scanner?

无人久伴 提交于 2019-12-03 14:47:38
I'm trying to write a program that will switch any letter of the alphabet (upper or lower cases) into the Phontic alphabet. For example, If I enter "A" or "a" my program will give me (change it to) "Alpha". I've done so much research on this and switch statements but I keep getting stuck. I've realized that I can't use 'char' in a scanner. However, when I change 'char' into a 'String' my switch statement messes up (specifically the toUpperCase in my code gets underlined. I can't see my mistake. Here's what I've done so far: import java.util.Scanner; public class PhoneticTranslate { public

How to stop reading multiple lines from stdin using Scanner?

与世无争的帅哥 提交于 2019-12-03 06:59:31
I'm working on an JAVA assignment should process multiple lines of input. The instructions read "Input is read from stdin." An example of sample input is given: one 1 two 2 three 3 I don't understand what the above sample input "read from stdin" means. Here's a test program I wrote that isolates my confusion: import java.io.*; import java.util.Scanner; class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); while(stdin.hasNextLine()) { String line = stdin.nextLine(); String[] tokens = line.split(" "); System.out.println(Integer.parseInt(tokens[1])); } }