Results 1 to 14 of 14
Like Tree9Likes
  • 1 Post By JosAH
  • 1 Post By Norm
  • 1 Post By jim829
  • 1 Post By JosAH
  • 1 Post By JosAH
  • 1 Post By Norm
  • 1 Post By Tolls
  • 1 Post By DarrylBurke
  • 1 Post By Tolls

Thread: switch case doesn't work

  1. #1
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default switch case doesn't work

    hello.

    why switch doesn't work ???

    PHP Code:
    value = (String)Rs.getMetaData().getColumnLabel(counter);
                System.out.println(value+"1");
                counter++;
                //  }
                col.setOnEditCommit(new EventHandler<CellEditEvent<Book, String>>() {
     
                    @Override
                    public void handle(CellEditEvent<Book, String> t) {
                        // t.getRowValue().setIsbn.i + (t.getNewValue());
                        System.err.println("Entered");
                        switch (value) {
                            case "isbn":
                                t.getRowValue().setIsbn(t.getNewValue());
                                System.err.println("ISBN");
                                break;
                            case "title":
                                t.getRowValue().setTitle(t.getNewValue());
                                System.err.println("Title");
                                break;
                            case "author":
                                t.getRowValue().setAuthor(t.getNewValue());
                                break;
                            case "price":
                                t.getRowValue().setPrice(t.getNewValue());
                                break;
                            case "category":
                                t.getRowValue().setCategory(t.getNewValue());
                                break;
                            default:
                                System.err.println("nothing");
     
                        }

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

    Default Re: switch case doesn't work

    What does 'doesn't work' mean? Doesn't your code compile? Doesn't it run? Is an error thrown to you? Did your computer explode?

    kind regards,

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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: switch case doesn't work

    A suggestion: The message printed in the default case should include the variable: value so you can see what the computer saw when the switch statement was executed.
    M0TRIX likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: switch case doesn't work

    And in addition to what Norm said, surround the variable with delimiters (e.g. square brackets). Sometimes a string has leading and/or trailing white space.

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

  5. #5
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default Re: switch case doesn't work

    Quote Originally Posted by JosAH View Post
    What does 'doesn't work' mean? Doesn't your code compile? Doesn't it run? Is an error thrown to you? Did your computer explode?

    kind regards,

    Jos
    logical error.

    look at this line :

    PHP Code:
    value = (String)Rs.getMetaData().getColumnLabel(counter);
    System.out.println(value);
    counter++;
    the output is isbn
    the next output is title and ....

    kow look at swtich case part. :

    PHP Code:
                       switch (value) {
                            case "isbn":
                                t.getRowValue().setIsbn(t.getNewValue());
                                System.err.println("ISBN");
    the output is nothing. it should print ISBN and run this line of code :
    PHP Code:
    t.getRowValue().setIsbn(t.getNewValue());

  6. #6
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default Re: switch case doesn't work

    Quote Originally Posted by Norm View Post
    A suggestion: The message printed in the default case should include the variable: value so you can see what the computer saw when the switch statement was executed.
    even default part dosen't work!!!!!!!nothing prints there.!!!!

  7. #7
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default Re: switch case doesn't work

    Quote Originally Posted by jim829 View Post
    And in addition to what Norm said, surround the variable with delimiters (e.g. square brackets). Sometimes a string has leading and/or trailing white space.

    Regards,
    Jim
    PHP Code:
    case ("title"):
                                t.getRowValue().setTitle(t.getNewValue());
                                System.err.println("Title");
                                break;
                            case ("author"):
                                t.getRowValue().setAuthor(t.getNewValue());
                                break;
                            case ("price"):
                                t.getRowValue().setPrice(t.getNewValue());
                                break;
                            case ("category"):
                                t.getRowValue().setCategory(t.getNewValue());
                                break;
                            default:
                                System.err.println("nothing"+value);
    dosen't work too

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

    Default Re: switch case doesn't work

    Change this line:

    Java FX Code:
    System.out.println(value);
    ... to this:

    Java FX Code:
    System.out.println("["+value+"]");
    ... and see whether or not the output is '[isbn]' (and no leading or trailing spaces, nor newline characters are present in 'value').

    kind regards,

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

  9. #9
    M0TRIX is offline Member
    Join Date
    Dec 2013
    Posts
    28
    Rep Power
    0

    Default Re: switch case doesn't work

    tnx josh.

    i found the problem. look at all my code :

    PHP Code:
     while (Rs.next()) {
    
                col = new TableColumn(Rs.getMetaData().getColumnName(counter).toString());
                col.setCellValueFactory(new PropertyValueFactory(Rs.getMetaData()
                        .getColumnName(counter).trim()));
                Tblresult.getColumns().add(col);
    
                col.setCellFactory(TextFieldTableCell.forTableColumn());
                value = (String) Rs.getMetaData().getColumnLabel(counter++);
                System.err.println("before Entered" + value);
    
                col.setOnEditCommit(new EventHandler<CellEditEvent<Book, String>>() {
    
                    @Override
                    public void handle(CellEditEvent<Book, String> t) {
    
                        System.err.println("Entered" + value);
    
                        switch (value) {
                            case "isbn":
                                t.getRowValue().setIsbn(t.getNewValue());
                                System.err.println("ISBN");
                                break;
                            case ("title"):
                                t.getRowValue().setTitle(t.getNewValue());
                                System.err.println("Title");
                                break;
                            case ("author"):
                                t.getRowValue().setAuthor(t.getNewValue());
                                break;
                            case ("price"):
                                t.getRowValue().setPrice(t.getNewValue());
                                break;
                            case ("category"):
                                t.getRowValue().setCategory(t.getNewValue());
                                break;
                            default:
                                System.err.println("nothing" + value);
    
                        }
                        System.err.println("Exit");
    
                    }
    
                });
            }
    the output is :

    before Entered isbn
    before Entered title
    before Entered author
    before Entered price
    before Entered category

    Entered category

    Exit

    u got what i mean??or i explain more
    Last edited by M0TRIX; 01-25-2015 at 11:45 AM.

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

    Default Re: switch case doesn't work

    Quote Originally Posted by M0TRIX View Post
    u got what i mean??or i explain more
    Please do, because I don't understand your code at all; espcially the part where you install a new EventHandler in that loop puzzles me).

    kind regards,

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

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: switch case doesn't work

    Is the handle() method ever called? If it's not called the switch() statement will never be executed.

    EDIT: Moved to JavaFX section.
    Last edited by Norm; 01-25-2015 at 02:52 PM.
    M0TRIX likes this.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: switch case doesn't work

    How does that code compile?

    You're using the variable 'value' inside an inner class, and that variable isn't final.
    Has Java 8 changed that requirement?
    M0TRIX likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: switch case doesn't work

    Quote Originally Posted by Tolls View Post
    How does that code compile?

    You're using the variable 'value' inside an inner class, and that variable isn't final.
    Has Java 8 changed that requirement?
    Yup. A variable may be 'deemed' final if it is never reassigned.

    db
    M0TRIX likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: switch case doesn't work

    Ta.
    Not that I'll remember, of course!
    M0TRIX likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. If-Else vs Switch-Case
    By R03 in forum New To Java
    Replies: 9
    Last Post: 04-29-2014, 01:56 AM
  2. switch case help! :(
    By domtay in forum New To Java
    Replies: 12
    Last Post: 12-18-2013, 08:43 AM
  3. switch case help! :(
    By domtay in forum Advanced Java
    Replies: 1
    Last Post: 12-17-2013, 11:34 AM
  4. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  5. SashForm doesn't seem to work in this case
    By Rodrigo Braz in forum SWT / JFace
    Replies: 0
    Last Post: 03-22-2009, 10:39 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
  •