0

I'm able to read the string variables, but it won't read the double for some reason. What can I do to make it read the double?

public class RundraiserApp { /** * @param args * */ public static void main(String[] args) { Fundraising[] dList = new Fundraising[10]; String name = null; String address = null; String cityStateZip = null; double donation = 0; int i = 0, ctr = 0; Scanner in; File file = new File("Donations.txt"); try { in = new Scanner(file); while (in.hasNext() && i < dList.length) { name = in.nextLine(); address = in.nextLine(); cityStateZip = in.nextLine(); donation = in.nextDouble(); i++; } ctr++; } catch (FileNotFoundException e1) { e1.printStackTrace(); } } } 
4
  • Could you give example of input file? Commented Mar 28, 2013 at 21:24
  • 1
    Try reading it as String and then convert it to double using Double#parseDouble Commented Mar 28, 2013 at 21:24
  • 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.nextDouble(Unknown Source) at RundraiserApp.main(RundraiserApp.java:51) Commented Mar 28, 2013 at 21:37
  • @user2073662 If you add your input file structure then it will be easy to help you out. Also dont put errors in comment. You can edit your question and add that there. Commented Mar 28, 2013 at 21:40

1 Answer 1

1

Considering your file structure below

Name Address Zip 2000.50 

Change donation like.

donation = Double.parseDouble(in.nextLine()); 
Sign up to request clarification or add additional context in comments.

2 Comments

Double.parseDouble(in.nextLine()); (lower case p in parseDouble)
@Taylor Thanks typo fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.