About 373,000 results
Open links in new tab
  1. java - How can I get this switch statement to work using a scanner ...

    Sep 6, 2013 · You can better create a Map<Character, String> to save yourself from writing 26 cases in switch. This way you just have to get the String for a particular character. Map<Character, String> mapping = new HashMap<Character, String>(); mapping.put('a', "Alpha"); mapping.put('b', "Beta"); ..

  2. java - Scanner problems in switch statement - Stack Overflow

    Mar 3, 2018 · Calling player.nextLine() and assigning it to your static variable choice causes the problem. Static variables are retrieved when the class is first called on, which in your case means before the main method is called.

  3. How to fix Switch-case with String and Scanner in Java

    This tutorial shows you how to fix Switch-case with String and Scanner in Java. Answer In Java, the switch statement traditionally works with integer values or enumerated types, and it doesn't support using strings directly in the switch expression prior to Java 7.

  4. [Solved] java | Problems with switch and String - holadevs.com

    The problem you are experiencing is due to the scanner's buffer When you use nextInt(), this method does not consume the new line (Enter) that is entered when pressing Enter. Therefore, when you later call nextLine() , this call reads the pending new line …

  5. Java Switch - W3Schools

    Instead of writing many if..else statements, you can use the switch statement. The switch statement selects one of many code blocks to be executed: This is how it works: The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.

  6. java - scanner class and switch statments | DaniWeb

    Got the solution of your problem... Check this code: import java.io.*; import java.util.Scanner; public class Scan { public static void main(String[] args) throws IOException{ int value; System.out.println("Enter any number"); Scanner scan= new Scanner(System.in); value=scan.nextInt(); switch(value){ case 1:System.out.println("One"); break ...

  7. java - Scanner + switch use - Stack Overflow

    Sep 11, 2018 · public static void ask() { Scanner sc = new Scanner(System.in); System.out.println("What do you want to do?"); while (sc.hasNext()) { int select = 0; if (sc.hasNextInt()) { select = sc.nextInt(); } switch (select) { case 1: System.out.println("call eat()"); break; case 2: System.out.println("call walk()"); break; case 3: System.out.println ...

  8. Resolving Input Issues with Java's Scanner Class – devgem.io

    Mar 10, 2025 · Explore why Java's Scanner class seems to skip string input after integers and learn how to handle this common issue effectively.

  9. in java programming use a scanner class along with switch case ...

    Sep 4, 2013 · You need to either use Scanner.hasNextInt() to test if the next token is an integer or catch and diagnose the exception that is thrown by Scanner.nextInt() when it can't read an int.

  10. Java String Switch Case Example

    May 16, 2019 · Java String Switch Case support was added in Java 7. Learn how to use strings in switch case statements, string switch case null and case insensitive check.

  11. Some results have been removed
Refresh