java.util.scanner

`NullPointerException` thrown while searching text file [closed]

女生的网名这么多〃 提交于 2019-12-13 09:37:19
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . First, I keep getting a NullPointerException on the line I put in ** below. Second, my program is giving the wrong output (I somehow got it to work but then it went back to error). It must be a logic error. I have a file directory.txt of 11 lines, each with a name on it. When I run my program to try to find a

Try-Catch inside a loop

百般思念 提交于 2019-12-13 09:35:47
问题 In the below code, I ask the user to give an integer input and if the input is 0 or a negative number, it loops again until the positive number is given. The thing is that if the users presses a letter, my code crashes and despite the fact that I used try-catch in a lot of ways nothing really worked. Any ideas? I used try-catch inside the loop, but it only worked for one letter input and not correctly. System.out.print("Enter the number of people: "); numberOfPeople = input.nextInt(); while

How to get the sum of an Integer arraylist?

风格不统一 提交于 2019-12-13 08:57:09
问题 Basically Im trying to make a program that allows a teacher to input grades for a test for each student then after they've inputted the grades it gives the teacher a sum of all the grades they inputted public static void grades(){ List<Integer> grade = new ArrayList<Integer>(); int gradetotal = IntStream.of(grades).sum;/* sum */ int gradelistnumber = 1; int inputedgrade = 0; while(inputedgrade != -1){ System.out.println("Enter Grade for student " + gradelistnumber + " (1-50): "); inputedgrade

Joining both hashsets together

假装没事ソ 提交于 2019-12-13 08:32:46
问题 My aim is to create a database like code where i add planets and the year they are found, and once the user inputs planets it shows all the planets and the year they are found, so far i have created this. import java.util.HashSet; import java.util.Scanner; import java.lang.*; public class sky { public static void main(String args[]) { Scanner in = new Scanner(System.in); // HashSet declaration HashSet < String > planet = new HashSet < String > (); // Adding elements to the HashSet planet.add(

First, last and sometime middle name detection Java

女生的网名这么多〃 提交于 2019-12-13 08:25:31
问题 I am trying to write a code to give off the user's name in different formats after they enter it. However, if a user does not have a middle name, the system should print that there was an error. I have it so it works perfectly if the user enters three names but does not work if the user enters two names. Here is my code: import java.util.Scanner; public class Assignment3 { public static void main(String[] args) { String fullName; Scanner in = new Scanner(System.in); System.out.print ("What

Java : How to use scanner.hasNextLine without Ctrl+Z in a console program

亡梦爱人 提交于 2019-12-13 08:07:14
问题 Say I have the below code Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { line = scanner.nextLine(); //do something } And my input in the console is goes like this. Wayne Rooney Luis Nani Shinji Kagawa I want to read this line by line. But the problem is the method hasNextLine blocks waiting for the input after the third line as the input from the keyboard (System.in) never reaches EOF. Now, how do I reach EOF just by pressing enter key? because I don't want to tell

Create multiple arrays using a for loop

天涯浪子 提交于 2019-12-13 07:29:03
问题 I would like to make a program where the user can input the number of variables and fill every variable with certain values. For example, the User inputs that he/she wants to make 10 arrays, then the User inputs that the first array should have 5 elements and the User fills that array with values, then the User wants the second array to have 4 elements and does the same and so on. This is the code I was using, but it doesn't work: public static void main(String[] args){ Scanner s = new

java scan coordinates as (X, Y) format

白昼怎懂夜的黑 提交于 2019-12-13 06:29:02
问题 How can I scan coordinates with (x, y) format? For example: (1, 3) (2, 4) (4, 1) (3, 2) What I've tried is this: String m = input.next(); String parts[] = m.split(","); String part1 = parts[0]; String part2 = parts[1]; part1 = part1.substring(1, part1.length()); part2 = part2.substring(0, part2.length()-1); That code works for a coordinate with (x,y) format but doesn't work for (x, y) 回答1: You mentioned you could get it working with format (x,y) but not (x, y) with the space in between. I

Both next() and nextLine() not helping to store name with spacing

我与影子孤独终老i 提交于 2019-12-13 06:17:27
问题 I am currently using a Scanner to record the user input which is a String and print it out. If the user input is a single name such as Alan, it works fine. If I enter a name with spacing such as Alan Smith, it returns an error saying InputMisMatchException. I read around similar cases here and they advised to use nextLine() instead of next(). It made sense but that doesn't work for me either. When I use a nextLine(), it immediately skips the step where I enter the name and goes back to the

NullPointerException with Scanner

不羁岁月 提交于 2019-12-13 05:56:08
问题 I am getting a NullPointerException on the 5th line. I am not really sure why or how to fix it... public static Scanner getInputScanner(Scanner console){ Scanner inputFile = new Scanner(System.in); Scanner file = null; String userInputFile = null; while (file.equals(null)) { try { System.out.print("Enter input file: "); userInputFile = inputFile.nextLine(); file = new Scanner(new File(userInputFile)); } catch (FileNotFoundException e) { System.out.print(userInputFile + " (No such file or