java.util.scanner

Java reading multiple ints from a single line

狂风中的少年 提交于 2019-11-27 00:53:14
I am working on a program and I want to allow a user to enter multiple integers when prompted. I have tried to use a scanner but I found that it only stores the first integer entered by the user. For example: Enter multiple integers: 1 3 5 The scanner will only get the first integer 1. Is it possible to get all 3 different integers from one line and be able to use them later? These integers are the positions of data in a linked list I need to manipulate based on the users input. I cannot post my source code, but I wanted to know if this is possible. Prasanth Louis I use it all the time on

Does the Scanner class load the entire file into memory at once?

試著忘記壹切 提交于 2019-11-26 23:37:48
问题 I often use the Scanner class to read files because it is so convenient. String inputFileName; Scanner fileScanner; inputFileName = "input.txt"; fileScanner = new Scanner (new File(inputFileName)); My question is, does the above statement load the entire file into memory at once? Or do subsequent calls on the fileScanner like fileScanner.nextLine(); read from the file (i.e. from external storage and not from memory)? I ask because I am concerned about what might happen if the file is too huge

Read next word in java

夙愿已清 提交于 2019-11-26 22:40:28
I have a text file that has following content: ac und accipio annehmen ad zu adeo hinzugehen ... I read the text file and iterate through the lines: Scanner sc = new Scanner(new File("translate.txt")); while(sc.hasNext()){ String line = sc.nextLine(); } Each line has two words. Is there any method in java to get the next word or do I have to split the line string to get the words? Christopher Tokar You do not necessarily have to split the line because java.util.Scanner's default delimiter is whitespace. You can just create a new Scanner object within your while statement. Scanner sc2 = null;

Extract Integer Part in String

こ雲淡風輕ζ 提交于 2019-11-26 22:33:27
What is the best way to extract the integer part of a string like Hello123 How do you get the 123 part. You can sort of hack it using Java's Scanner, is there a better way? Why don't you just use a Regular Expression to match the part of the string that you want? [0-9] That's all you need, plus whatever surrounding chars it requires. Look at http://www.regular-expressions.info/tutorial.html to understand how Regular expressions work. Edit: I'd like to say that Regex may be a little overboard for this example, if indeed the code that the other submitter posted works... but I'd still recommend

Issues with nextLine(); [duplicate]

本小妞迷上赌 提交于 2019-11-26 22:22:52
问题 Possible Duplicate: Scanner issue when using nextLine after nextInt I am trying create a program where it lets the user inputs values into an array using scanner. However, when the program asks for the student's next of kin, it doesn't let the user to input anything and straight away ends the program. Below is the code that I have done: if(index!=-1) { Function.print("Enter full name: "); stdName = input.nextLine(); Function.print("Enter student no.: "); stdNo = input.nextLine(); Function

Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?

佐手、 提交于 2019-11-26 22:22:36
问题 How does this program actually work...? import java.util.Scanner; class string { public static void main(String a[]){ int a; String s; Scanner scan = new Scanner(System.in); System.out.println("enter a no"); a = scan.nextInt(); System.out.println("no is ="+a); System.out.println("enter a string"); s = scan.nextLine(); System.out.println("string is="+s); } } The output is: enter the no 1234 no is 1234 enter a string string is= //why is it not allowing me to enter a string here? 回答1: .nextInt()

Scanner close after use

梦想的初衷 提交于 2019-11-26 22:12:09
问题 While using scanner like following: Scanner s = new Scanner(System.in); String response = s.next(); Boolean approved = (response.contains("Y") || response.contains("y")) ? true : false; if (approved){ Do Stuff } s.close(); I'm getting no such Element exception exception: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source)**** I'm calling s (Scanner) multiple times, the runtime error occur on the

Blank input from scanner - java

自古美人都是妖i 提交于 2019-11-26 21:44:54
问题 What I'm trying to do is if the user clicks on the enter key the program should throw a BadUserInputException. My problem is whenever I press the enter key it just puts me in another line in the console essentially doing nothing. Scanner input = new Scanner(System.in); System.out.println("Enter Student ID:"); String sID = null; if (input.hasNextInt()==false){ System.out.println("Please re-check the student number inputted. Student number can only be digits."); throw new BadUserInputException(

how to take user input in Array using java?

南楼画角 提交于 2019-11-26 21:23:05
问题 how to take user input in Array using Java? i.e we are not initializing it by ourself in our program but the user is going to give its value.. please guide!! 回答1: Here's a simple code that reads strings from stdin , adds them into List<String> , and then uses toArray to convert it to String[] (if you really need to work with arrays). import java.util.*; public class UserInput { public static void main(String[] args) { List<String> list = new ArrayList<String>(); Scanner stdin = new Scanner

Scanner error with nextInt() [duplicate]

只愿长相守 提交于 2019-11-26 20:57:59
This question already has an answer here: How to use java.util.Scanner to correctly read user input from System.in and act on it? 1 answer I am trying to use Scanner to get an int from the keyboard, but I getting the following error: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at TableReader.mainMenu(TableReader.java:122) at TableReader.main(TableReader.java:76) This is what I have. It is