Results 1 to 19 of 19

Thread: `java array

  1. #1
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default `java array

    i have to write a program that uses an array and from the inputted numbers i need to sort the biggest,smallest,median and mean of the numbers entered into the array
    this is as far as i got

    Java Code:
    class array
     {
     public static void main (String [] args)
    
           {
           double num1, num2, num3, num4, num5;
    
    	   		System.out.println ("Enter a number");
    	   		num1 = EasyIn.getDouble ();
    
    	   		System.out.println ("Enter another number");
    	   		num2 = EasyIn.getDouble ();
    
    	   		System.out.println ("Enter a third number");
    	   		num3 = EasyIn.getDouble ();
    
    	        System.out.println ("Enter fourth number");
    		    num4 = EasyIn.getDouble ();
    
    			System.out.println ("Enter a fifth number");
    	   		num5 = EasyIn.getDouble ();
    
    	   		//output
    	   		System.out.print(" The biggest of the five numbers, "+num1+", "+num2+", "+num3+","+num4+", "+num5);
    	   		System.out.println(", is " + max5Numbers(num1, num2, num3, num4, num5));
    		}
    
    
              public static double max5Numbers (double s, double h, double a, double n, double e)
    	   	{
    	   		double largest = s;
    
    	   		if (h> largest)
    	   		{
    	   			largest = h;
    	   		}
    
    	   		if (a > largest)
    	   		{
    	   			largest = a;
    	   		}
    	   		if (n> largest)
    		    {
    				largest = n;
    		    }
    
    			if (e > largest)
    			{
    				largest = e;
    	   		}
    
    	   		return largest;
    	   	}
    
    
    
            public static double max5Numbers (double s, double h, double a, double n, double e)
    		  	   	{
    		  	   		double smallest = s;
    
    		  	   		if (h< smallest)
    		  	   		{
    		  	   			smallest = h;
    		  	   		}
    
    		  	   		if (a < smallest)
    		  	   		{
    		  	   			smallest = a;
    		  	   		}
    		  	   		if (n< smallest)
    		  		    {
    		  				smallest = n;
    		  		    }
    
    		  			if (e < smallest)
    		  			{
    		  				smallest = e;
    		  	   		}
    
    		  	   		return smallest;
    			   	}
    	}
    i use a compiler called easyin

    here is output

    F:\Java\Assignment\array.java:58: max5Numbers(double,double,double,double,double) is already defined in array
    public static double max5Numbers (double s, double h, double a, double n, double e)
    ^
    1 error

    Tool completed with exit code
    any help is appreciated

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

    Default

    The compiler is telling your exactly what's wrong: you're declaring a method with the same name and "signature" twice. Solution: don't do this.

  3. #3
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    i got that-thanks,but now when it compiles only the largest number is sorted,the smallest number is not done

  4. #4
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    Check out bubble sort algoritm, and you may get some ideas how to implement yours.

    Bubble sort - Wikipedia, the free encyclopedia

  5. #5
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    thanks will check it out

  6. #6
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    When you say not done, do you mean it's not being printed, or it is printed but is incorrect?

  7. #7
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    it is only printing the largest number,it is not printing the smallest number

  8. #8
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    well, you dont seem to have any line of code for printing the smallest one.

    You should also edit the 3rd method´s name to min5Number, instead of max5Number

  9. #9
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Rubber duck debugging. Talk yourself through your code line by line, don't rush past anything. While you do that, think about which parts might be linked to your 'error'.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by shane123 View Post
    i have to write a program that uses an array
    Where's the array? Naming your class 'array' doesn't count.

    kind regards,

    Jos

  11. #11
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    i have been working on my code and here is where i am now,any suggestions?

    Java Code:
    class accessarray
    {
    	public static void main (String[] args)
    	{
    
            int user_input;
            int input_length;
    		int num[] = new int [3];
    
    		System.out.println ("Please enter num 1");
    		num[0] = EasyIn.getInt();
    
    		System.out.println ("Please enter num 2");
    		num[1] = EasyIn.getInt();
    
    		System.out.println ("Please enter num 3");
    		num[2] = EasyIn.getInt();
    		System.out.println ("");
    
    		System.out.println ("The nums entered are as follows");
    
    
    		for (int i = 0; i < 3; i++)
    		{
    			System.out.println ("Nums in position " + i +" = " +num [i]);
    		}
    		
    		  System.out.println();
    		  System.out.println("The number is digits entered :         " + input_length);
    		  System.out.println("The Largest Number is        :         " + largestint(user_input) );
    		  System.out.println("The Smallest Number is       :         " + smallestint(user_input) );
    		  System.out.println("The average Number is        :         " + average(user_input) );
    		  System.out.println("The middle Number is         :         " + middle(user_input) );
    		 }
    
    		
    		 public static int largestint(int [] user_input)
    		 {
    		  int largest_integer = 0;
    		  for(int i=0; i<user_input.length; i++)
    		  {
    		   if(user_input[i] > largest_integer)
    		   {
    		    largest_integer = user_input[i];
    		   }
    		  }
    		  return largest_integer;
    		 }
    		
    		 public static int smallestint(int  [] user_input)
    		 {
    		 int  smallest_integer = 1;
    		  for(int  i=0; i<user_input.length; i++)
    		  {
    		   if(user_input[i] < smallest_integer)
    		   {
    		    smallest_integer = user_input[i];
    		   }
    		  }
    		  return smallest_integer;
    		 }
    		
    		 public static int average(int [] user_input)
    		 {
    		  int result=0.0;     
    		      for(int  i=0; i < user_input.length; i++)  
    		  {
    		         result=result + user_input[i];
    		      }
    		  int average=result/user_input.length;  
    		  return average;      
    		 }
    		
    		 public static int middle(int[] user_input)
    		 {
    		      int middle = user_input.length/2;  
    		      if (user_input.length%2 == 1)     
    		  {
    		          return user_input[middle];   
    		      }
    		  else 
    		  {
    		          return (user_input[middle-1] + user_input[middle]) / 2;
    		      }
    		 }
    }
    F:\Java\Lecture 9\accessarray.java:32: largestint(int[]) in accessarray cannot be applied to (int)
    System.out.println("The Largest Number is : " + largestint(user_input) );
    ^
    F:\Java\Lecture 9\accessarray.java:33: smallestint(int[]) in accessarray cannot be applied to (int)
    System.out.println("The Smallest Number is : " + smallestint(user_input) );
    ^
    F:\Java\Lecture 9\accessarray.java:34: average(int[]) in accessarray cannot be applied to (int)
    System.out.println("The average Number is : " + average(user_input) );
    ^
    F:\Java\Lecture 9\accessarray.java:35: middle(int[]) in accessarray cannot be applied to (int)
    System.out.println("The middle Number is : " + middle(user_input) );
    ^
    F:\Java\Lecture 9\accessarray.java:78: possible loss of precision
    found : double
    required: int
    int result=0.0; // To store the total(can have a decimal point)
    ^
    5 errors

    Tool completed with exit code 1

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

    Default

    The compiler error messages may seem cryptic but they really aren't and they are in fact telling you exactly what your errors are. You're passing user_input, an int variable that is never set to anything into a method that expects an array. Solution: don't do this. Instead pass the array that you have into the method. Learning to understand these error messages will help you go far. Much luck!

  13. #13
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    the program is compiling now but it printing out 0 for all the answers,any ideas?
    Java Code:
    // PROGRAMMER; SHANE REDDY
    // DATE; 21/3/2010
    // ASSIGNMENT; WRITE ARRAY PROGRAMME
    
    
    
            class arrays
            {
            public static void main(String[] args)
            {
    	    // get input and length
            int userInt =0;
      		int input_length;
    
      		System.out.print("Please Enter integers (e.g 1234):");
            String Input1 =  EasyIn.getString();
            input_length = Input1.length();
    
            int[] user_input = new int[input_length];
    
            for (int counter=0; counter<input_length; counter++)
            {
      	    user_input[counter] = userInt;
    
            }
            System.out.println();
            System.out.println("The Largest Number is        :         " + largestint(user_input) );
            System.out.println("The Smallest Number is       :         " + smallestint(user_input) );
            System.out.println("The average Number is        :         " + average(user_input) );
            System.out.println("The middle Number is         :         " + middle(user_input) );
     }
    
    /*
    ********************************************************************************************************************
    ********************************************************************************************************************
    **************************     METHODS     *********************************************************************
    ********************************************************************************************************************
    ********************************************************************************************************************
    */
     // Methods to Calculate largest value, smallest value, average and middle
    
     // largest - returns the largest integer
     public static int largestint(int [] user_input)
     {
      int largest_integer = 0;
      for( int i=0; i<user_input.length; i++)
      {
       if(user_input[i] > largest_integer)
       {
        largest_integer = user_input[i];
       }
      }
      return largest_integer;
     }
    
     // Smallest - returns the smallest integer
     public static int smallestint(int [] user_input)
     {
      int smallestint;
      smallestint = user_input[0];
      return smallestint;
     }
    
     // Average - returns the average value (with a decimal point)
     public static double average(int [] user_input)
     {
      double result=0.0;
      int i=0;
          for(i=0; i < user_input.length; i++)
      {
             result=result + user_input[i];
          }
      double average=result/user_input.length;
      return average;
     }
     // Midlle - Gets the middle value (or closest)
     public static int middle(int [] user_input)
     {
          int middle = user_input.length/2;  // subscript of middle element
          if (user_input.length%2 == 1)
      {
              // If Odd number of elements -- return the middle one.
              return user_input[middle];
          }
      else
      {
              // Even number -- return average of middle two
              // Must cast the numbers to double before dividing.
              return (user_input[middle-1] + user_input[middle]) / 2;
          }
     }
    
    }

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Java Code:
    ...
            int userInt =0;
    ...
            for (int counter=0; counter<input_length; counter++)
            {
      	    user_input[counter] = userInt;
    
            }
    You are assigning 0 to all elements of your user_input array, so of course you are getting all zeroes in your results.

    -Gary-

  15. #15
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    i cant get it to work,assignmnent due in morn!

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

    Default

    Quote Originally Posted by shane123 View Post
    i cant get it to work,assignmnent due in morn!
    Did you fix the problem that gcalvin noted above?

  17. #17
    shane123 is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    i tried not initializing the user_int variable to 0 but still same problem

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

    Default

    It may be time to talk to your teacher, I think, to get some one-on-one time.

  19. #19
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by shane123 View Post
    i tried not initializing the user_int variable to 0 but still same problem
    The problem is not that user_int has been initialized to 0. The problem is that it has nothing useful in it when you are assigning its value to the elements of the user_input array. Seriously, we're not trying to be mean about this, but if it is this much of a struggle getting a list of integers from user input and assigning them to an array, maybe you should be considering another field of study. Surely there must be examples in your text and study materials of getting input from users and assigning that input to variables, arrays, etc.?

    -Gary-

Similar Threads

  1. Java Array Help
    By md69holla in forum New To Java
    Replies: 14
    Last Post: 02-15-2010, 07:02 PM
  2. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 07:24 PM
  3. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 03:41 PM
  4. Help with array in java
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:03 AM
  5. Help with array and files in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:52 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
  •