Results 1 to 5 of 5
Thread: Arrays
- 11-23-2010, 10:53 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
Arrays
Hello there. I writing a program which uses arrays and for loops. In this program, it will ask the user to input their names and whether they are under 16. The data they entered will be stored in two arrays, adults and children.
In the end it will print an invoice listing names with the amount and also the total.
For examples;
what is the name of person 1? Beck
Is Beck under 16 ? No
what is the name of person 2? Jon
Is Jon under 16 ? Yes
.
.
.
.
.
What I have written so far
However I'm not sure how to put the names of adults and children to two different arrays.Java Code:class exercise { public static void main (String[] param) { waterRafting(); System.exit(0); } // END main public static void waterRafting() { // Declare variables String numberofparty = JOptionPane.showInputDialog("How many in the party (maximum 6) ?"); int total = Integer.parseInt(numberofparty); int[] people = new int[total]; while((people <=6) && (people >=0)) { for(int i = 1; i<=people.length; i++) { String name = JOptionPane.showInputDialog("What is the name of person " + i + " ?"); String age = JOptionPane.showInputDialog("Is " + name + " under 16 ?"); //String[] adults; //String[] children; if(age.equalsIgnoreCase("No")) { //Not under 16 add to adult array adultsArray(names); } else { //Under 16 add to children array childrenArray(names); } } return people; public static void adultsArray(String[] names) { boolean flag = true; for (int i = 0; i<names.length && true) { } }
- 11-24-2010, 12:19 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Does that compile? If not, and you can't understand the compiler's message copy and post it along with some indication of which line of your code it is referring to.
- 11-24-2010, 12:31 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
Hi,
I tried to change this part a bit
this is the errorJava Code:public static void waterRafting() { // Declare variables String numberofparty = JOptionPane.showInputDialog("How many in the party (maximum 6) ?"); int total = Integer.parseInt(numberofparty); int[] people = new int[total]; while((people <=6) && (people >=0)) { for(int i = 1; i<=people.length; i++) { String name = JOptionPane.showInputDialog("What is the name of person " + i + " ?"); String age = JOptionPane.showInputDialog("Is " + name + " under 16 ?"); String[] adults; String[] children; if(age.equalsIgnoreCase("No")) { //Not under 16 add to adult array adults = adultsArray(names); } else { //Under 16 add to children array children = childrenArray(names); } } return people; public static adultsArray(String[] names) { boolean flag = true; int i = 0; while(true) { for (i<names.length;i++) { names[i]; } } }
Java Code:exercise6.java:60: illegal start of expression public static void adultsArray(String[] names) ^ exercise6.java:60: illegal start of expression public static void adultsArray(String[] names) ^ exercise6.java:60: ';' expected public static void adultsArray(String[] names) ^ exercise6.java:60: '.class' expected public static void adultsArray(String[] names) ^ exercise6.java:60: ';' expected public static void adultsArray(String[] names) ^ exercise6.java:67: > expected for (i<names.length) ^ exercise6.java:67: not a statement for (i<names.length) ^ exercise6.java:68: illegal start of expression { ^ exercise6.java:68: ';' expected { ^ exercise6.java:69: illegal start of expression names[i]; ^ exercise6.java:69: ')' expected names[i]; ^ exercise6.java:69: illegal start of expression names[i]; ^
- 11-24-2010, 01:35 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
I tried to modify my program again.
I got this errorJava Code:public static void waterRafting() { // Declare variables String numberofparty = JOptionPane.showInputDialog("How many in the party (maximum 6) ?"); int total = Integer.parseInt(numberofparty); int[] people = new int[total]; String[] adults; String[] children; while((people <=6) && (people >=0)) { for(int i = 1; i<=people.length; i++) { String name = JOptionPane.showInputDialog("What is the name of person " + i + " ?"); String age = JOptionPane.showInputDialog("Is " + name + " under 16 ?"); if(age.equalsIgnoreCase("No")) { //Not under 16 add to adult array adults = printAdultsArray(names); } else { //Under 16 add to children array children = printChildrenArray(names); } } return people; } public static printAdultsArray(String[] names) { List<String> list = new ArrayList<String>(names.length); for (String s : names) { list.add(s); } return printAdultsArray;
Java Code:exercise6.java:64: illegal start of expression public static printAdultsArray(String[] names) ^ exercise6.java:64: illegal start of expression public static printAdultsArray(String[] names) ^ exercise6.java:64: ';' expected public static printAdultsArray(String[] names) ^ exercise6.java:64: ';' expected public static printAdultsArray(String[] names) ^ exercise6.java:100: reached end of file while parsing } ^ 5 errors
-
Please fix your indentation carefully and if you do, you'll quickly see that you have a method nested within another method, which is not allowed. This is one of the reasons for proper indentation.
Similar Threads
-
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Arrays
By hypes057 in forum New To JavaReplies: 13Last Post: 09-04-2009, 10:40 AM -
Arrays
By HosHos in forum New To JavaReplies: 3Last Post: 08-14-2009, 04:23 AM -
Need help with Arrays
By dietgal in forum New To JavaReplies: 21Last Post: 10-08-2008, 01:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks