Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Convert from 'C' to "java" (switch)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Convert from 'C' to "java" (switch)

    we were doing this program in 'C' language and it compiled, runs, perfectly without any logical errors

    this is the 'C' Code:
     
    #include <stdio.h>
    void main()
     
    {
    int age;
     
    printf("\nEnter your age: ");
    scanf("%i", &age);
     
    	switch (age >= 18) {
     
    		case 1:
     
    			printf("You Can Vote");
    			break;
     
    		case 0:
     
    			prinft("You CANNOT Voe!");
    			break;
    	}
     
    }

    please try to run it in a "C" compiler, you will see the logic ..
    but for good sake I will state it ,

    the logic is :
    if any number that will be enterd is less than to 18 it will print ("You CANNOTt vote") but if the entered value is GREATER than or equal to 18 then it will print ("You can Vote")

    --every of part of this program (in 'C') from compiling through logic doesnt have any errors;


    but when i convert this program into java ..here it is

    this is the JAVA code:

    public class Exercise {
     
            private static BufferedReader br = new BufferedReader(new InputStreamReader(
                                               System.in));
     
            public static void main(String[] args) throws IOException {
     
                int age;
     
                System.out.print("Enter your age: ");
                age = Integer.parseInt(br.readLine());
     
                switch (age >= 18) {
     
                    case 1:
     
                        System.out.println("You Can Vote!");
                        break;
     
                    case 0:
     
                        System.out.println("You Cannot Vote!");
                        break;
     
                }
     
            }        
    }


    you can notice that in 'C' language,even if the entered value is integer it can return a value of a boolean ,
    where Case 1: is eqaul to TRUE if the entered value is greater than or equal to 18,
    where Case 0: is equal to FALSE if the entered value is less than 18,

    but in java,I have noticed that it CANNOT return a boolean value from an integer of a switch..

    why is it happening like that?... its perfectly running in 'C' but why it cant in java?..

    can anyone help me figure this out.. i really want to convert this in java... same logic... .


    please .. need help badly...


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Convert from 'C' to "java" (switch)

    Hello!

    First off, in Java you're not allowed to use booleans in switches. I guess this is because there is no point really, either its true or its false.

    So what you want to do is a simple if statement to check if age is greater than or equal to 18.

        if(age >= 18) {
            System.out.println("You Can Vote!");
        } else {
            System.out.println("You Cannot Vote!");
        }

    To read up on switch in Java have a look at The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    You will within time also notice that you cannot assign the integer 1 or 0 into a primitive boolean as Java will only accept true or false.

        boolean myBoolean = 1; // This will fail
     
        boolean myBoolean = true; // This is the correct syntax

    EnjoY!

    // Json
    Last edited by Json; October 6th, 2009 at 08:37 AM.

  3. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (October 6th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Convert from 'C' to "java" (switch)

    ahh so in 'java' the reserve word 'switch' can only accept numerical data types and as well as enums or enumerated data types,

    and between 'C' and 'java' i think this is where everything ends up, 'java' was made to make the logic more logical and far more easy to think.I even understand the simple logic of that program between the age,but the hardest part was thinking how to make it done in java in such a way that i did in 'C' language, so it is impossible to make that algorithm in java.. ryt sir?

    anyway the second code you posted? how was that related to the first?

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert from 'C' to "java" (switch)

    switch can only accept the following data types:

    byte, short, int, long (not positive about), and char.

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 6th, 2009)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Convert from 'C' to "java" (switch)

    another question,in 'C' ,base on the first program 1 and 0 is not only considered as number it is also considered as boolean.(in compiling) .while in java .1 and 0 is only numerical data type when it comes in compiling...
    do i make sense?.. hmm.....

  8. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Convert from 'C' to "java" (switch)

    Technically there is no such thing as a boolean in C. Operators which you believe retrn a boolean value such as == or > actually return a signed integer value. All operators that act on 'boolean' values within C follow the laws of, 0 for false, non-zero (this means ANY number) for true.

    in C99 (The latest version) a header "stdbool.h", which allows the use of 'bool' as a boolean data type, but again that follows the rule of 0 is false, non-zero is true. it also allows the use of true and false.

    int i = -10;
        while(i < 10){
            if(i) printf("Hello %d\n", i++);
        }

    proves that any non-zero number acts as true and zero acts as false. So switch in C only acts on integers, not booleans because boolean DO NOT EXSISTS in C!

    EDIT: And although C standards do not state you should use int main(), you SHOULD.
    void main() is the worst creation on this earth! If you ever posted void main() in C++ code, every C++ programmer would attempt to kill you

    Chris
    Last edited by Freaky Chris; October 6th, 2009 at 12:42 PM.
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  9. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Convert from 'C' to "java" (switch)

    What Chris said indeed and my second code block is relevant to just that, Java wont accept numbers into boolean.

    And also the switch statement will not take a long, it can take any number that is represented as an int or smaller so, byte, short, int and char as well as an enum.

    // Json

  10. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Convert from 'C' to "java" (switch)

    oh chris sorry for that ..regarding with 'void main()' im not too much familiar with 'C' , anway i understand a little bit that in "C".. '0' is considered as false and '>=1' i considerd as true...but it is different in java . so thats why i cant make it...
    Last edited by chronoz13; October 6th, 2009 at 09:23 PM.

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 04:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 02:38 AM
  3. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 11:46 PM
  4. Replies: 4
    Last Post: August 13th, 2009, 06:54 AM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 11:26 AM