PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 8, 2022

[FIXED] How do I invalidate a char input in Java?

 November 08, 2022     invalidation, java, menu     No comments   

Issue

This is extremely simple, but I am new to the Java programming language and some guidance would be helpful. I am making a menu where the options are listed. However, I would love for the user to type a character as an option to get a text like the one here and return the user to the selection again.

I present errors by confusing the case with the default.

import java.util.Scanner;

public class selectMenu {

    public static void main(String[] args) { 
        System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
        
        int op = scanner.nextInt();
        
        switch(op) {
            
            case 1 -> System.out.println("TODAY MENU");
            
            case 2 -> System.out.println("TOMORROW MENU");
            
            case 3 ->
                if (option==char) {
                    System.out.println("This is an invalid option.");
                }
                
            
            default -> System.out.println("Opcion invalida.");
            
        }
}

Solution

You could try doing something like this:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
            System.out.println("1) showing today menu");
            System.out.println("2) showing tomorrow menu");
            String op = in.nextLine();

            switch(op)
            {
                case "1":
                        System.out.println("TODAY MENU");
                        break menu;
    
                case "2":
                        System.out.println("TOMORROW MENU");
                        break menu;
    
                default:
                        try
                        {
                            Integer.parseInt(op);
                            System.out.println("You typed an int but it isn't 1 or 2");
                        }
                        catch(NumberFormatException e)
                        {
                            if(op.length() > 1) System.out.println("You typed a String");
                            else System.out.println("You typed a char");
                        }
                        break;
            }
        }
    }
}

It takes the next line rather than nextInt() (which will throw an exception if it's not a number), checks if the answer is "1" or "2", then checks if it's a valid int ("3", "4", etc). If not, it checks the length, and tells you it's a char if the length is 1, otherwise a String.

If you don't care about what the user actually entered (char, String, etc), just that they didn't type an int, you could move the try-catch around the scanner input:

import java.util.Scanner;
public class selectMenu
{
    public static void main(String[] args)
    { 
        Scanner in = new Scanner(System.in);

        menu: while(true)
        {
            System.out.println("select your option:");
        System.out.println("1) showing today menu");
        System.out.println("2) showing tomorrow menu");
            try
            {
                int op = in.nextInt();
    
            switch(op)
                {
              case 1:
                        System.out.println("TODAY MENU");
                        break menu;
        
                case 2:
                        System.out.println("TOMORROW MENU");
                        break menu;
        
              default:
                        System.out.println("int input, but not 1 or 2");
            }
            }
            catch(java.util.InputMismatchException e)
            {
                System.out.println("non-int input");
            }
            in.nextLine();
        }
    }
}

Sorry if the indenting is a bit off, I don't know why that was happening.



Answered By - funny username
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing