Jump to content

Java switch statement

Go to solution Solved by kwljunky,
snip

Character.toLowerCase()

Or Character.toUpperCase() ? 

 

Here is the details of the Character class in the java doc: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html

 

so: 

 

/** * Write a description of class Practice here. *  * @author (your name)  * @[member='versions'] (a version number or a date) */import java.util.Scanner;public class Practice{    public static void main(String[]args)    {        Scanner scan = new Scanner(System.in);        char letter;        System.out.print("enter a letter you idiot");        letter = scan.next().charAt(0);        myABCs(letter);    }        public static void myABCs(char letter)    { letter = Character.toLowerCase(letter); //before the switch statement as the char is passed into the method          switch(letter)        {            case 'a':                System.out.print("apple");                break;            case 'b':                System.out.print("batt");                break;            case 'c':                System.out.print("cat");                break;        }    }               }
/** * Write a description of class Practice here. *  * @author (your name)  * @[member=versions] (a version number or a date) */import java.util.Scanner;public class Practice{    public static void main(String[]args)    {        Scanner scan = new Scanner(System.in);        char letter;        System.out.print("enter a letter you idiot");        letter = scan.next().charAt(0);        myABCs(letter);    }        public static void myABCs(char letter)    {        switch(letter)        {            case 'a':                System.out.print("apple");                break;            case 'b':                System.out.print("batt");                break;            case 'c':                System.out.print("cat");                break;        }    }               }

I'm doing some practice for a test. How do I accept upper case letters as well as lower case without adding another case to my switch statement?

Link to comment
https://linustechtips.com/topic/263845-java-switch-statement/
Share on other sites

Link to post
Share on other sites

I'm doing some practice for a test. How do I accept upper case letters as well as lower case without adding another case to my switch statement?

Letter.toUpperCase(); //capitalizes all the letters

Edit: whoops, letter isn't a string, and toUpperCase() doesn't work on chars (i think).

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/263845-java-switch-statement/#findComment-3588501
Share on other sites

Link to post
Share on other sites

Why dont use if?

 

When doing multiple comparisons like this a switch statement works out to be cleaner

Character.toUpperCase(char);

Will convert a character to uppercase. If you convert any character you get to uppercase, then write your cases to only look at uppercase characters then it doesn't matter what case the characters you are given are.

Link to comment
https://linustechtips.com/topic/263845-java-switch-statement/#findComment-3588578
Share on other sites

Link to post
Share on other sites

snip

Character.toLowerCase()

Or Character.toUpperCase() ? 

 

Here is the details of the Character class in the java doc: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html

 

so: 

 

/** * Write a description of class Practice here. *  * @author (your name)  * @[member='versions'] (a version number or a date) */import java.util.Scanner;public class Practice{    public static void main(String[]args)    {        Scanner scan = new Scanner(System.in);        char letter;        System.out.print("enter a letter you idiot");        letter = scan.next().charAt(0);        myABCs(letter);    }        public static void myABCs(char letter)    { letter = Character.toLowerCase(letter); //before the switch statement as the char is passed into the method          switch(letter)        {            case 'a':                System.out.print("apple");                break;            case 'b':                System.out.print("batt");                break;            case 'c':                System.out.print("cat");                break;        }    }               }

"Use the force Harry" 

                   -Gandalf

Link to comment
https://linustechtips.com/topic/263845-java-switch-statement/#findComment-3588600
Share on other sites

Link to post
Share on other sites

How would you do this one? I know how to print out random numbers, but not within a range. How would you do that?

 Write a short class to print out 5 randomly-generated numbers in the range 3-18 (inclusive).  The results should be different every time the program is run.

Link to comment
https://linustechtips.com/topic/263845-java-switch-statement/#findComment-3588940
Share on other sites

Link to post
Share on other sites

How would you do this one? I know how to print out random numbers, but not within a range. How would you do that?

 Write a short class to print out 5 randomly-generated numbers in the range 3-18 (inclusive).  The results should be different every time the program is run.

System.out.print((int)((Math.random()*16)+3))

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/263845-java-switch-statement/#findComment-3588949
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×