| | |
If/Array Error
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 49
Reputation:
Solved Threads: 1
Ok in my program i'm trying to prevent a user from adding too many objects. i have three arrays called subs airs and dests, the user shouldn't be able to have more than a total of 10 objects between them.
in order to add an object to an array the user must press a JButton on my GUI
here is the action listener for one of these buttons
to prevent the user from adding more than 10 objects ive included in the if statement a code which adds the lengths of the arrays together and then sees if the total is less than or equal to 10.
this doesn't work because the length of the arrays always exceeds 10, so how do i change this code so that i can add together the total number of objects are in the array?
in order to add an object to an array the user must press a JButton on my GUI
here is the action listener for one of these buttons
java Syntax (Toggle Plain Text)
if(e.getSource() == addAir && (subs.length + airs.length + dests.length)<=10) { String name=JOptionPane.showInputDialog(null, "What is the name of this Aircreft Carrier?"); String row=JOptionPane.showInputDialog(null, "What is the horizontal posistion of this Aircreft Carrier?"); String col=JOptionPane.showInputDialog(null, "Where is the vertical posistion of this Aircreft Carrier?"); String numAircraft=JOptionPane.showInputDialog(null, "How many Aircrafts are on this Aircraft Carrier?"); }
to prevent the user from adding more than 10 objects ive included in the if statement a code which adds the lengths of the arrays together and then sees if the total is less than or equal to 10.
this doesn't work because the length of the arrays always exceeds 10, so how do i change this code so that i can add together the total number of objects are in the array?
•
•
Join Date: Nov 2008
Posts: 49
Reputation:
Solved Threads: 1
thanks for that heres an up dated form of the action listener cos im stilling having a little problem
my problem now is on the line which starts with position i'm trying to work out a way to take the input from the user in the row and col JOptionPanes and then to take it for the position formula so that the image icon show up in the right block
i cant use the row or col variable because they are strings and ints are needed for the formula, it wont let me use the scanner objects and i cant work out how to turn the strings for scanners into an int
java Syntax (Toggle Plain Text)
if(e.getSource() == addSub && count <=10) { String name=JOptionPane.showInputDialog(null, "What is the name of this Submarine?"); String row=JOptionPane.showInputDialog(null, "What is the horizontal posistion of this Submarine?"); Scanner rowScan = new Scanner(row); int row1 = rowScan.nextInt(); String col = JOptionPane.showInputDialog(null, "Where is the vertical posistion of this Submarine?"); Scanner colScan = new Scanner(col); int col1 = colScan.nextInt(); String depth=JOptionPane.showInputDialog(null, "What is the depth of this Submarine?"); posistion=(rowScan*10)+colScan; blocks[posistion].setIcon(subPic); count++; }
i cant use the row or col variable because they are strings and ints are needed for the formula, it wont let me use the scanner objects and i cant work out how to turn the strings for scanners into an int
Last edited by Laidler; Aug 20th, 2009 at 12:06 pm.
![]() |
Similar Threads
- adding to a dynamic array size... (Java)
- 2 dimentional array error (C++)
- char array error (C++)
- help me fix my program(strtok,strcmp,array) (C)
- Dynamic array (C++)
- array error (C++)
- bubble sort function w/ array (C++)
- boolean array (Java)
Other Threads in the Java Forum
- Previous Thread: Garbage collection and ResultSet, PreparedStatement, Connection
- Next Thread: illegal start of expression
Views: 233 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation bank binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image infinite input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen server set size sms socket sort sorting spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows






