0

Here is my CSV file:

11608030,12345 11608045,54321 

Here is my code:

package csvtest; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Test { private ArrayList<Long> account_number = new ArrayList<Long>(); private ArrayList<String> password = new ArrayList<String>(); public Test() throws FileNotFoundException { Scanner scanner = new Scanner(new File("E:\\account.csv")); scanner.useDelimiter(","); while(scanner.hasNext()){ this.account_number.add(Long.parseLong(scanner.next())); this.password.add(scanner.next()); } System.out.println(account_number); } } 

and in the main class there is only one command

Test test = new Test();

but when i run this code i got a message like this

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1371) at csvtest.Test.(Test.java:28) at csvtest.CSVTest.main(CSVTest.java:21) C:\Users\hp\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

whats wrong with my code?? need help!!! thanks in advance :)

3
  • 1
    what happens if scanner.next() is null when it is called a second time? Commented Feb 13, 2018 at 17:51
  • i dont know,i am fresher in java, Can u pls help me to modify my code? i want to read and store first number in account_name ArrayList and 2nd number in password ArrayList Commented Feb 13, 2018 at 17:55
  • You are checking hasNext once, which means you can only be sure of having one more element, but you always read 2 elements with next. If there is only one element, the second one will throw an exception. Commented Feb 13, 2018 at 18:12

2 Answers 2

4

You should use scanner.useDelimiter(",|\\n"); The issue that you originally had was that the second scanner.next() read in "12345\n11608045" since you didn't specify that a newline could be a delimiter as well. So when scanner.next() was called the last time there wasn't anything to read from since that second call to next() read two of your values.

Sign up to request clarification or add additional context in comments.

Comments

1

scanner.next() gets you a line from the file, you then need to split that line into your comma seperated values:

See example:

 @Test void parseAccountsCsvFile() throws FileNotFoundException { ArrayList<Long> account_number = new ArrayList<Long>(); ArrayList<String> password = new ArrayList<String>(); Scanner scanner = new Scanner(new File("accountsCsvTest.txt")); while(scanner.hasNext()){ String scannerLine = scanner.next(); String[] values = scannerLine.split(","); account_number.add(Long.valueOf(values[0])); password.add(values[1]); } System.out.println(account_number); System.out.println(password); scanner.close(); } } 

Output:

[11608030, 11608045]

[12345, 54321]

I think that is what you are looking for.

4 Comments

This won't compile. The bal() function is missing a return type. ( In the original question as well)
Well spotted, i didn't the other lines... have updated bal to void, since it seems to set instance variables.
@AlanBlyth , values[0] is String type, it should be first parsed into Long before adding into account_number list.
Good point, please forgive my laziness, I made penance by actually creating the csv file and running the code completely to ensure it is complete and correct. Updated Answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.