Java Double Array

I'm having trouble setting up and placing values into an array using a text file containing the floating point numbers 2.1 and 4.3 each number is separated by a space - below is the error I'm getting:

Exception in thread "main" java.util.NoSuchElementException

import java.util.*;
import java.io.*;

public class DoubleArray {

    public static void main(String[] args) throws FileNotFoundException {   

        Scanner in = new Scanner(new FileReader("mytestnumbers.txt"));

        double [] nums = new double[2];

        for (int counter=0; counter < 2; counter++) {
            int index = 0;
            index++;
            nums[index] = in.nextDouble();
        }
    }
}

Thanks, I'm sure this isn't a hard question to answer... I appreciate your time.

Answers


You should always use hasNext*() method before calling next*() method

    for (int counter=0; counter < 2; counter++) {
       if(in.hasNextDouble(){ 
           nums[1] = in.nextDouble();
       }
    }

but I think you are not doing the right, I'd rather

    for (int counter=0; counter < 2; counter++) {
       if(in.hasNextDouble(){ 
           nums[counter] = in.nextDouble();
       }
    }

NoSuchElementException is thrown by nextDouble method @see javadoc


I would suggest printing the value of index out immediately before you use it; you should spot the problem pretty quickly.


It would appear you're not getting good values from your file.

Oli is also correct that you have a problem with your index, but I would try this to verify you're getting doubles from your file:

String s = in.next();
System.out.println("Got token '" + s + "'"); // is this a double??
double d = Double.parseDouble(s);

EDIT: I take this partly back...

You simply don't have tokens to get. Here's what next double would have given you for exceptions:

InputMismatchException - if the next token does not match the Float 
                         regular expression, or is out of range 
NoSuchElementException - if the input is exhausted 
IllegalStateException - if this scanner is closed

Need Your Help

Pyinstaller with pygame

python pygame pyinstaller

I have tried multiple times to use PyInstaller to bundle my Python2 programs, but it never seems to work with the Pygame module. I have seen many other issues on this topic, but I couldn't find any

Is HTML parsing (in Java/Android) then extracting data from it, an effective way of getting a webpage's content?

java android html-parsing data-extraction http-post

So, I'm using HTTP Post Requests in Android Java to log into a website, before extracting the entire HTML code. After that, I use Pattern/Matcher (regex) to find all the elements I need before extr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.