-6
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class question2_Arrays {


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

    double[]loanbalanceArray = new double[500];
    int index = 0;

    File file = new File("loanbalance.txt");
    Scanner inputFile = new Scanner(file);

    while(inputFile.hasNext()&& index<loanbalanceArray.length){
        loanbalanceArray[index] = inputFile.nextInt();
        index ++;
        }
    inputFile.close();

I am trying to read the contents of the text file loanbalance into an array sized 500 this is what i have so far but when i execute the program it gives me this error "Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source)"

any suggestions where i could be going wrong?

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • 1
    http://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html: you're reading an int, and the file contains something that can't be parsed as an int. – JB Nizet Oct 18 '15 at 12:57
  • 1
    Even if I knew nothing about java I would read this text `InputMismatchException` and say well, it sounds like my input doesn't match what was expected. My code expects int from `inputFile.nextInt()` so I must not be giving an int. Then I check the input file. Did you do anything like this? – takendarkk Oct 18 '15 at 12:58
  • @takendarkk Who needs to read messages if you can ask other people to do the explaining for you ... – GhostCat Oct 18 '15 at 12:59
  • Also, why make your array of type `double` but then fill it with `int`? – takendarkk Oct 18 '15 at 12:59
  • sorry guys my mistake i put in nextint instead of nextDouble @takendarkk – user3607224 Oct 18 '15 at 13:03
  • but even when i change the hasnextInt to double it still gives me an error of type mismatch cannot convert boolean to double?@takendarkk – user3607224 Oct 18 '15 at 13:07
  • You checked if it has a next double, but did you change the part where you try to remove an int from the file? – takendarkk Oct 18 '15 at 13:10
  • this is what i have so far while(inputFile.hasNextDouble()|| index – user3607224 Oct 18 '15 at 13:13
  • And you are sure you have doubles in your input file and they are separated correctly? Also, your while condition should be `&&`, not `||`. Think about what would happen if your file had 1000 numbers. Your while loop would run which is not good. – takendarkk Oct 18 '15 at 13:15
  • yes there are doubles in my input file and they are on a seperate line @takendarkk – user3607224 Oct 18 '15 at 13:17

1 Answers1

0
while (inputFile.hasNextDouble() || index<loanbalanceArray.length){
    loanbalanceArray[index] = inputFile.nextDouble();
    index ++;
    }

Use inputFile.hasNextInt method. Your input can return true on hasNext call but it can be not int or null and throw exception during call of loanbalanceArray[index] = inputFile.nextDouble();

And according to your comments you're using loanbalanceArray[index] = inputFile.hasNextDouble() for assignment, but you should use loanbalanceArray[index] = inputFile.nextDouble()

hasNextDouble() checking if your input has double value further and returns Boolean is so but nextDouble() returns actual next Double value of input

Klever
  • 133
  • 8
  • That could still throw `InputMismatchException`. – takendarkk Oct 18 '15 at 13:08
  • @takendarkk topicStarter just using loanbalanceArray[index] = inputFile.**hasNextDouble()** instead .nextDouble for assignment – Klever Oct 18 '15 at 13:17
  • i did that the error doesnt appear anymore but when i try to display the contents of the array it gives me weird characters rather than the contents of the file @Klever – user3607224 Oct 18 '15 at 13:30
  • @user3607224 http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – JB Nizet Oct 18 '15 at 14:07
  • the link just shows how to print string and int arrays. i want to learn how to print the contents of an array that is double. any other links if you have any would be useful @user3607224 – user3607224 Oct 18 '15 at 14:15
  • @user3607224 same applicable for double. System.out.println(Arrays.toString(array)); In your case: System.out.println(Arrays.toString(loanbalanceArray)); – Klever Oct 18 '15 at 14:25