I want the program to only allow integers at certain points. The code works but I have to enter a value twice then it skips a part of my code.

My code is

`

    case 1:

                char ans;

                do{

                    Event event = new Event();


                    out.println("Please Enter the name of event");
                    event.setEvent(input.next());
                    input.nextLine();


                do{
                    try{

                    out.println("Please Enter the price of this event\n");
                    event.setprice(input.nextDouble());



                    }catch (InputMismatchException e){
                        out.println("Please Enter the price of this event\n");

                    }   

                    input.nextLine();
                } while (! input.hasNextDouble());



                    out.println("Please Enter the date of this event\n");
                    event.setdate(input.next());




                    out.println("Please enter the amount of tickets available for this event.\n");
                    event.setavailability(input.nextInt());

                    out.println("Please enter the venu");
                    event.setvenue(input.next());

                    Events.add(event);

                    out.println("Would you like to add a new event?");

                    String answer = input.next();
                    ans = answer.charAt(0);


                }while (ans == 'y');



            out.println(Events.get(0));


                break;

the problem with this is it displays like this:

1
Please Enter the name
red
Please Enter the price of this event

40
40
Please Enter the date of this event

Please enter the amount of tickets available for this event.

40

It doesn't allow me to enter the date and I have to enter the price value twice

Any help and advice would be great.

Thanks

What might fix your problem is Integer.getInteger(String num);. This returns null if the String passed is not numeric.

Scanners are generally not a good idea, use a BufferedReader.

Yes, I agree. Scanners are supoposed to be easy, but in reality seem to cause a lot of problems. If you use a BufferedReader, and readLine() to read whole lines you can parse their contents yourself and be sure exactly what is happening.

PS A small typo from DoogleDude there - it's Integer.parseInt, not Integer.getInteger. Integer.getInteger is for getting a System property given its name. And Integer.parseInt throws an Exception for bad input, it does not (cannot) return null.

My bad, I read around the java doc trying to find a solution, and getInteger was one that I thought would work better in this case, guess I should've read it better.

I have used parseInt and I didn't think it would be best for this application. I don't know why anymore haha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.