Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › java "InputMismatchException" error
New Posts  All Forums:Forum Nav:

java "InputMismatchException" error

post #1 of 6
Thread Starter 
I am attempting to write a program for my java class and I can't seem to get past this error. I assume it has something to do with how the program is reading the lines on my .txt file but I don't see a problem. Could some one please point me in the right direction.

Here is what I have:
Code:
import java.util.Scanner;   // Needed for Scanner class
import java.io.*;           // Needed for File class

public class AcctInfo {

        public static void main(String[]args) throws FileNotFoundException
         {
        
                File file = new File("AcctInfo.txt");
                Scanner inputFile = new Scanner(file);
                // declare variables
                int acctNum;
                String acctType;
                String custName;
                String custType;
                double balance;
                int monthlyFee = 0;
                
                // assign file lines
               while (inputFile.hasNext()){
                        
                    acctNum = inputFile.nextInt();
                    acctType = inputFile.nextLine();
                    custName = inputFile.nextLine();
                    custType = inputFile.nextLine();
                    balance = inputFile.nextDouble();

                        if (acctType.equals("Checking"))
                        { 
                                if (custType.equals("Value") || custType.equals("value") && + 
                                                balance < 1500)
                                        monthlyFee = 5;
                           else if (custType.equals("Advantage") || custType.equals("advantage") && +
                                                 balance < 1000)
                                                monthlyFee = 10;
                                else 
                                                monthlyFee = 30;
                        }
        System.out.println("Account #: " + acctNum);
        System.out.println("Account Type: " +acctType);
        System.out.println("Customer Name: " + custName);
        System.out.println("Customer Type: " + custType);
        System.out.println("Balance: " + balance);
        System.out.println("Monthly Fee: " + monthlyFee);
        
        }
                
        
}       
}

This is the AcctInfo.txt file I am reading from:
Code:
40239
Checking
Charles Xavier
premier
31000.35

20925
Savings
Scott Summers
Value
345.89

21354
Savings
Robert Drake
advantage
1235.23

45687
Checking
Warren Worthington
Advantage
675.98

43234
Checking
Henry McCoy
Premier
24576.76

21356
Savings
Jean Grey
Premier
12678.45

40123
Checking
Lorna Dane
value
1105.78

Anything will be helpful, thanks
Project Haswell.
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770k Asus Maximus VI Hero EVGA 980ti FTW G.Skill Trident X 2400Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 pro Samsung Spinpoint F3 1TB Raystorm Block RX360 Extreme, RS 240 
CoolingOSMonitorKeyboard
Pump D5 Vario  Windows 10 Pro Q-nix 1440p Logitech G-710 
PowerCaseMouse
Corsair 750w Thermaltake Core x9 Snow Edition Logitech G500 
  hide details  
Reply
Project Haswell.
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770k Asus Maximus VI Hero EVGA 980ti FTW G.Skill Trident X 2400Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 pro Samsung Spinpoint F3 1TB Raystorm Block RX360 Extreme, RS 240 
CoolingOSMonitorKeyboard
Pump D5 Vario  Windows 10 Pro Q-nix 1440p Logitech G-710 
PowerCaseMouse
Corsair 750w Thermaltake Core x9 Snow Edition Logitech G500 
  hide details  
Reply
post #2 of 6
Can you post the full stacktrace for the exception? My guess is it is either the double or the int.
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
post #3 of 6
Thread Starter 
Is this what you mean?
Code:
----jGRASP exec: java AcctInfo

Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:909)
        at java.util.Scanner.next(Scanner.java:1530)
        at java.util.Scanner.nextDouble(Scanner.java:2456)
        at AcctInfo.main(AcctInfo.java:32)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
Project Haswell.
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770k Asus Maximus VI Hero EVGA 980ti FTW G.Skill Trident X 2400Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 pro Samsung Spinpoint F3 1TB Raystorm Block RX360 Extreme, RS 240 
CoolingOSMonitorKeyboard
Pump D5 Vario  Windows 10 Pro Q-nix 1440p Logitech G-710 
PowerCaseMouse
Corsair 750w Thermaltake Core x9 Snow Edition Logitech G500 
  hide details  
Reply
Project Haswell.
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770k Asus Maximus VI Hero EVGA 980ti FTW G.Skill Trident X 2400Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 pro Samsung Spinpoint F3 1TB Raystorm Block RX360 Extreme, RS 240 
CoolingOSMonitorKeyboard
Pump D5 Vario  Windows 10 Pro Q-nix 1440p Logitech G-710 
PowerCaseMouse
Corsair 750w Thermaltake Core x9 Snow Edition Logitech G500 
  hide details  
Reply
post #4 of 6
Code:
 acctNum = inputFile.nextInt();
             acctType = inputFile.next();
             
             custName = inputFile.next();
             custName = custName.concat(" " + inputFile.next());
             
             custType = inputFile.next();
             balance = inputFile.nextDouble();

This is a hack way to fix it. I have never liked the Scanner library and dealing with data is much easier in XML or even comma separated. If you want to see what it is doing with your current code, just put a println printing out each variable as you call "nextInt" and "nextLine". Scanner doesn't parse them the way you think it would.
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
post #5 of 6
Thread Starter 
thanks I'll give it a try and see if that helps. I just need to make sure my output matches the format I had it set up for.
Project Haswell.
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770k Asus Maximus VI Hero EVGA 980ti FTW G.Skill Trident X 2400Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 pro Samsung Spinpoint F3 1TB Raystorm Block RX360 Extreme, RS 240 
CoolingOSMonitorKeyboard
Pump D5 Vario  Windows 10 Pro Q-nix 1440p Logitech G-710 
PowerCaseMouse
Corsair 750w Thermaltake Core x9 Snow Edition Logitech G500 
  hide details  
Reply
Project Haswell.
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770k Asus Maximus VI Hero EVGA 980ti FTW G.Skill Trident X 2400Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 pro Samsung Spinpoint F3 1TB Raystorm Block RX360 Extreme, RS 240 
CoolingOSMonitorKeyboard
Pump D5 Vario  Windows 10 Pro Q-nix 1440p Logitech G-710 
PowerCaseMouse
Corsair 750w Thermaltake Core x9 Snow Edition Logitech G500 
  hide details  
Reply
post #6 of 6
Don't use the same Scanner for reading line and int/double/next and you'll avoid this problem. Make a Scanner for reading all the lines in a file, and another Scanner to read the content of each line.
Hackintosh
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770K MSI Z77 MPower Asus GTX 670 DCII G.SKILL Ares 2x4GB 
Hard DriveCoolingOSOS
Samsung 830 SSD NZXT Havik 140 Mac OSX 10.8 Windows 8 Professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Q270 Logitech G510 Seasonic X750 Corsair 500R 
MouseAudio
Logitech G9x Logitech Z623 
  hide details  
Reply
Hackintosh
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770K MSI Z77 MPower Asus GTX 670 DCII G.SKILL Ares 2x4GB 
Hard DriveCoolingOSOS
Samsung 830 SSD NZXT Havik 140 Mac OSX 10.8 Windows 8 Professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Q270 Logitech G510 Seasonic X750 Corsair 500R 
MouseAudio
Logitech G9x Logitech Z623 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › java "InputMismatchException" error