Results 1 to 2 of 2
- 09-20-2012, 05:57 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 1
- Rep Power
- 0
Having trouble getting scanner to read double from .txt file InputMismatchException
Hey guys Im new here was having some trouble so i figured id try to come here for help as ive looked here quite a bit while using google to help fix problems im having
Anyway im trying to read information in from a text file and i keep getting InputMismatchException
My Main
ReaderJava Code:import java.io.IOException; import java.util.Scanner; public class Assignment1 { public static void main(String[] args) throws IOException { CustomerList list = new CustomerList(); String FileName; Scanner input = new Scanner(System.in); System.out.print("Please enter your file name: "); FileName = input.nextLine(); list.getCustomerList(FileName); while(1 == 1){ System.out.print("Please Enter a Customer Number:"); Scanner CNI = new Scanner(System.in); int CN = CNI.nextInt(); CustomerRecord rec = list.getCustomer(CN); String talk = rec.toString(); System.out.println(talk); } } }
another object with get/setJava Code:import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class CustomerList { private int count; private CustomerRecord[] data = new CustomerRecord[100]; public CustomerList(){ } public CustomerList(int count, CustomerRecord[] data ){ this.count = count; this.data = data; } public void getCustomerList(String FileName) throws IOException { count = 0; File custinfo = new File(FileName); String Fname; String Lname; int id; double Balance; try{ Scanner s = new Scanner(custinfo); while(s.hasNext()){ id = s.nextInt(); Fname = s.nextLine(); Lname = s.nextLine(); Balance = s.nextDouble(); CustomerRecord rec = new CustomerRecord(); System.out.println(""+id); rec.setCustomerNumber(id); rec.setFirstName(Fname); rec.setLastName(Lname); rec.setBalance(Balance); data[count] = rec; count++; } s.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public CustomerRecord getCustomer(int CustomerNumber) { int CN = CustomerNumber; for (int i = 0; i != count;) { CustomerRecord rec = data[i]; int CRN = rec.getCustomerNumber(); i++; if(CN == CRN); return rec; } return null; } }
Java Code:public class CustomerRecord { /**get/set Methods for each data attribute*/ /**a unique number assigned to each customer*/ private int CustomerNumber; /**the customer’s first name*/ private String FirstName; /**the customer’s last name*/ private String LastName; /**the customer’s balance*/ private double Balance; /**get/set Methods for each data attribute*/ public void setCustomerNumber(int CustomerNumber){ this.CustomerNumber = CustomerNumber; } public int getCustomerNumber(){ return CustomerNumber; } public void setFirstName(String FirstName){ this.FirstName = FirstName; } public String getFirstName(){ return FirstName; } public void setLastName(String LastName){ this.LastName = LastName; } public String getLastName(){ return LastName; } public void setBalance(double Balance){ this.Balance = Balance ; } public double getBalance(){ return Balance; } public String toString(){ return ("Customer Number:" + CustomerNumber + "\nFirst Name:" + FirstName + "\nLast Name:" + LastName + "\nBalance:" + Balance); } }
what i was using in my test file was this
1 Jake Sanders 99999.99
2 Tammy Partin 111.10
3 Croc Rock 21213.00
4 Hoover Damn 12131.11.00
5 SixToes Pete 231321.00
7 One-Eyed Willy 12324.00
8 Big Kahuna 12324.00
9 Ben Dover 12324.00
10 Another Beer 34253.00
11 Im Almost 532525.00
12 Done Typing 6464.00
13 These Random 5647.00
14 Names of 47786.00
15 People cuz 347457.00
16 Im Out 575858.00
17 Of Names 88888.00
sorry if i over posted more than i need and thanks for the help
- 09-20-2012, 09:54 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Having trouble getting scanner to read double from .txt file InputMismatchExcepti
Stick some debugging in that loop and print out, after every nextXXX() call, what the Scanner has read in.
That sequence of calls is not doing what you think it is.Please do not ask for code as refusal often offends.
Similar Threads
-
Read text file with scanner class.
By frente158 in forum New To JavaReplies: 3Last Post: 03-11-2012, 01:58 PM -
read only double input from text file
By napi1234 in forum New To JavaReplies: 6Last Post: 06-28-2010, 04:06 PM -
Using Read/Write From File Using Scanner
By javaisntcoffee123 in forum New To JavaReplies: 4Last Post: 04-15-2010, 03:35 AM -
[SOLVED] Trouble with Scanner FileInputStream not finding the file
By miss.meli in forum New To JavaReplies: 0Last Post: 12-01-2008, 09:50 PM -
Use Scanner to read various types of data from a file
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks