java.util.scanner

Java accepting only numbers from user with Scanner

﹥>﹥吖頭↗ 提交于 2019-12-12 02:29:08
问题 I am trying to understand how to only accept numbers from the user, and I have attempted to do so using try catch blocks, but I would still get errors with it. Scanner scan = new Scanner(System.in); boolean bidding; int startbid; int bid; bidding = true; System.out.println("Alright folks, who wants this unit?" + "\nHow much. How much. How much money where?" ); startbid = scan.nextInt(); try{ while(bidding){ System.out.println("$" + startbid + "! Whose going to bid higher?"); startbid =+ scan

java.util.Scanner.throwFor(Unknown Source). Why doesn't my code work?

瘦欲@ 提交于 2019-12-12 01:57:42
问题 Ok so I have written two blueprint classes and one tester.I keep getting this same error: 'java.util.Scanner.throwFor(Unknown Source)'. Can anyone enlighten me on what I am doing wrong? Here is my code. blueprint class ShoppingCart: package exercise3; import java.util.Scanner; import javax.swing.JOptionPane; public class ShoppingCart { ScanShop amount = new ScanShop(); public void getbill() { JOptionPane.showMessageDialog(null,"your total is: " + amount.getcart()); } public void billCal() {

Out of memory using Scanner to read large file into memory

雨燕双飞 提交于 2019-12-12 01:26:00
问题 The following block of code is throwing a java.lang.OutOfMemoryError exception when I pass a large file into the scanner. What is the best way to fix this? Is the problem in the arraylist or the scanner? ArrayList rawData = new ArrayList(); Scanner scan = new Scanner(file); while (scan.hasNext()) { String next = scan.next(); rawData.add(next); } 回答1: Increase the java heap size, e.g. java -Xmx6g myprogram will set the heap size to 6 gigabytes. Of course there will always be a limit.... 回答2:

Java passing scanner into method

一曲冷凌霜 提交于 2019-12-12 01:06:29
问题 So I'm going through a bunch of basic programming ideas to help me get the hang of Java and I created a program that will print PI up to the 10th decimal place (and I can add more if I want). However, I decided to go an extra step and create an option to keep running the program over and over until the user tells it to stop. I created a method to return either true (run again) or false (exit program). Originally I created a scanner in the method to take user input, and the program runs fine

Checking user input is a number

假如想象 提交于 2019-12-11 23:56:04
问题 I need to check users input. I have a menu and I need the user to select numbers 0-4 but if the user selects a letter instead of a number then I just get a InputMismatchException. So I am trying to validate that the user entered a number. Here is my code: public class TestBankAccount { static Scanner input = new Scanner(System.in); public static void main(String[] args) throws FileNotFoundException { ArrayList<BankAccount> list = new ArrayList<BankAccount>(); int choice; do { System.out

java.util.NoSuchElementException on reading a file (Scanner) [duplicate]

依然范特西╮ 提交于 2019-12-11 23:42:05
问题 This question already has an answer here : Scanner only reads file name and nothing else (1 answer) Closed 2 years ago . I have a text file with 3 words on it, and on trying to read these words I get the following error: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at search.search(search.java:121) at main.main(main.java:38) Below is the code in question: Scanner reader = new Scanner(path +

while loop with hasNextInt() finishes, but program does not do anything after it

安稳与你 提交于 2019-12-11 21:40:32
问题 So I have this program here: ArrayList<Integer> meh = new ArrayList<Integer>(); Scanner sc = new Scanner(System.in); System.out.print("Input 3 numbers: "); while (sc.hasNextInt()) { meh.add(sc.nextInt()); System.out.println("meeeeeep"); } System.out.println("Goodbye"); Output (if 3 integers are inputed): meeeeeep meeeeeep meeeeeep It doesn't print the goodbye message, or do anything that I put after it. 回答1: That's because the scanner is waiting for the coming input. sc.hasNextInt() is

Using Scanner to validate user input

两盒软妹~` 提交于 2019-12-11 21:36:27
问题 I have a hard time to figure out how to use the Scanner correctly to validate the user input. For example, the program need to have a user input of int, user can only input positive number, and if the user input "apple","delicious apple",or negative number, the problem will show error message. I have tried the code below, and then i found a disturbing questions, the "That's not a number!" are printed twice, I have no idea what caused this questions.... import java.util.*; public class input {

Scanner cant find my file to read [Java with eclipse]

寵の児 提交于 2019-12-11 21:05:43
问题 I'm working on a project that takes in criteria supplied by a user, and compares it to an already created list of object containing similar criteria. Currently, I'm trying to get the program to read the file, but I keep getting my exception and not what I want. My code for the scanner and file is as followed: package project205; import java.util.*; import java.io.File; import java.util.Scanner; import java.io.FileNotFoundException; public class HouseList { ArrayList<House> houseList = new

Detect scanner input in java (double or int)

最后都变了- 提交于 2019-12-11 20:09:31
问题 I was trying to do this: import java.util.Scanner; public class Prov{ public static void main(){ Scanner getInfo = new Scanner(System.in); showInfo(getInfo.next()); } public void showInfo(int numb){ System.out.println("You typed this integer: " + numb); } public void showInfo(double numb){ System.out.println("You typed this double: " + numb); } } but it doesnt work no matter if I look for scanner.next or scanner.nextInt it wont just get a double when i write a double and an int when I type an