java.util.scanner

The need to catch IOExceptions & Exceptions

徘徊边缘 提交于 2019-12-11 12:03:50
问题 Should one always catch an IOException/Exception if a program reading in a txt file using a scanner object produces nothing but a FileNotFoundException? Would such extra code be unneeded or important? import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TestingScanner { public TestingScanner() { readFile("dummy.txt"); } public void readFile(String filePath) { File file = new File(filePath); Scanner scanner = null; try { scanner = new Scanner(file);

While loop to check user input for only positive integers

跟風遠走 提交于 2019-12-11 11:57:36
问题 I need help implementing a loop that keeps telling the user to enter only positive integers, but I don't know where to start. Can someone help me. import java.util.Scanner; public class GCD { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a positive integer: "); int firstN = input.nextInt(); System.out.print("Enter another positive integer: "); int secondN = input.nextInt(); System.out.println("The GCD of " + firstN + " and " + secondN

Java - How do I stop the user from entering something other than an integer?

牧云@^-^@ 提交于 2019-12-11 11:22:23
问题 I've got the program working fine, other the fact that the user can still enter strings/chars that will break the program, causing an error. I want to write a statement so that when the user inputs anything other than an integer between 0-100 an error message will be displayed (basically my string saying they can't do that) or preferably they wont be able to enter anything besides an integer at all (if possible). Here is my code: int[] cw_Mark = new int[6]; for (int i = 0; i < cw_Mark.length;

Scanner not asking for input , throws No Such Element Exception - multiple scanners

坚强是说给别人听的谎言 提交于 2019-12-11 10:37:10
问题 In the below code I have used two scanners, One in performAction() method and one in getTicketNumber() method. I have used try with resources and once when I go into getTicketNumber() method, it works fine and once when the control returns back and comes to performAction() I am getting No Such Element Exception when it executes the scanner.nextInt() line. I wanted to continue this process until I press exit. I guess it is due to the use of multiple scanners since if I don't go into

How do I parse a String to get the decimal and a word with a dot properly?

南楼画角 提交于 2019-12-11 10:25:56
问题 How do I differentiate the difference in finding a decimal but at the same time ignoring it if it is a period? For example, assume Scanner String s = "2015. 3.50 please"; When I use the function scanner.hasNextFloat() , how do I ignore Hi. ? I am only scanning 1 line. I need to identify whether a word is string, integer, or float. My final result should look something like this: This is a String: 2015. This is a float: 3.50 This is a String: please But in my conditions when I use scanner

String while loop

情到浓时终转凉″ 提交于 2019-12-11 10:18:33
问题 I need to check if the user enter a number(double) or string, everything works perfect except the last part. If the user enters "hello", the program will ask to enter the valid number but it doesn't work properly. It gives me an infinite loop unless I will enter space " ". Here is my code: double bill = 0.0; System.out.print("Please enter the total amount of your bill > "); String strAmount = keysIn.nextLine(); try { bill = Double.parseDouble(strAmount); while (bill < 0) { System.out.print(

java.util.NoSuchElementException Error when closing the Scanner

不想你离开。 提交于 2019-12-11 10:15:09
问题 public class a2 { public static int read() { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); sc.close(); return num; } public static void out (int a, int b) { System.out.println("Sum: " + (a+b)); System.out.println("Difference: " + (a-b)); System.out.println("Product: " + (a*b)); System.out.println("Quotient: " + ((double)a/(double)b)); System.out.println("Remainder: " + (a%b)); } public static void main(String[] args) { System.out.println("Please enter two integers!:"); int a =

Validate input of a scanner: is it an integer and within a certain number range?

此生再无相见时 提交于 2019-12-11 10:09:19
问题 I'm new at programming and I am currently following a Java class where I must validate a user's scanner input. The code below seems to work but I think it's a little bit inefficient. There must be a better way to do this! Your suggestions are welcome! Here's what I've got: do { System.out.println("What is you age?"); while (!scSudoku.hasNextInt()) { System.out.println("You must enter an number"); scSudoku.next(); } age = scSudoku.nextInt(); if (age < 10 || age > 90) { System.out.println("You

Start reading the file after a specific word

为君一笑 提交于 2019-12-11 09:44:19
问题 I have a text file with some information in it, which looks something like displayed below. I'm supposed to read the file after a specific word occurs ( Complete Population ), and store the vertically aligned values in each line like in an array (could be arraylist too) What the file looks like - Tue May 14 08:27:25 EST 2013 mating_pool=80 mutation_dist=3 algo_name=ARMO Complete Population 8.78792396E8 7.45689508E8 8.37899916E8 9.52778502E8 8.47061622E8 8.80017166E8 7.50224432E8 8.23658404E8

Scanner is not taking input of another string and skipping it

微笑、不失礼 提交于 2019-12-11 08:54:27
问题 Why scanner is not taking input of another string and skepping it? I cant understand, here is my code: import java.util.Scanner; public class demo { public static void main(String[] args) { Scanner s = new Scanner(System.in); String name; String address; int age; System.out.println("Enter your name"); name = s.nextLine(); System.out.println("My name is :" + name); System.out.println("Enter your age"); age = s.nextInt(); System.out.println("My age is :" + age); System.out.println("Enter your