Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By jim829

Thread: coding problem

  1. #1
    0smile0 is offline Member
    Join Date
    Oct 2014
    Posts
    8
    Rep Power
    0

    Unhappy coding problem

    Hello everybody
    please help me with this code I want to know the errors so I can fix them.
    also if someone can fix it would be great .

    Java Code:
    public class Q5
    {
    public static void main(String args[]) {
    final int J=0;
    final int D=0;
    int i=5 , j ,x=8;
    switch (i==3){
    case D:x++;
    case j: x+=5;break;
    case 6.2: x--;
    case 0: x=0;
    case 1+5: ; break;
    case 0.5: --x;break;
    case 'a':  break;
    default: x*=2;
    
    
    }
    
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: coding problem

    You have quite a few problems and it would be much easier for you to read
    The switch Statement (The Java Tutorials > Learning the Java Language > Language Basics)
    than try to explain everthing here. Try and fix as much as you can, repost, and then someone can help you.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: coding problem

    Why don't you feed it to the compiler and read what it has to say about that crappy mess?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    0smile0 is offline Member
    Join Date
    Oct 2014
    Posts
    8
    Rep Power
    0

    Default Re: coding problem

    I did but every time I fix the errors nothing work
    may someone please fix it for me I need to hand it tomorrow unfortunately

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: coding problem

    Quote Originally Posted by 0smile0 View Post
    I did but every time I fix the errors nothing work
    may someone please fix it for me I need to hand it tomorrow unfortunately
    Well, the lesson for next time is: start earlier, because we don't do your homework here. You can show us the part(s) you're stuck with and we can help you out; but for now, you haven't shown us any effort on your part; i.e. all you did was asking us to do your homework.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    0smile0 is offline Member
    Join Date
    Oct 2014
    Posts
    8
    Rep Power
    0

    Default Re: coding problem

    Quote Originally Posted by JosAH View Post
    Well, the lesson for next time is: start earlier, because we don't do your homework here. You can show us the part(s) you're stuck with and we can help you out; but for now, you haven't shown us any effort on your part; i.e. all you did was asking us to do your homework.

    kind regards,

    Jos

    Q5.java:7: error: incompatible types: boolean cannot be converted to int
    switch (i==3){
    ^
    Q5.java:9: error: constant expression required
    case j: x+=5;break;
    ^
    Q5.java:11: error: duplicate case label
    case 0: x=0;
    ^
    3 errors

    These are the errors.
    my questions:
    1/ how I can convert boolean to int
    2/why the program refuse to run case j ?
    3/ I did not get the last error that case 0 is duplicate

    sorry for bothering I did not start late but this is new to me that's why I'm freaking out .
    I just want to know how to fix the errors without changing my code from the start because I'm sure about it .

    Best wishes and again sorry for asking to solve it for me.

  7. #7
    0smile0 is offline Member
    Join Date
    Oct 2014
    Posts
    8
    Rep Power
    0

    Default Re: coding problem

    Quote Originally Posted by jim829 View Post
    You have quite a few problems and it would be much easier for you to read
    The switch Statement (The Java Tutorials > Learning the Java Language > Language Basics)
    than try to explain everthing here. Try and fix as much as you can, repost, and then someone can help you.

    Regards,
    Jim
    Thank you for sharing this link it did help me alot.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: coding problem

    Quote Originally Posted by 0smile0 View Post
    Q5.java:7: error: incompatible types: boolean cannot be converted to int
    switch (i==3){
    ^
    Q5.java:9: error: constant expression required
    case j: x+=5;break;
    ^
    Q5.java:11: error: duplicate case label
    case 0: x=0;
    ^
    3 errors

    These are the errors.
    my questions:
    1/ how I can convert boolean to int
    2/why the program refuse to run case j ?
    3/ I did not get the last error that case 0 is duplicate

    sorry for bothering I did not start late but this is new to me that's why I'm freaking out .
    I just want to know how to fix the errors without changing my code from the start because I'm sure about it .

    Best wishes and again sorry for asking to solve it for me.
    1) you can't switch on a boolean; you need an int expression at 'switch( ... )';
    2) by definition 'x' has to be a constant value in 'case x:';
    3) each and every case clause has to be unique.

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: coding problem

    As I look at this snippet of code, I see there are lots of various "possible" ways of constructing a switch statement. Some
    are valid and others are not. Was this given to you as homework to identify what is legal and what isn't? I ask because
    I can see no reason why anyone would try all of these things unless it was part of a test or homework assignment.

    Regards,
    Jim
    Last edited by jim829; 10-18-2014 at 06:59 PM. Reason: typo
    Norm and DarrylBurke like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Coding problem
    By MW130 in forum New To Java
    Replies: 6
    Last Post: 01-10-2013, 04:59 PM
  2. The problem with the coding xml
    By torsion in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-05-2010, 04:52 AM
  3. Coding Problem
    By mfaulhaber in forum New To Java
    Replies: 2
    Last Post: 11-17-2009, 12:44 AM
  4. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 08:01 AM
  5. Problem in my coding
    By one198 in forum New To Java
    Replies: 9
    Last Post: 08-09-2007, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •