Results 1 to 3 of 3
  1. #1
    eddyk6's Avatar
    eddyk6 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Traversing an Array

    Hi i need a little bit of help with arrays. I have a user input a positive integer but the thing is it's not storing the number that they input. I think the values they input are being replaced rather than being stored...

    Here's my code...

    Java Code:
       import java.util.Scanner;
    	
       public class kvasove
       {
          static Scanner kb = new Scanner(System.in);
       
          public static void main(String[] args)
          {//Start Main
             System.out.print("Welcome!\n");
             int[] ArrayTrav = new int[500];
             int size = 0;  
             int choice = 0;
            
           //Start Switch 
             while(choice != 4)
             {	
                choice = menu(kb);
                switch(choice)
                {
                   case 1:
                      System.out.println("Enter a Positive Integer");
                      int choice1 = kb.nextInt();
                      ArrayTrav[size] = choice1;
                      size++;
                      break;
                   
                   case 2:
                      GetMean(ArrayTrav);
                      break;
                   
                   case 3:
                   		//GetMedian();
                      System.out.println("You entered option number 3");
                      break;
                   	
                   case 4:
                      System.out.println("You have quit the program");
                      break;
                }
             	
             }
          	
           
          
          }//Main End Method
       
          public static int menu(Scanner kb)
          {//public class menu start
             int choice= 0;
             boolean flag;
             do
             {//do while start
                flag = false;
                System.out.print("\nPlease select from the following menu choices:\n\n"
                      				+ "\t1. Enter a number\n"
                      				+ "\t2. Display Mean\n"
                      				+ "\t3. Display Median\n"
                      				+ "\t4. Quit the program\n" );
                
                System.out.print("\n Choice -> ");
                	
                	
                try
                {
                   choice = kb.nextInt();
                      
                }
                                  
                   catch(Exception e)
                   {
                      System.out.println("Enter a positive integer!");
                      kb.nextLine();
                   }
                	
                	
                	
                if (choice != 1 && choice != 2 && choice != 3 && choice !=4)
                {
                   System.out.print("\nI am sorry, that is an invalid menu choice.\nPlease try again.\n");
                   flag= true;
                }
                   
                
             }while (flag);//do while end
             return choice;     
          
          
          }
       
       	
       	
       	
       	
       	
          public static int GetMean(int[] ArrayTrav )
          {
             int total = 0;
           
             int average = 0;
           
             for(int i = 0; i<ArrayTrav.length;i++)
             {
                total += ArrayTrav[i];
             
             }
             average = (total/ArrayTrav.length);
           
             System.out.println("The Mean of the numbers is:"+average);
           
             return average;
          
          
          }
       	
       	
       			
       	
       	
       	
       	
       }
    Last edited by Fubarable; 02-18-2012 at 09:07 PM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,573
    Rep Power
    23

    Default Re: Traversing an Array

    Please copy and paste the output that shows what you are talking about.
    Add comments describing what you expect to be shown.

  3. #3
    eddyk6's Avatar
    eddyk6 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Traversing a min heap
    By sehudson in forum New To Java
    Replies: 0
    Last Post: 05-08-2011, 06:44 PM
  2. TreeTable Traversing
    By english in forum SWT / JFace
    Replies: 0
    Last Post: 12-14-2010, 01:00 PM
  3. Traversing a JTabbedPane
    By Inks in forum AWT / Swing
    Replies: 12
    Last Post: 03-11-2009, 05:15 AM
  4. Replies: 1
    Last Post: 03-25-2008, 05:06 AM
  5. Traversing a directory
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •