Results 1 to 20 of 22
- 02-28-2010, 08:32 PM #1
haveing troubles with a parsting cod
i'm reading some values from JTextFields and converting them to int like this
the declaration part: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; }
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 messageJava Code:public JTextField c[][] = new JTextField[9][]; public int mat[][] = new int[9][9];
silence i'm trying to meditate:p
- 03-01-2010, 02:36 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
and the code you have now is...not working? if so, why? errors? what? Please be a little more specific.
- 03-01-2010, 07:51 AM #3
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
- 03-01-2010, 07:55 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I have never heard of "parsting" any kind of fish, much less "cod". What is "parsting", anyway?
- 03-01-2010, 08:04 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 03-01-2010, 08:12 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 03-01-2010, 08:13 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 03-01-2010, 08:17 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Yeah! Maybe it has a synapse problem.
- 03-01-2010, 08:20 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Synax! You tricky little rascal ...
- 03-01-2010, 08:39 AM #10
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
- 03-01-2010, 09:09 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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?
- 03-01-2010, 09:16 AM #12
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 messageJava 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); } }//classsilence i'm trying to meditate:p
- 03-01-2010, 09:20 AM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
When I you what said understand you I answer will.
- 03-01-2010, 09:26 AM #14
:o
put in the first textfield "a" then hit solve
it should gives the error message not the solve one this is the issuesilence i'm trying to meditate:p
- 03-01-2010, 10:01 AM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I am not going to compile and execute that code, until you at least attempt to implement that that I suggested.
- 03-01-2010, 11:01 AM #16
i have tried this before posting here but is not working
is like you suggested or at least this is what i sow into your postJava 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"); } }silence i'm trying to meditate:p
- 03-01-2010, 11:26 AM #17
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 03-01-2010, 11:26 AM #18
i have tried this before posting here but is not working
is like you suggested or at least this is what i sow into your postJava 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"); } }silence i'm trying to meditate:p
- 03-01-2010, 11:28 AM #19
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 03-01-2010, 11:37 AM #20
Similar Threads
-
Char troubles
By diggdude in forum New To JavaReplies: 2Last Post: 11-10-2009, 03:55 PM -
Integer troubles
By Tb0h in forum New To JavaReplies: 11Last Post: 08-13-2009, 04:56 PM -
Gif decoding/LZW troubles
By hellochar in forum Advanced JavaReplies: 2Last Post: 07-14-2009, 11:26 PM -
Image troubles
By Theodoreb in forum New To JavaReplies: 24Last Post: 07-14-2009, 12:41 AM -
subclass troubles
By xf021209 in forum New To JavaReplies: 12Last Post: 04-20-2009, 11:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks