New Posts  All Forums:Forum Nav:

A small Java problem

post #1 of 10
Thread Starter 
I've just started programming in Java and I am currently making a quiz for my group project at Uni, although I am having some small issues. Or more, I just don't know what I want to do.

I can best explain this using an example. I have menuChoice as in int and I am using the scanner class to give it a value. I then have a menu system using case statements and this has 3 cases. All which loads 3 different methods based in what value the user gives the int. 1, 2 or 3. I have also made it so that if they type -1000 or 1000 it will re direct them to this method using the default function in case statements.

What I want to do is make it so that if I type a or sdsdsadsad my program wont crash. But simply reload this method again. I want to be able make them enter only an int, or make it so that it will accept a char/string and from this reload the method. I think I can do this using hasNextInt() , next() and while/catch loops but I am unfamiliar with the latter.

Could anyone help?

I have attached the method I am talking about.

http://pastebin.com/xkUGgXqV
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
post #2 of 10
You're probably crashing from a NumberFormatException. I'm not familiar with your entire program (specifically, where the user input is coming from in that code example), but here is a simple example:
Code:

String userGeneratedInput = "badtext";
int parsedCode = -1;
try{
     Integer.parseInt(userGeneratedInput);
} catch(NumberFormatException e){
     // it is your duty to tell the user they messed up here
}

if(parsedCode==-1){
     // the user has done bad input
}
else{
     // insert your switch statement here
}

Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #3 of 10
Thread Starter 
http://pastebin.com/87g8ShWp

this is my entire program.

I shall check this now.



EDIT: It appears this is the exception which occurs to me.

I have pasted a section from the console in here so you can better understand my problem.

http://pastebin.com/iP5XYj6k
Edited by evilspongebob72 - 11/18/11 at 9:53am
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
post #4 of 10
Ah, I see what you're doing..

Scanner#nextInt() presumes that you have something parseable as an int next in the stream. (I'm surprised it doesn't throw a NumberFormatException, I guess they use that as a more generalized approach). What I would do is use the next() method to just get the raw string data
Code:
String userGeneratedInput = keyboard.next();
int parsedCode = -1;
try{
     parsedCode = Integer.parseInt(userGeneratedInput);
} catch(NumberFormatException e){
     // it is your duty to tell the user they messed up here
}

if(parsedCode==-1){
     // the user has done bad input
}
else{
     // insert your switch statement here
}
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #5 of 10
Thread Starter 
Do you mean use next() instead of next int or just paste your code in as I tried before?

Both did not seem to work. Still throwing errors up everywhere.

Is there any way I can make it so they cannot enter anything but an integer?
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
post #6 of 10
You can also nest that into a while loop, and continue to ask the user for input while the parseCode = -1
Title System
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 965 Gigabyte GA-MA785GMT-UD2H Sapphire HD 6850 1GB DDR3 Crucial Ballistix Tracers 4GB 
Hard DriveOptical DriveCoolingOS
Kingston SSD 64GB BENQ DVD-RW Custom W/C Kit Windows 7 Professional x64 
MonitorKeyboardPowerCase
24" LG LED Logitech Wave Corsair 700W Modular Coolmaster Half-X 
MouseAudio
Logitech MX Revolution 5.1 Surround / Studio Recording Headphones 
  hide details  
Reply
Title System
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 965 Gigabyte GA-MA785GMT-UD2H Sapphire HD 6850 1GB DDR3 Crucial Ballistix Tracers 4GB 
Hard DriveOptical DriveCoolingOS
Kingston SSD 64GB BENQ DVD-RW Custom W/C Kit Windows 7 Professional x64 
MonitorKeyboardPowerCase
24" LG LED Logitech Wave Corsair 700W Modular Coolmaster Half-X 
MouseAudio
Logitech MX Revolution 5.1 Surround / Studio Recording Headphones 
  hide details  
Reply
post #7 of 10
The last snippet I posted is closer to something you could drop in but it probably needs a bit of rewiring still.

But yes, the major point is to use Scanner#next() and run that through Integer.parseInt()
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #8 of 10
Thread Starter 
Code:
do
            {
                try
                {
                    System.out.println("Enter your number: ");
                    a = keyboard.nextInt();
                    validInput = true;
                }
                catch (InputMismatchException e)
                {
                    System.out.println("Must enter an integer!");
                    keyboard.next();   // discard the bad input
                    validInput = false;
                }
 
            }while(!validInput);

I just found this code somewhere and it looks like it could work as it uses the same exception I get. Although InputMismatchException is in red for me. Do I need to declare something before I can use it?
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
post #9 of 10
You'd need to import the InputMismatchException class.

The exception you get is just a byproduct of what scanner throws, as its trying to do Integer.parseInt() in that method. My guess, without looking in OpenJDK is that the nextInt() method looks like this:
Code:
public int nextInt() throws InputMismatchException{
     String token = next();
     try{
           return Integer.parseInt(token);
     } catch(NumberFormatException e){
          throw new InputMismatchException();
     }
}
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #10 of 10
Thread Starter 
Code:
public static void mobileGamesTrainingMenu()
    {
        System.out.println("Welcome to the Mobile Games Tutorial!");
        System.out.println("What difficultly level would you like help with?");
        System.out.println("1: Easy");
        System.out.println("2: Medium");
        System.out.println("3: Hard");
        System.out.println("4: Back to Mobile Games Menu\t\t5: Back to Topic List");
        System.out.println("If a non-valid choice is entered you will be returned to this menu.");
        System.out.print("What would you like to do? ");
        do
        {
            try
            {
                System.out.print("What would you like to do: ");
               menuChoice = keyboard.nextInt();
                validInput = true;
                System.out.println("");
            }//try
            catch (InputMismatchException e)
            {
                keyboard.next();   // discard the bad input
                validInput = false;
                System.out.println("");
                mobileGamesTrainingMenu();
            }//catch
         }while(!validInput);
        switch (menuChoice)
            {
                case 1:
                    mobileGamesTrainingEasy();
                    break;
                case 2:
                    mobileGamesTrainingMedium();
                    break;
                case 3:
                    mobileGamesTrainingHard();
                    break;
                case 4:
                    mobileGamesMenu();
                    break;
                case 5:
                    menuTopic();
                    break;
                default:
                    mobileGamesTrainingMenu();
            }//switch
        }//mobileGamesTrainingMenu

This seems to have worked. Thanks a lot guys smile.gif)
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
Meh...
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8400 | 3.8GHz | 1.31v GA-P35C-DS3R ATI Sapphire 4870 512MB 4GB OCZ Platinum XTC 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F1 320GB | Seagate 250GB Storage Pioneer DVD-RW DVR-216D Windows Seven x64 BenQ G2410HD 24" 1080p 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard Corsair TX Series 650W Antec Nine Hundred Two Logitech G5 Gaming Mouse 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming