java.util.scanner

Java print two lines together

為{幸葍}努か 提交于 2019-12-11 08:47:09
问题 I'm trying to print a message on the screen and after that, take a value from the keyboard. i have 4 prints in a row but i have scan methods between them. When i run my code, the first two prints run together and i can't insert a value at the first variable, after the first print. case 1: System.out.println("###Book Data###"); System.out.print("Name of the book:\t"); String Name = key.nextLine(); System.out.print("ISBN of the book:\t"); String ISBN = key.nextLine(); System.out.print("Author

Emulating user input for java.util.Scanner

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 08:06:02
问题 I'm writing a game in Java, and I want the user to be able to provide input from both the command line and my GUI. Currently, I use this method to get input: static String getInput(){ System.out.println("Your move:"); Scanner sc = new Scanner(System.in); return sc.nextLine(); } I want to keep using this, but let a mousePressed event emulate the user actually typing in their input as well. It's not that efficient of a solution, but it makes sense in my application. So the question is: how do I

Java Unicode Problems (I think)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 07:47:45
问题 I'm new to Java, so bear with me if I say anything stupid! I'm having a few problems, which I think are Unicode-related. I'm using Scanner to read in tokenised commands from a text file, saved with UTF-8 encoding. Basically I want to first check that the command isn't equal to either "command1" or "command2" (I do something else in these cases), then otherwise read in a character. If the token isn't a single character, I'm going to output an error. Here's my code: public static void main

Count same lines in a file, JAVA

♀尐吖头ヾ 提交于 2019-12-11 07:36:11
问题 I have a text file which has lines of animals, who occur on this list from 1 to n times. I need to read this text file, count all the separate animal occurences, sort them from highest to lowest and put them in a jtable. For example, the text file looks like this: dog sheep cat horse cat tiger cat cat tiger I need to count all the same occurences like so: dog 1 sheep 1 cat 4 horse 1 tiger 2 And then sort them from highest to lowest and somehow put them into a table, so that would be: Animal

Scanner hasNextInt() return true if hasNextInt() has nothing

人盡茶涼 提交于 2019-12-11 06:18:30
问题 After reading the hasNextInt() API, I'm still at a lost at this code. Scanner input = new Scanner(System.in); while (input.hasNextInt()) { input.next(); } Why does the loop start even though input has nothing. Also, the hasNextInt() API mentions The scanner does not advance past any input. What does that mean? And does that have anything to do with it? 回答1: Scanner input = new Scanner(System.in) is not a blocking call, it's just creating a new Scanner with STDIN as input. Thus execution

scanner nextLine() exception: no line found

こ雲淡風輕ζ 提交于 2019-12-11 05:46:28
问题 I'm doing the scanner for a static method, and occur this exception: Exception in thread "main" java.util.NoSuchElementException: No line found My modify method tends to get 2 string as input from the console, but it doesn't work. NOTE: I didn't use scanner.close(); static ArrayList<Book> modBook(){ Book tempbook = Book.searchTitle(); if(tempbook !=null){ Scanner sc = new Scanner(System.in); int i = BookList.indexOf(tempbook); System.out.println("Please enter title:"); String booktitle = sc

Multi-word scanner input in java?

不羁岁月 提交于 2019-12-11 05:06:23
问题 So I'm trying to use if-else statement dependant upon the user's input. It works when the user's input is only one word, however, multiple word inputs go unrecognized and triggers the else statement. How can i resolve this? import java.util.Scanner; public class MyFirstJavaClass { public static void main(String[] args) { @SuppressWarnings("resource") Scanner myScanner = new Scanner(System.in); String answer; System.out.println("Catch the tiger or run away?"); answer = myScanner.next(); if

JOptionPane and Scanner input issue

旧城冷巷雨未停 提交于 2019-12-11 03:46:43
问题 I'm writing a program where at one point, I need to print a String on a window using JOptionPane. The code for the line looks something like this: JOptionPane.showMessageDialog(null, "Name: " + a.getName()); The getName function refers to the object a that i created that has a method that returns a String. However, when my code reaches this point, the program appears to enter some kind of infinite loop as the window never pops up and when using debug, it appears never-ending. The main thing

1. Scanning current (as opposed to next) line location. 2. Scanning line X (Java Beginner)

徘徊边缘 提交于 2019-12-11 03:13:51
问题 let's say I have a text file I'm inputing text from... File file = new File("example.txt"); Scanner inputFile = new Scanner(file); if I want to reference the next line of text I would do inputfile.nextLine(); Let's say I want to reference that same line of text again. Is there like a "currentLine()" method? What else could I do? In general, let's say I want to open the file and refer to the 3rd line of text or the 150th line or whatever, how do I get the Scanner to read that specific line?

If statement skipping right to else after being called once?

*爱你&永不变心* 提交于 2019-12-11 02:50:04
问题 I'm making a simple coin toss game, and I wrote several methods to call and make my main class short and simple. After the game is played once, the first If/Else statement to ask users for input is jumping right to the Else statement without prompting for input. package cointoss; import java.util.Random; import java.util.Scanner; public class Game { int money; int result; int bet; Random rn = new Random(); Scanner in = new Scanner(System.in); String playerPick; String aResult; public void