java.util.scanner

How do I stop while loop from running infinitely?

江枫思渺然 提交于 2019-12-24 02:43:24
问题 Can't figure out how to stop this while loop from repeating infinitely. I'm using hasNextInt to check if user input is an int. If an int is not entered the loop repeats infinitely. public static void validatingInput(){ Scanner scan = new Scanner(System.in); boolean valid = false; int userNumber = 0; while(!valid) { System.out.println("Enter number between 1 and 20: "); if (scan.hasNextInt()) { userNumber = scan.nextInt(); valid = true; } else System.out.print("Not an int. "); } } 回答1: You

Java Scanner Take Input, call a method, then back to read more input not working

喜你入骨 提交于 2019-12-24 02:16:12
问题 I have a class called PlayGame , and in the main method I have this chunk of code: public class PlayGame { public static void main(String args[]) { while (true) { System.out.println("Where would you like your adventure to begin? (Enter a number)\n"); System.out.println("1. Play the Game\n2. Quit the Game"); Scanner userInput = new Scanner(System.in); String userAction; try { userAction = userInput.nextLine().trim(); if (userAction.equals("1")) { pressPlay(); } else if (userAction.equals("2"))

Using Java.util.scanner with GWT

若如初见. 提交于 2019-12-24 00:38:22
问题 for some reason when I try to use scanner with gwt, i get the following error: No source code is available for type java.util.Scanner; did you forget to inherit a required module? I looked around and it seems the "No source code is available for type xxxx" errors are due to not having a Javascript equivalent type for the Java type. Is scanner not able to be used with GWT? Here is a snippet of my code: import java.util.Scanner; ... public void submit(){ String text = editor.getEditor().getText

checking for is letter in a string

元气小坏坏 提交于 2019-12-24 00:35:33
问题 I am asking for user input in a string and i want to check for is alpha or numeric but i am new to java. this is what i have so far Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); isletter(s); // a call to the function // function public void isletter(String s) { for (int i = 0; i < s.length(); i++) if (isLetter(s.charAt(i) ) ) { System.out.println("is alpha = " + s); } else{ } } Here is the error i am getting when trying to compile through dos c:\programming>javac

Java Scanner input dilemma. Automatically inputs without allowing user to type

耗尽温柔 提交于 2019-12-23 23:32:16
问题 Thanks a lot for responses, I will probably stick to just adding extra input.nextLine() statements to catch any "leftovers" So in this code I input 2, and once it goes to the if statement it skips the "sCreateLogin = input.nextLine();" and proceeds to the next input. Probably because there is something lingering in the Scanner yet I cannot figure out why it does it and how exactly to fix it. If I do input.next() it stops, but it just isn't good enough because if you accidentally add a space

Java: how to read an input int

折月煮酒 提交于 2019-12-23 23:09:15
问题 So, I was looking for an efficient way, using Java's standard packages, to read an input integer... For example, I came across the class "Scanner", but I found two main difficulties: if I don't insert an int, I'm not actually able to solve the exception; this class works with tokens, but my aim is to load the string in its full length. This is an example of execution I would like to realize: Integer: eight Input error - Invalid value for an int. Reinsert: 8 secondtoken Input error - Invalid

Reading File into Array - Java

…衆ロ難τιáo~ 提交于 2019-12-23 20:15:23
问题 I am practicing java, and looking at exercises online: However, I am stuck at the point in which I need to Read the file again, and initialise the elements of the array Task Write class Members representing a list of members as an array Constructor should take String argument (file name) Use scanner to read lines and create array big enough to hold the file Read the file again and initialise elements of the array Current Code import java.io.*; import java.util.*; class Members { MemberElement

Simple Java Scanner code not working [duplicate]

跟風遠走 提交于 2019-12-23 19:39:33
问题 This question already exists : Scanner issue when using nextLine after nextXXX [duplicate] Closed 5 years ago . Here is the skeleton of some basic code I am writing to make a simple game: Scanner in = new Scanner(System.in); String name; String playing; int age; do { System.out.println("Enter your name"); name = in.nextLine(); System.out.println("Enter your age"); age = in.nextInt(); System.out.println("Play again?"); playing = in.nextLine(); } while (true); The code does not work as expected

Java Using Scanner Multiple Times

断了今生、忘了曾经 提交于 2019-12-23 13:32:17
问题 I am having this problem a lot. When I use a Scanner a lot of times, it doesn't get input from user. Scanner scan = new Scanner(System.in); System.out.println("1---"); int try1 = scan.nextInt(); System.out.println("2---"); int try2 = scan.nextInt(); System.out.println("3---"); String try3 = scan.nextLine(); System.out.println("4---"); String try4 = scan.nextLine(); When I run this code, result it : 1--- 12 2--- 321 3--- 4--- aa As you can see, it skipped at 3rd input. Why this is happening? I

Reading a Text File From a .jar File

旧城冷巷雨未停 提交于 2019-12-23 13:08:09
问题 I have a rookie question. I'm currently writing a program that takes in alot of data from a text file using the File and Scanner class as shown below: File data = new File("champdata.txt"); Scanner read = new Scanner(data); read.useDelimiter("%"); The Scanner then retrieves data from the text file correctly while in the IDE, but when I run the program as a .jar file, the file cannot be retrieved. I've read a little about adding a text file to the .jar file itself, and using the InputStream