Results 1 to 5 of 5

Thread: Arrays

  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default 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

    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)
            {
            	 
            }
            
         	
         }
    However I'm not sure how to put the names of adults and children to two different arrays.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    Hi,
    I tried to change this part a bit

    Java 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];
           		}
            }
            
            
         	
         }
    this is the error

    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];
                                   ^

  4. #4
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    I tried to modify my program again.

    Java 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;
    I got this error
    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. Arrays
    By hypes057 in forum New To Java
    Replies: 13
    Last Post: 09-04-2009, 11:40 AM
  4. Arrays
    By HosHos in forum New To Java
    Replies: 3
    Last Post: 08-14-2009, 05:23 AM
  5. Need help with Arrays
    By dietgal in forum New To Java
    Replies: 21
    Last Post: 10-08-2008, 02:59 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
  •