Results 1 to 2 of 2
  1. #1
    weighyoursins is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default 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
    Java 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);
            	
            }      
            
        }
    }
    Reader

    Java 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;
      }
      
      }
    another object with get/set
    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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default 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

  1. Read text file with scanner class.
    By frente158 in forum New To Java
    Replies: 3
    Last Post: 03-11-2012, 01:58 PM
  2. read only double input from text file
    By napi1234 in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 04:06 PM
  3. Using Read/Write From File Using Scanner
    By javaisntcoffee123 in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 03:35 AM
  4. Replies: 0
    Last Post: 12-01-2008, 09:50 PM
  5. Replies: 0
    Last Post: 04-17-2008, 07:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •