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 10 of 10

Thread: Im very new to java And switch statements

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Im very new to java And switch statements

    Hello everyone,

    Its my first time posting here and im very thankful for the work you guys do here

    Heres a java assignment i just received from my class and i think i did some of it right.
    Can someone please tell me why this wont compile properly.

    import java.io.*;
    class Question07{
     
    	public static void main(String args[]) throws IOException{
     
    	BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     
    	System.out.println("Enter GPA : ");
    	double gpa = Double.parseDouble(stdin.readLine());
     
     
     
    		if(gpa>=3.5){
    			int a = 1;
    		}else if(gpa>=3.49 & gpa >= 3.00){
    			int a = 2;
    		}else if(gpa>=2.99 & gpa>=2.5){
    			int a = 3;
    		}else if(gpa>=2.49 & gpa>=2.00){
    			int a = 4;
    		}else if(gpa<2.0){
    			int a = 5;
    		}else{
    			break;
    		}	
    			int x = a;
     
    			switch(x){
     
    			case (1):
    			System.out.println("First Class\n");
    			break;
     
    			case(2):
    			System.out.println("Upper second class\n");
    			break;
     
    			case(3):
    			System.out.println("Lower secodn class\n");
    			break;
     
    			case(4):
    			System.out.println("Pass\n");
    			break;
     
    			case(5):
    			System.out.println("Fail\n");
    			break;
     
    			default:
    			System.out.println("Invalid GPA\n");
     
     
    		}
     
    	}
     
    }

    Its good if someone can tell me what iv done wrong and how to correct it with good visual example.I just dont seem to have a good time with java

    Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Im very new to java And switch statements

    why this wont compile
    If you get compiler errors, please copy the full text of the error messages and paste it here.

    One problem I see is the variable a needs to be defined outside of the if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Im very new to java And switch statements

    This portion looks fudged up to me. You should revise it. First off your using a bit-wise operator to compare the expressions in your if statements. You should be using &&. Secondly, if gpa == 4.0, wouldn't all your else-if's expressions be true? You should format it so only one expression is true at a time.
                  int a = 0;  //Initialize outside of if-loop
                     if(gpa>=3.5){
    			a = 1;
    		}else if(gpa <= 3.49 && gpa >= 3.00){
    			a = 2;
    		}else if(gpa <=2.99 && gpa >= 2.5){
    			a = 3;
    		}else if(gpa <=2.49 && gpa >=2.00){
    			a = 4;
    		}else if(gpa < 2.0)
    			a = 5;
    		}else{
    			break;
    		}
                            switch(a){   //You don't need to assign it to another variable
                                            }

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Im very new to java And switch statements

    you declare the variable "a" globally or outside of the loop. Now you can run that code.

    The problem is variable "a" is declared inside of the loop every time .

    So the variable is not handle the values in out of the loop.

    So it's got error in the line of this int x = a;
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Im very new to java And switch statements

    Also revise where you are allowed to use the 'break' statement.

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Location
    Norway
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Im very new to java And switch statements

    Here is my take on it, hope it helps.

    - Moved int a outside the loop.

    - Removed the "break;" statement that was in the last else statement, and replaced it with a = -1;. I also added"(gpa<2.0 && gpa >= 0)" in the statement where a = 5 to enable to last else to get valid when you input a number below 0. This way, the last "default" switch will let you know you inputted an invalid number.

    I must admit I am new to java as well, but I have found that trying to help other newbies helps me as well



    import java.io.*;
    class test{
     
    	public static void main(String args[]) throws IOException{
     
    	BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     
    	System.out.println("Enter GPA : ");
    	double gpa = Double.parseDouble(stdin.readLine());
     
    	int a;
     
    		if(gpa>=3.5){
    			a = 1;
    		}else if(gpa>=3.49 & gpa >= 3.00){
    			a = 2;
    		}else if(gpa>=2.99 & gpa>=2.5){
    			a = 3;
    		}else if(gpa>=2.49 & gpa>=2.00){
    			a = 4;
    		}else if(gpa<2.0 && gpa >= 0){
    			a = 5;
    		}else{
    			a = -1;
    		}	
    			int x = a;
     
    			switch(x){
     
    			case (1):
    			System.out.println("First Class\n");
    			break;
     
    			case(2):
    			System.out.println("Upper second class\n");
    			break;
     
    			case(3):
    			System.out.println("Lower secodn class\n");
    			break;
     
    			case(4):
    			System.out.println("Pass\n");
    			break;
     
    			case(5):
    			System.out.println("Fail\n");
    			break;
     
    			default:
    			System.out.println("Invalid GPA\n");
     
     
    		}
     
    	}
     
    }

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Im very new to java And switch statements

    Try this code this is work correctly..I hope this will be help to you..

    class Question07{
     
    	public static void main(String args[]) throws IOException{
     
    	BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     
      	System.out.println("Enter GPA : ");
    	double gpa = Double.parseDouble(stdin.readLine());
     
                    int a = 0 ;
     
    		if(gpa>=3.5){
    			 a = 1;
    		}else if(gpa>=3.49 & gpa >= 3.00){
    			 a = 2;
    		}else if(gpa>=2.99 & gpa>=2.5){
    			 a = 3;
    		}else if(gpa>=2.49 & gpa>=2.00){
    			 a = 4;
    		}else if(gpa<2.0){
    			 a = 5;
    		}else{
     
    		}	
    			int x = a;
     
    			switch(x){
     
    			case (1):
    			System.out.println("First Class\n");
    			break;
     
    			case(2):
    			System.out.println("Upper second class\n");
    			break;
     
    			case(3):
    			System.out.println("Lower secodn class\n");
    			break;
     
    			case(4):
    			System.out.println("Pass\n");
    			break;
     
    			case(5):
    			System.out.println("Fail\n");
    			break;
     
    			default:
    			System.out.println("Invalid GPA\n");
     
     
    		}
     
    	}
     
    }
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  8. #8
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Im very new to java And switch statements


  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Im very new to java And switch statements

    Firstly is a switch sequence necessary at all? --- Unless it's actually mentioned in the assignment? (can u post the wording please)

    Also, have you checked the program logic by entering some gpa's? Enter 2.1 and tell us the result.

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Re: Im very new to java And switch statements

    use this code, you got error bcoz:-
    1. you have to initialize the the variable
    ...
    Quote Originally Posted by RevChe View Post
    Hello everyone,

    Its my first time posting here and im very thankful for the work you guys do here

    Heres a java assignment i just received from my class and i think i did some of it right.
    Can someone please tell me why this wont compile properly.

    import java.io.*;
    class Question07{
     
    	public static void main(String args[]) throws IOException{
     
    	BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
     
    	System.out.println("Enter GPA : ");
    	double gpa = Double.parseDouble(stdin.readLine());
     
     
     
    		if(gpa>=3.5){
    			int a = 1;
    		}else if(gpa>=3.49 & gpa >= 3.00){
    			int a = 2;
    		}else if(gpa>=2.99 & gpa>=2.5){
    			int a = 3;
    		}else if(gpa>=2.49 & gpa>=2.00){
    			int a = 4;
    		}else if(gpa<2.0){
    			int a = 5;
    		}else{
    			break;
    		}	
    			int x = a;
     
    			switch(x){
     
    			case (1):
    			System.out.println("First Class\n");
    			break;
     
    			case(2):
    			System.out.println("Upper second class\n");
    			break;
     
    			case(3):
    			System.out.println("Lower secodn class\n");
    			break;
     
    			case(4):
    			System.out.println("Pass\n");
    			break;
     
    			case(5):
    			System.out.println("Fail\n");
    			break;
     
    			default:
    			System.out.println("Invalid GPA\n");
     
     
    		}
     
    	}
     
    }

    Its good if someone can tell me what iv done wrong and how to correct it with good visual example.I just dont seem to have a good time with java

    Thanks in advance
    Last edited by copeg; March 18th, 2013 at 02:27 PM. Reason: removed spoonfeeding.

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. IF and SWITCH Statements: How and When to Use Them
    By snowguy13 in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 11th, 2012, 10:46 AM
  3. Help with switch statements
    By suxen in forum Loops & Control Statements
    Replies: 4
    Last Post: February 15th, 2011, 04:55 AM
  4. Why Did You switch to Java?
    By Sisyphus in forum The Cafe
    Replies: 2
    Last Post: May 26th, 2010, 04:01 AM
  5. need help with JButton and switch statements
    By jjoubert in forum AWT / Java Swing
    Replies: 5
    Last Post: October 28th, 2009, 09:13 AM