0

After the name, Charlie is printed out it throws the input mismatch exception and says I'm entering a string into the int scanner but it really is a number so why is it telling me it's a string?

This is my textfile that the scanner is reading from: Alice,1234567891,Priority Brandon,1987654321,Senior Charlie,7642874781,Regular Danny,5274847643,Senior

package file reading;

public class Customer {
    
    private String name;
    private int phoneNumber;
    private int seatNumber;
    String  classification;


    public Customer(String name, int phoneNumber, String classification) 
    {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.classification = classification;
    }



    public String getName() {return name;}
    public int getPhoneNumber() {return phoneNumber;}
    public int getSeatNumber() {return seatNumber;}
    public String getClassification() {return classification;}


    public void setName(String name) {this.name = name;}
    public void setPhoneNumber(int phoneNumber) {this.phoneNumber = phoneNumber;}
    public void setSeatNumber(int seatNumber) {this.seatNumber = seatNumber;}
    public void setClassification(String classification) {this.classification = classification;}


    public String toString()
    {
        return "Name: " + name + "\nPhone Number: " + phoneNumber +
        "   \nclassification: "  + classification +"\n";
    }
    
}



package file reading;

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

public class URLDissector
{
   //-----------------------------------------------------------------
   //  Reads customer info from a file and prints their path components.
   //-----------------------------------------------------------------
   public static void main(String[] args) throws IOException
   {
      String customerInfo;
      Scanner fileScan, infoScan;
      String name = "", classification = "";
      int phoneNumber = 0 ,seatNumber = 0;
   
      
      try {     
          fileScan = new Scanner(new File("info.txt"));

          while (fileScan.hasNext())
          {
             customerInfo = fileScan.nextLine();
             System.out.println("Customer: " + customerInfo);
    
             infoScan = new Scanner(customerInfo);
             infoScan.useDelimiter(",");
    
             //  Print each part of the customer info
             while (infoScan.hasNext()) {
                 name = infoScan.next();
                 //The error is hapening right here, right after printing out the name Charlie it won't 
                 //print the phone number and stops everything.
                 phoneNumber = infoScan.nextInt();
                 
                 classification = infoScan.next();

                 
                 Customer customer = new Customer(name, phoneNumber, classification);
                 System.out.println("Customer " + customer);
             }
                
    
             System.out.println();
          }
      }catch (FileNotFoundException f){
          System.out.println ("File not found");
      }
   }

Thank you for reading my question!

reyna
  • 21
  • 2
  • 1
    What is max value of `int`? How is it compared to 7_642_874_781? – Pshemo Nov 04 '20 at 04:02
  • 1
    @Pshemo Thank you for reminding me of this! the number for Charlie and Danny were too big for an int value. That was a simple fix thank you for your help! – reyna Nov 04 '20 at 04:09

1 Answers1

2

In Java, the integer datatype is 32-bit which has the maximum value of 2,147,483,647 but your input string is "7642874781" which will be converted to 7,642,874,781. That exceeds the maximum value of an integer data type, which causes the error.

Consider using long datatype with the method nextLong() of the scanner object.

Kinin Roza
  • 1,908
  • 1
  • 7
  • 17