1 2 Previous Next 15 Replies Latest reply: Apr 17, 2007 3:09 PM by 807599

Using a range to input numbers to a table

Hi i have written this bit of code to allow numbers to be written to the table. I use "b" as the limit the numbers go to but when i want to use "a" as the start point it returns the error loss of precision. E.g. i enter a = 3 and b = 8 and in the table it returns 3, 4, 5, 6, 7, 8 in a single column. The error message reads "Loss of possible precision" and

found : double
required: int
for (int row = a; row <= b; row++)
1 error
BUILD FAILED (total time: 0 seconds)

double a = Double.parseDouble(txtRangeFrom.getText());
double b = Double.parseDouble(txtRangeTo.getText());
int i = 1;
for (int row = a; row <= b; row++)
for (int col = 1; col <= 1; col++){
tblOut.setValueAt(Double.toString(i),row,col);
i++;
}
}
Thanks any help would be appreciated
• 1. Re: Using a range to input numbers to a table
Get rid of the doubles -- unless you need input like 3.1415:
int a = Integer.parseInt(txtRangeFrom.getText());
• 2. Re: Using a range to input numbers to a table
Hi i have written this bit of code to allow numbers
to be written to the table. I use "b" as the limit
the numbers go to but when i want to use "a" as the
start point it returns the error loss of precision.
E.g. i enter a = 3 and b = 8 and in the table it
returns 3, 4, 5, 6, 7, 8 in a single column. The
error message reads "Loss of possible precision" and

found : double
required: int
for (int row = a; row <= b; row++)
ror
BUILD FAILED (total time: 0 seconds)

double a =
Double.parseDouble(txtRangeFrom.getText());
double b =
Double.parseDouble(txtRangeTo.getText());
int i = 1;
for (int row = a; row <= b; row++)
for (int col = 1; col <= 1; col++){
tblOut.setValueAt(Double.toString(i),row,col);
i++;
}
}
ode]

Thanks any help would be appreciated
row is an int and you're trying to compare it to b which is a Double.....
• 3. Re: Using a range to input numbers to a table
I need to be able to enter numbers with decimal points
• 4. Re: Using a range to input numbers to a table
I tried that but now i am getting the error " setValueAt(java.lang.Object,int,int) in javax.swing.JTable cannot be applied to (java.lang.String,double,int)"

tblOut.setValueAt(Double.toString(i),row,col);
1 error
BUILD FAILED (total time: 0 seconds)
• 5. Re: Using a range to input numbers to a table
Hint: get rid of all the doubles/Doubles. Why do you think you needed doubles?
• 6. Re: Using a range to input numbers to a table
Because i want to be able to enter numbers like 1.3424
• 7. Re: Using a range to input numbers to a table
Now I'm confused. Could you post the current version of your code?
• 8. Re: Using a range to input numbers to a table
double a = Double.parseDouble(txtRangeFrom.getText());
double b = Double.parseDouble(txtRangeTo.getText());
int i = 1;
for (double row = a; row <= b; row++)
for (int col = 1; col <= 1; col++){
tblOut.setValueAt(Double.toString(i),row,col);
i++;
}
}
• 9. Re: Using a range to input numbers to a table
Now why did you make row a double?
• 10. Re: Using a range to input numbers to a table
Because CSAngel said that, that was where the problem was. (above)
• 11. Re: Using a range to input numbers to a table
Because CSAngel said that, that was where the problem
was. (above)
CSAngel said the warning was because you were mixing ints and doubles.
There was no mention in that post of turning row into a double.

Let's start from the top: the rows in a tables are numbered 0, 1, 2, 3, 4, ...
Why do you want to work with doubles when specifying a range of rows?
• 12. Re: Using a range to input numbers to a table
Right i get what you mean now so the code reads

int a = Integer.parseInt(txtRangeFrom.getText());
int b = Integer.parseInt(txtRangeTo.getText());
int i = 1;
for (int row = a; row <= b; row++)
for (int col = 1; col <= 1; col++){
tblOut.setValueAt(Integer.toString(i),row,col);
i++;
}
}

When it compiles there are no errors. Works fine when the values are entered lik a=1 and b=6 the result is 1,2,3,4,5,6. But when i change a to 2 the output reads 1,1,2,3,4,5. Whereas i want it to go 2,3,4,5,6.
• 13. Re: Using a range to input numbers to a table
I suggest you create a new model for the input, since the number of rows may change.
Your code will look something like this.
int lo = 2;
int hi = 6;
DefaultTableModel model = new DefaultTableModel(hi-lo+1, 1);
for(int value = lo; value <= hi; ++value)
model.setValueAt(String.valueOf(value), value-lo, 0);
table.setModel(model);
DefaultTableModel is in package javax.swing.tablle.
• 14. Re: Using a range to input numbers to a table
Is there a way i can change the DefaultTableModel to just the name of my table e.g. tblOut
1 2 Previous Next