java.util.scanner

NullPointerException When Entering Data Into a Dynamic Array from Text File

这一生的挚爱 提交于 2019-12-20 06:20:52
问题 I am making a voting system where the user logs in with their student number and makes a selection. Once someone has voted, they cannot be able to log in again. I made an object; Students, containing a String for the student number and a boolean for whether or not that student number has already voted (both being private). I made a dynamic array of this type as to accept a given amount of students off of a text file that is read through the use of a Scanner. However, when I try to populate

java.util.NoSuchElementException in basic Scanner usage

老子叫甜甜 提交于 2019-12-20 05:26:45
问题 I want to take an input from user with using Scanner in my project. It gives same error so i created another project to try Scanner only but it still gives this error. I looked up for solved questions in stackoverflow but they're large codes than mine, so maybe NoSuchElementException caused by their codes. I mean isn't it weird in simple Scanner code? Simpler codes are using in Scanner tutorials. Am i doing little thing wrong? Here it is: package deneme; import java.util.Scanner; public class

Stumped by FileNotFound exception

假装没事ソ 提交于 2019-12-20 04:18:12
问题 All I want to do is import data from a text file. The file exists at C:\temp\text.txt. However, I keep getting a file not found error. What the heck am I doing wrong??? public static void main(String[] args) throws IOException { String mFilename = "C:\\temp\\text.txt"; Scanner myDataStream = new Scanner(new File(mFilename));} 回答1: For the sake of giving this question an answer: Googling brought up a case where someone had file extensions hidden and his file was actually text.txt.txt . Check

How to exit the program when Ctrl+D is entered in Java?

泄露秘密 提交于 2019-12-20 02:51:02
问题 Below is a section of my Reverse Polish Calculator. If an integer is entered, push it to the stack and, if = is pressed, peek the result. However, I want to add another condition: if CTRL + D is pressed by the user, the program exits. I've had a look online but can't seem to find any solutions. Any ideas? Thanks. Scanner mySc = new Scanner(System.in); //If input is an integer, push onto stack. if (mySc.hasNextInt()) { myStack.push(mySc.nextInt()); } //Else if the input is an operator or an

Java - Scanning comma delimited double and int values

假装没事ソ 提交于 2019-12-20 02:49:07
问题 I'm trying to use Java's Scanner class to scan in double and int values delimited by commas. The following Scanner input = new Scanner(System.in).useDelimiter("\\D"); can only scan int values separated by , . e.g. input = 1000,2,3 How do I scan in double and int values separated by , e.g. input = 1000.00,3.25,5 or 100.00,2,3.5 ? I tried the following but they don't seem to work: Scanner input = new Scanner(System.in).useDelimiter(","); Scanner input = new Scanner(System.in).useDelimiter("\\,"

reading from console with scanner in a loop for

99封情书 提交于 2019-12-20 02:17:09
问题 i have this method, and the idea is read from the console (keyboard) a sequence of int numbers and add all of them in an ArrayList, im using the class Scanner to read the numbers but in the for loop doesnt works, it throws "java.util.NoSuchElementException". public static int mayorNumberSecuence(){ System.out.println("Give me a size "); Scanner sn = new Scanner(System.in); int n = sn.nextInt(); sn.close(); ArrayList<Integer> list = new ArrayList<Integer>(); for (int i=0; i<= n; ++i){ System

reading from console with scanner in a loop for

自闭症网瘾萝莉.ら 提交于 2019-12-20 02:17:04
问题 i have this method, and the idea is read from the console (keyboard) a sequence of int numbers and add all of them in an ArrayList, im using the class Scanner to read the numbers but in the for loop doesnt works, it throws "java.util.NoSuchElementException". public static int mayorNumberSecuence(){ System.out.println("Give me a size "); Scanner sn = new Scanner(System.in); int n = sn.nextInt(); sn.close(); ArrayList<Integer> list = new ArrayList<Integer>(); for (int i=0; i<= n; ++i){ System

Scanner class java file not found

一笑奈何 提交于 2019-12-19 10:17:06
问题 Scanner Class couldnt find the file I use NetBeansIDE, and the test.txt is in the folder path: D:\netbeans project works\ReadFile\src\readfile\test.txt in the same folder the readfile.java exsist. the code is as below. It generates file not found. package readfile; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class ReadFile { public static void main(String[] args) throws IOException , FileNotFoundException { Scanner

Stop scanner from reading user input java?

强颜欢笑 提交于 2019-12-19 10:09:13
问题 I am trying to write this method, which keeps reading from the user, until the word "exit" is inputted. I tried with a break and for loop; it didn't work. I was trying with while, but it does not stop even when the word "exit" is inputted. Any ideas how to fix this? Thanks. public void read(Scanner scanner){ while(3<4){ String in = scanner.nextLine(); Scanner scanner_2 = new Scanner(in); String name = null; if(scanner_2.hasNext()){ //create and add the user to the user container class name =

Fast & Efficient Way To Read Large JSON Files Line By Line in Java

笑着哭i 提交于 2019-12-19 09:48:41
问题 I have 100 millions of records in JSON file, need an efficient and fastest method to read the array of arrays from a JSON file in java . JSON file look like: [["XYZ",...,"ABC"],["XYZ",...,"ABC"],["XYZ",...,"ABC"],...,["XYZ",...,"ABC"], ["XYZ",...,"ABC"],["XYZ",...,"ABC"],["XYZ",...,"ABC"],...,["XYZ",...,"ABC"], ... ... ... ,["XYZ",...,"ABC"],["XYZ",...,"ABC"],["XYZ",...,"ABC"]] I want to read this JSON file line by line as: read first: ["XYZ",...,"ABC"] then: ["XYZ",...,"ABC"] so on:' ... ...