Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default haveing troubles with a parsting cod

    i'm reading some values from JTextFields and converting them to int like this
    Java Code:
    for (int i = 0; i < 9; i++) 
        for (int j = 0; j < 9; j++) 
                   try {
                    mat[i][j] = Integer.parseInt(c[i][j].getText());
                   } catch (Exception e) {
                        mat[i][j] = 0;
                    }
    the declaration part:
    Java Code:
    public JTextField c[][] = new JTextField[9][];
    public int mat[][] = new int[9][9];
    basicly is ok but i'd like to print a message if someone puts string value on the textfield but the problem is that i want the parser to convert the empty string (when the user don;t put a value on) to 0 and if the user put a string value to get a error message
    silence i'm trying to meditate:p

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    and the code you have now is...not working? if so, why? errors? what? Please be a little more specific.

  3. #3
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    it's working but here is the thing:
    i have made a program to solve sudoku (ya i know there are planty of them on the internet but i have made the program for a school project and i used the constraint programing to solve it) and i decided to build an interface for it. this is the original cod when i didin;t check the values read from the interface. i decided to add a method that check the array to make sure it's a valid sudoku and if is not to print a message like "dude give the corect values" and stop the program form running it. it was easy to modify it and add the checks if the numbers are a sudoku the problem comes when someone enters a letter which is virtually ignored. and i want to print the same message with the incorect values for that case when someone enters a letter(or more of them )
    i have tryed with an if but is ignored becous of the case :confused:
    silence i'm trying to meditate:p

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I have never heard of "parsting" any kind of fish, much less "cod". What is "parsting", anyway?

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

    Default

    Quote Originally Posted by masijade View Post
    I have never heard of "parsting" any kind of fish, much less "cod". What is "parsting", anyway?
    Checking for synax errors?

    kind regards,

    Jos ;-)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    Checking for synax errors?

    kind regards,

    Jos ;-)
    Don't be silly. Fish (especially cod) doesn't have syntax errors.
    Sure you can use it to slap people now and again but checking syntax errors on fish is a waste of time.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by r035198x View Post
    Don't be silly. Fish (especially cod) doesn't have syntax errors.
    Sure you can use it to slap people now and again but checking syntax errors on fish is a waste of time.
    Who is talking about syntax errors? Parsting checks for synax errors! :-P

    kind regards,

    Jos

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yeah! Maybe it has a synapse problem.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Synax! You tricky little rascal ...

  10. #10
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    ok guys try to be helpfull it was a typo it's parseing not parsting somehow i misspelled the code too... :o
    Last edited by Dumisan; 03-01-2010 at 08:43 AM.
    silence i'm trying to meditate:p

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    In the try block add a if check to check if the length of the trimmed string is 0 (getText will return an empty string if nothing is entered, not null). And, in the catch block, add a JOptionPane message. Where's the problem?

  12. #12
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.border.EmptyBorder;
    
    public class sudoku extends JFrame {
    
        public JTextField c[][] = new JTextField[9][];
        JLabel message = new JLabel("                        ");
        JButton solve = new JButton("solve");
        JButton clear = new JButton("clear");
        JButton exit = new JButton("exit");
        public int mat[][] = new int[9][9];
    
        public sudoku() {
            this.setTitle("Sudoku");
            clear.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent evt) {
                    clearAP(evt);
                }
            });
            exit.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent evt) {
                    System.exit(0);
                }
            });
            solve.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent evt) {
                    solveAP(evt);
                }
            });
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            solve.setSize(50, 50);
            clear.setSize(50, 50);
            exit.setSize(50, 50);
            JPanel panou_centru = new JPanel();
            JPanel panou_sud = new JPanel();
            JPanel panou_nord = new JPanel();
            Container panou = this.getContentPane();
            panou.setLayout(new BorderLayout(0, 10));
            panou_centru.setLayout(new GridLayout(9, 9));
            panou_centru.setLayout(new GridLayout(3, 3, 5, 5));
            panou_centru.setBorder(new EmptyBorder(3, 3, 3, 3));
            panou_centru.setBackground(Color.black);
            JPanel[][] innerPanels = new JPanel[3][3];
            for (int i = 0; i < innerPanels.length; i++) {
                for (int j = 0; j < innerPanels[i].length; j++) {
                    innerPanels[i][j] = new JPanel(new GridLayout(3, 3, 2, 2));
                    panou_centru.add(innerPanels[i][j]);
                }
            }
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    c[i] = new JTextField[9];
                }
            }
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    c[i][j] = new JTextField("", 3);
                    innerPanels[i / 3][j / 3].add(c[i][j]);
                }
            }
            add("Center", panou_centru);
            panou_sud.setLayout(new FlowLayout());
            panou_sud.add(solve);
            panou_sud.add(clear);
            panou_sud.add(exit);
            add("South", panou_sud);
            panou_nord.add(message);
            add("North", panou_nord);
        }//constructor
    
        private void solveAP(ActionEvent evt) {
             for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                   try {
                    mat[i][j] = Integer.parseInt(c[i][j].getText());
                   } catch (Exception e) {
                        mat[i][j] = 0;
                    }
    
                }
            }
            int n = 9;
            if (test()) {
               message.setText("solverd");
            } else {
                message.setText("please insert a valid sudoku grid");
            }
        }//solveAP
    
        private void clearAP(ActionEvent evt) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    c[i][j].setText("");
                }
            }
            message.setText("                                ");
        }//clearAP
    
        private boolean test() {
            int n = 9;
            boolean ok = true;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    for (int k = j; k < n; k++) {
                        if (k != j) {
                            if (mat[i][j] != 0) {
                                if (mat[i][j] == mat[i][k]) {
                                    ok = false;
                                }
                            }
                        }
                    }
                }
            }
            for (int ci = 0; ci < n; ci += 3) {
                for (int cj = 0; cj < n; cj += 3) {
                    for (int i = ci; i < ci + 3; i++) {
                        for (int j = cj; j < cj + 3; j++) {
                            for (int k = ci; k < ci + 3; k++) {
                                for (int l = cj; l < cj + 3; l++) {
                                    if ((mat[i][j] != 0) & (mat[k][l] != 0)) {
                                        if (k != i || l != j) {
                                            if (mat[i][j] == mat[k][l]) {
                                                ok = false;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            for (int j = 0; j < n; j++) {
                for (int i = 0; i < n; i++) {
                    for (int k = 0; k < n; k++) {
                        if (k != i) {
                            if (mat[i][j] != 0) {
                                if (mat[i][j] == mat[k][j]) {
                                    ok = false;
                                }
                            }
                        }
                    }
                }
            }
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    if(mat[i][j]>9 || mat[i][j]<0) ok = false;
            return ok;
        }
    
         public static void main(String[] args) {
            sudoku sdk = new sudoku();
            sdk.setSize(300,380);
            sdk.setVisible(true);
    
          }
    }//class
    here i cut the solver part it will just print a solve message, is just the interface complete with a letter and you will receive the solve message, when that happens I want to appear the error message
    silence i'm trying to meditate:p

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    When I you what said understand you I answer will.

  14. #14
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    :o
    put in the first textfield "a" then hit solve
    it should gives the error message not the solve one this is the issue
    silence i'm trying to meditate:p

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I am not going to compile and execute that code, until you at least attempt to implement that that I suggested.

  16. #16
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    i have tried this before posting here but is not working
    Java Code:
    private void solveAP(ActionEvent evt) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
    
    
                    try {
                        if (c[i][j].toString().isEmpty()) {
                            mat[i][j] = 0;
                        } else {
                            mat[i][j] = Integer.parseInt(c[i][j].getText());
                        }
                    } catch (Exception e) {
                        message.setText("please insert a valid sudoku grid");
                    }
    
                }
            }
            int n = 9;
            if (test()) {
                message.setText("solverd");
            } else {
                message.setText("please insert a valid sudoku grid");
            }
        }
    is like you suggested or at least this is what i sow into your post
    silence i'm trying to meditate:p

  17. #17
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Okay, so what exactly, does "not working" mean?

    Exceptions? Compile messages? Difference between expected and actual results?

    Whichever it is, provide the full info here.

  18. #18
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    i have tried this before posting here but is not working
    Java Code:
    private void solveAP(ActionEvent evt) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
    
    
                    try {
                        if (c[i][j].toString().isEmpty()) {
                            mat[i][j] = 0;
                        } else {
                            mat[i][j] = Integer.parseInt(c[i][j].getText());
                        }
                    } catch (Exception e) {
                        message.setText("please insert a valid sudoku grid");
                    }
    
                }
            }
            int n = 9;
            if (test()) {
                message.setText("solverd");
            } else {
                message.setText("please insert a valid sudoku grid");
            }
        }
    is like you suggested or at least this is what i sow into your post
    silence i'm trying to meditate:p

  19. #19
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by masijade View Post
    Okay, so what exactly, does "not working" mean?

    Exceptions? Compile messages? Difference between expected and actual results?

    Whichever it is, provide the full info here.


    Okay, again.

  20. #20
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    sorry for the duplication

    if complemented by a point shows the solved message this means is not working
    silence i'm trying to meditate:p

Page 1 of 2 12 LastLast

Similar Threads

  1. Char troubles
    By diggdude in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 03:55 PM
  2. Integer troubles
    By Tb0h in forum New To Java
    Replies: 11
    Last Post: 08-13-2009, 04:56 PM
  3. Gif decoding/LZW troubles
    By hellochar in forum Advanced Java
    Replies: 2
    Last Post: 07-14-2009, 11:26 PM
  4. Image troubles
    By Theodoreb in forum New To Java
    Replies: 24
    Last Post: 07-14-2009, 12:41 AM
  5. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-20-2009, 11:46 PM

Posting Permissions

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