Java switch statement

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Java switch statement

Post by Reason7194 on Thu Jan 27, 2011 7:20 pm
([msg=52932]see Java switch statement[/msg])

Hello everyone, i am trying my hand at a switch statement in my java program. I am using the NetBeans IDE and when i write in the syntax for the switch statement, NetBeans tells me thought i have an error on the line. I honestly do not know what i have done wrong with switch statement. If anyone could give me advice on how to solve the problem that would be wonderful.

Switch statement:
Code: Select all
switch( choice = (char)System.in.read());


Here is the full code of my program ( that is not finished yet )
Code: Select all
/**
*
* @author Administrator
*/
import java.io.*;


public class Test {

public static void main(String[] args)throws IOException{
    char choice;

    System.out.println("Pick one of the following");
    System.out.println("A for Apple");
    System.out.println("B for Bapple");
    System.out.println("C for Capple");
    System.out.flush();

    try{
        switch( choice = (char)System.in.read());
            {
            case 'A': System.out.println("You have picked Apple");

            break;

            case 'B': System.out.println("You have picked Bapple");

            break;

            case 'C': System.out.println("You have picked Capple");

            break;
       
       
       catch(IOException){
           System.out.println("An IO exception has been called");
           
              }
            }
           }
         }
       }
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Java switch statement

Post by Assassian360 on Thu Jan 27, 2011 7:49 pm
([msg=52934]see Re: Java switch statement[/msg])

Remove the semicolon (;) from the end of that line.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Java switch statement

Post by Reason7194 on Thu Jan 27, 2011 7:55 pm
([msg=52936]see Re: Java switch statement[/msg])

Thanks, that was a really simple error..i should have saw that.

Now my problem is that it is saying my try-catch is wrong. Why?

Updated project syntax:
Code: Select all
import java.io.*;


public class Test {

public static void main(String[] args)throws IOException{
    char choice;

    System.out.println("Pick one of the following");
    System.out.println("A for Apple");
    System.out.println("B for Bapple");
    System.out.println("C for Capple");
    System.out.flush();

    try{
        switch( choice = (char)System.in.read())
            {
            case 'A': System.out.println("You have picked Apple");

            break;

            case 'B': System.out.println("You have picked Bapple");

            break;

            case 'C': System.out.println("You have picked Capple");

            break;

        defult: System.out.println("Invalid");
       
       catch(IOException){
           System.out.println("Invaled"));

       }
       }
        }

        }
}
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Java switch statement

Post by sanddbox on Thu Jan 27, 2011 8:33 pm
([msg=52937]see Re: Java switch statement[/msg])

It was a mix between your curly braces being screwed up, a lot of misspellings (it's default, not defult, for example), and you not giving a name to your IOException object. Here you go:

Code: Select all
package helpnoobagain;

import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {
        char choice;

        System.out.println("Pick one of the following");
        System.out.println("A for Apple");
        System.out.println("B for Bapple");
        System.out.println("C for Capple");
        System.out.flush();
        try {
            switch (choice = (char) System.in.read()) {
            case 'A':
                System.out.println("You have picked Apple");

                break;

            case 'B':
                System.out.println("You have picked Bapple");

                break;

            case 'C':
                System.out.println("You have picked Capple");

                break;

            default:
                System.out.println("Invalid");
            }
        } catch (IOException e) {
            System.out.println("Invalid");
        }
    }
}


EDIT: Ignore the package statement :)
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2344
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Java switch statement

Post by Reason7194 on Thu Jan 27, 2011 8:42 pm
([msg=52941]see Re: Java switch statement[/msg])

Ah, thanks. Question: Why must i set the IOException to a character?
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Java switch statement

Post by sanddbox on Thu Jan 27, 2011 8:51 pm
([msg=52942]see Re: Java switch statement[/msg])

Reason7194 wrote:Ah, thanks. Question: Why must i set the IOException to a character?


Remember that in Java, everything is an object. IOException is...well, no exception. You're asking for an IOException object to be passed to your catch block, so you're going to need to give it a name, just as you name any other variable.

Here's an example that actually uses the IOException object:

Code: Select all
catch (IOException e) {
            e.printStackTrace();
        }


Remember, "e" is just the name we gave the IOException object; it could be anything.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2344
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Java switch statement

Post by Reason7194 on Thu Jan 27, 2011 9:37 pm
([msg=52944]see Re: Java switch statement[/msg])

O ok, thank you. I understand now. Silly mistake i had made then.

-Thanks everyone
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: Java switch statement

Post by yourmysin on Mon Jan 31, 2011 9:57 am
([msg=53036]see Re: Java switch statement[/msg])

sanddbox wrote:
Reason7194 wrote:Ah, thanks. Question: Why must i set the IOException to a character?


Remember that in Java, everything is an object. IOException is...well, no exception.


I see what you did there ;)
A+, Network+, MCTS(70-620), Security+, CCNA
yourmysin
Experienced User
Experienced User
 
Posts: 83
Joined: Mon Apr 21, 2008 9:02 pm
Location: Newport, Maine, USA
Blog: View Blog (0)


Re: Java switch statement

Post by MarkIntelhunter99 on Sat Apr 07, 2012 4:44 pm
([msg=65536]see Re: Java switch statement[/msg])

I checked out your source code i found some files that are needed not for the program.

here's the debugged one and at the same time, i was having fun improving it a little bit.^_^.
Code: Select all
/**
*
* @author Administrator
*/
import java.io.*;


public class Test2 {

public static void main(String[] args)throws IOException{
    char choice;
   boolean test = false;
   do{
    System.out.println("Pick one of the following");
    System.out.println("A for Apple");
    System.out.println("B for Bapple");
    System.out.println("C for Capple");
    System.out.flush();

        switch( choice = (char)System.in.read())
            {
            case 'A': System.out.println("You have picked Apple");
         test = true;
            break;

            case 'B': System.out.println("You have picked Bapple");
         test = true;
            break;

            case 'C': System.out.println("You have picked Capple");
         test = true;
            break;

            default: System.out.println("Error");
            break;
            }



       }while (test != true);
   }
}

MarkIntelhunter99
New User
New User
 
Posts: 7
Joined: Thu Apr 05, 2012 12:48 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests