java.util.scanner

Java scanner input validation [duplicate]

。_饼干妹妹 提交于 2021-01-28 09:57:14
问题 This question already has answers here : Validating input using java.util.Scanner [duplicate] (6 answers) Closed 3 years ago . I am trying to accept only three numbers from my input: 1, 2, 3. Any other different than that must be invalid. I have created method but I don't have an idea why it did not working. What must I change? int number; do { System.out.println("Enter 1, 2 or 3"); while (!scanner.hasNextInt()) { System.out.println("Invalid input!"); } number = scanner.nextInt(); } while

Java scanner input validation [duplicate]

徘徊边缘 提交于 2021-01-28 09:56:03
问题 This question already has answers here : Validating input using java.util.Scanner [duplicate] (6 answers) Closed 3 years ago . I am trying to accept only three numbers from my input: 1, 2, 3. Any other different than that must be invalid. I have created method but I don't have an idea why it did not working. What must I change? int number; do { System.out.println("Enter 1, 2 or 3"); while (!scanner.hasNextInt()) { System.out.println("Invalid input!"); } number = scanner.nextInt(); } while

Java Scanner - next String until a | is found

微笑、不失礼 提交于 2021-01-27 19:20:50
问题 Currently I'm trying to read through some basic cells, in this format: +-------+-------+ | | | +-------+-------+ Now I need to get the string representation of the cell's contents and send it off to another method. The problem is that the cells have no pre-defined length. I'm reading these from a file, so my easiest option should be to just use the Scanner I already have set up. Problem is, I don't really know how for this case. I have a strong feeling that I need to use the pattern somehow,

How to check the user input is an integer or not with Scanner?

此生再无相见时 提交于 2021-01-21 11:53:43
问题 I want the country codes are integer that input by the user. I want an error message to be show when user inputs a code which is not an integer. How can I do this? The program is to ask user to enter country name and country code. In which user will input the country code. But if user inputs a character I want a message to be shown saying Invalid Input. System.out.println("Enter country name:"); countryName = in.nextLine(); System.out.println("Enter country code:"); int codeNumber = in

Finding Index of last character in an input string JAVA?

断了今生、忘了曾经 提交于 2021-01-01 09:58:13
问题 I'm trying to figure out how I can find the index of the last character in the first word in a String variable, while using Scanner class to get the String. In my code I need to use a single String for full name, and find the index of the last character in the first name. So far I made this: String fullName; Scanner s = new Scanner(System.in); System.out.println("Insert full name: "); //For example: Joseph Adams. fullName = s.nextLine(); int position = fullName.indexOf(" "); System.out

Finding Index of last character in an input string JAVA?

落爺英雄遲暮 提交于 2021-01-01 09:57:44
问题 I'm trying to figure out how I can find the index of the last character in the first word in a String variable, while using Scanner class to get the String. In my code I need to use a single String for full name, and find the index of the last character in the first name. So far I made this: String fullName; Scanner s = new Scanner(System.in); System.out.println("Insert full name: "); //For example: Joseph Adams. fullName = s.nextLine(); int position = fullName.indexOf(" "); System.out

How to skip a leading string when scanning text file?

牧云@^-^@ 提交于 2020-12-15 05:36:50
问题 I'm making a program that draws a basic image using instructions from a text file. The format for the instructions is: SIZE 1000 500 // GROUND LINE 0 350 1000 350 LINE 0 351 1000 351 LINE 0 352 1000 352 LINE 0 353 1000 353 and this is my code: public void start(Stage stage) { int fwidth = 0; int fheight = 0; try { Scanner obj = new Scanner(new File("scene.txt")); while(obj.hasNextLine()){ String str = obj.nextLine(); if(str.contains("SIZE")){ String a = "SIZE"; obj.skip(a); System.out.println

How to JUnit test a Method with a Scanner?

做~自己de王妃 提交于 2020-12-12 11:11:37
问题 I have a class which reads a file and takes in a user input with a scanner and if the scanner equals a part of a line in that file, it will display a string from the same line. How would I go and create a Junit test method for this? Here is some of my code that I want a test method for: Scanner Input = new Scanner(System.in); String name = Input.nextLine(); BufferedReader br; try{ br = new BufferedReader(new FileReader(new File(filename))); String nextLine; while ((nextLine = br.readLine()) !

How to JUnit test a Method with a Scanner?

↘锁芯ラ 提交于 2020-12-12 11:09:51
问题 I have a class which reads a file and takes in a user input with a scanner and if the scanner equals a part of a line in that file, it will display a string from the same line. How would I go and create a Junit test method for this? Here is some of my code that I want a test method for: Scanner Input = new Scanner(System.in); String name = Input.nextLine(); BufferedReader br; try{ br = new BufferedReader(new FileReader(new File(filename))); String nextLine; while ((nextLine = br.readLine()) !

How to print list of string inside bracket? [closed]

倾然丶 夕夏残阳落幕 提交于 2020-11-30 01:57:29
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last month . Improve this question I am new to Java. I am facing a problem to print a list of string separated by commas inside brackets. public class House extends Property { protected static List<String> paints; public String paint; public House() { super("House", 45); String paints = new String()