java.util.scanner

How to get ArrayList<Integer> and Scanner to play nice?

强颜欢笑 提交于 2019-12-01 07:38:50
import java.util.*; public class CyclicShiftApp{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<Integer>(); while(scan.hasNextInt()){ list.add(scan.nextInt()); } Integer[] nums = new Integer[list.size()]; nums = list.toArray(nums); for(int i = 0;i < nums.length; i++){ System.out.println(nums[i]); } } Thanks to poor-mans-debugging I've found that the while(scan.hasNextInt()) isnt actually adding anything. What might be going wrong? Is my google-fu weak or lack of know-how letting me down? I am rather new to programming, and

Java Scanner class reading strings [duplicate]

十年热恋 提交于 2019-12-01 06:50:12
This question already has an answer here: Java Scanner class reading strings 5 answers I got the following code: int nnames; String names[]; System.out.print("How many names are you going to save: "); Scanner in = new Scanner(System.in); nnames = in.nextInt(); names = new String[nnames]; for (int i = 0; i < names.length; i++){ System.out.print("Type a name: "); names[i] = in.next(); } System.out.println(names[0]); When I run this code, the scanner will only pick up the first name and not the last name. And it will sometimes skip a line when trying to enter a name, it will show up as if I had

Scanner.nextLine() blocks when using InputStream from Socket

我与影子孤独终老i 提交于 2019-12-01 05:51:06
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[] args) { try { ServerSocket ss = new ServerSocket(15180); Socket socket = ss.accept(); Scanner scanner =

How to get Java scanner to acknowledge blank input?

£可爱£侵袭症+ 提交于 2019-12-01 05:46:57
I am having trouble getting my program to respond to empty inputs. For example, say I wanted to prompt the user to enter a value for money with type BigDecimal as well as a currency type. Here is what the program in question looks like. public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the amount of money " + "and specify currency (USD or CNY): "); // initialize variable moneyInput BigDecimal moneyInput; // check if moneyInput is numeric try { moneyInput = input.nextBigDecimal(); } catch (InputMismatchException e){ System.err.println(

Read utf-8 using Scanner [closed]

两盒软妹~` 提交于 2019-12-01 05:43:57
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 convenient, but it doesn't read correctly because of the encoding. I've read about using InputStream etc but I don't want to deal with messy line chopping. Is there a way to use Scanner with UTF-8? This code snippet might help: Locale loc = new Locale("es", "ES"); Scanner sc = new Scanner

How exactly does Java Scanner parse double?

假如想象 提交于 2019-12-01 05:38:00
I'm using a Windows 7 machine whose "Control Panel\Clock, Language, and Region" is "Denmark" According to the documentation for Scanner : A scanner's initial locale is the value returned by the Locale.getDefault() method; But when I run the code: System.out.println(Locale.getDefault()); Scanner sc = new Scanner("1.0"); sc.nextDouble(); It outputs "en_US" and then throws a java.util.InputMismatchException at sc.nextDouble() . It works when the scanner is initialized with "1,0" However, if I explicitly set the Locale: Locale.setDefault(Locale.US); System.out.println(Locale.getDefault()); Scanner

How to get ArrayList<Integer> and Scanner to play nice?

非 Y 不嫁゛ 提交于 2019-12-01 05:10:52
问题 import java.util.*; public class CyclicShiftApp{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<Integer>(); while(scan.hasNextInt()){ list.add(scan.nextInt()); } Integer[] nums = new Integer[list.size()]; nums = list.toArray(nums); for(int i = 0;i < nums.length; i++){ System.out.println(nums[i]); } } Thanks to poor-mans-debugging I've found that the while(scan.hasNextInt()) isnt actually adding anything. What might be

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

喜夏-厌秋 提交于 2019-12-01 04:48:50
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 and wait for next input. Do I need to use multiple threads for this? Do I need to use multiple threads

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

大憨熊 提交于 2019-12-01 04:01:29
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 1 word at a time and decides how to ////add it to the output file while (sc.hasNext() == true) {

How exactly does Java Scanner parse double?

余生长醉 提交于 2019-12-01 03:56:59
问题 I'm using a Windows 7 machine whose "Control Panel\Clock, Language, and Region" is "Denmark" According to the documentation for Scanner: A scanner's initial locale is the value returned by the Locale.getDefault() method; But when I run the code: System.out.println(Locale.getDefault()); Scanner sc = new Scanner("1.0"); sc.nextDouble(); It outputs "en_US" and then throws a java.util.InputMismatchException at sc.nextDouble() . It works when the scanner is initialized with "1,0" However, if I