Results 1 to 12 of 12
  1. #1
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default How do i make a bunch of arrays into a list?

    When i sort it now the list comes out like this

    [1,2,3,4]

    how can i change it so the output comes out like this?

    1
    2
    3
    4


    Heres the code i have so far if it helps.

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    public class array
    {
    	public static void main(String [] args)
    	{
    		Scanner console = new Scanner(System.in);
    		
    				
    		int arraysize;
    		int value;
    		
    		
    		
    		System.out.print("Enter size of array: " );
    		arraysize = console.nextInt();	
    
    		int[] array = new int[arraysize];
    
    		
    		
    		for(int x = 0 ; x < array.length; ++x)
    			{
    			System.out.print("Enter value #" + (x+1) + ": " );
    			array [x]= console.nextInt();
    			}
    	
    		System.out.println("\n" + "Your data is:");
    		System.out.println(Arrays.toString(array));
    		
    
    
    		System.out.println("\n" + "Your sorted data is:");
    		Arrays.sort(array);
    		System.out.println(Arrays.toString(array));
    		
    
    		
    	}
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Java Code:
    System.out.println(Arrays.toString(array));
    Instead of using a method in the API to "format" the values in the array, create your own loop that goes through the array and prints each value on a separate line.

  3. #3
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Thank you!

    Also, how would i find the median, average, smallest and largest numbers of the arrays?

    For the average i was thinking of making a loop that keeps adding them and then divide the total by the array size but i dont know how i would do that.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You just described the solution perfectly. If you have problems with the base syntax, I'd suggest reading a few tutorials on the Sun java site, or your textbook, if this is for school.

  5. #5
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    ok I got it to work, but my teacher said it has to be 5 seperate methods? One that prints the value, one that prints the average, one that prints the smallest, one that prints the largest, and the last is the median.

    How do i break my main method down into seperate ones??

    Heres the code i have to break down

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    //change to 4 methods, pg331
    
    public class array
    {
    	
    
    	public static void main(String [] args)
    
    	{
    	
    	
    		Scanner console = new Scanner(System.in);
    		
    				
    		int arraysize;
    		int value;
    		double average = 0;
    		int smallest;
    		double even = 0;
    		
    
    		
    			
    		System.out.print("Enter size of array: ");
    
    		arraysize = console.nextInt();	
    
    		int[] array = new int[arraysize];
    		
    	
    
    		for(int x = 0 ; x < array.length; ++x)
    			{
    				System.out.print("Enter value #" + (x+1) + ": " );
    				array [x]= console.nextInt();
    			}
    
    		
    		
    		
    			System.out.println("\n" + "Your data is:");
    		
    			for(int x = 0; x < array.length; ++x)
    			{
      					System.out.println(array[x]);
    
    			}
    
    				
    
    		Arrays.sort(array);
    		
    		for(int x = 0; x < array.length; ++x)
    			{
    				average=average + array[x];
    			}
    	
    		System.out.println("\n"+"Average is: " + average/arraysize);
    		
    		
    		smallest = array[0];
    		
    		int largest = array[0];
    		
    		for(int x = 1; x < array.length; x++)
    		{
    			if(largest < array[x])
    			{
    				largest = array[x];
    			}
    
    		}
    		
    							
    
    		System.out.println("Smallest value is: " + smallest);
    		System.out.println("Largest value is: " + largest);
    
    
    		System.out.println("\n" + "Your sorted data is:");
    		
    		
    		for(int x = 0; x < array.length; ++x)
    			{
       			System.out.println(array[x]);
    			}
    
    		int result = array.length % 2; 
    
    		double median = 0; 
    
    
    		if (result == even) 
    		{ 
    			int rightNumber = array.length / 2; 
    			int leftNumber = rightNumber - 1; 
    			median = (array[rightNumber] + array[leftNumber]) / 2; 
    		} 
    		else 
    
    		{
     
    		median = array[array.length / 2]; 
     
    		
    		} 
    
    
    		System.out.println("\n" + "The median is: " + median);
    
    
    		
    
    		
    	}
    }

  6. #6
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Quite easily! for "average", for example, do this: replace

    Java Code:
    for(int x = 0; x < array.length; ++x)
    			{
    				average=average + array[x];
    			}
    	
    		System.out.println("\n"+"Average is: " + average/arraysize);
    With

    Java Code:
    printAverage (array);
    and then create the new method (on the same level as main()):

    Java Code:
    private static void printAverage (int [] array) {
    int average = 0;
    for(int x = 0; x < array.length; ++x)
    			{
    				average=average + array[x];
    			}
    	
    		System.out.println("\n"+"Average is: " + average/array.length);
    }
    as you see, the body of the method is exactly the code you already have. all the other methods are done similarly.

    Cheers!

  7. #7
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    ok i havent changed the methods yet because i have been trying to set up error messages and im stuck, i cant get the second one to work i need help! thanks

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    //change to 4 methods, pg331
    
    public class array
    {
    	
    
    	public static void main(String [] args)
    
    	{
    	
    	
    		Scanner console = new Scanner(System.in);
    		
    
    			
         		boolean good = false;
        		int arraysize;
    		int value;
    		double average = 0;
    		int smallest;
    		double even = 0;
    		String arraySizeString = null;
    		String Stgarray = null;
    		
    		
    
    		 while(!good)
         			{
          				good = true;
            			System.out.print("Enter size of array: ");
              			arraySizeString = console.nextLine();
    
         				for(int x=0; x<arraySizeString.length(); ++x)
    					
         				
    				if(!Character.isDigit(arraySizeString.charAt(x)))
             			good=false;
    			
    
    
           				if(!good)
             			System.out.println("Invalid array size - please enter an integer value");
    					
         						
    			}
    	
    
    
    
    
    		
    					
    			arraysize = Integer.parseInt(arraySizeString);
    
    			int[] array = new int[arraysize];
    
    
    
    			good = false;
    
    		for(int i = 0 ; i < array.length; ++i)
    	{
    
    		while(!good)
         			{
    
    				good = true;
            			System.out.print("Enter Value #" + (i+1) + ":");
    				
              			arraySizeString = console.nextLine();
    
         				for(int x=0; x<arraySizeString.length(); ++x)
    					
         				
    				if(!Character.isDigit(arraySizeString.charAt(x)))
             			good=false;
    			
    
    
           				if(!good)
             			System.out.println("Invalid array size - please enter an integer value");
    					
    			}
    			
    					
    			arraysize = Integer.parseInt(arraySizeString);
    
        			
    	
    		}
    		
    			System.out.println("\n" + "Your data is:");
    		
    			for(int x = 0; x < array.length; ++x)
    			{
      					System.out.println(array[x]);
    
    			}
    
    				
    
    		Arrays.sort(array);
    
    
    
    
    
    		
    		for(int x = 0; x < array.length; ++x)
    			{
    				average=average + array[x];
    			}
    	
    		System.out.println("\n"+"Average is: " + average/array.length);
    			
    		
    		
    			
    
    
    
    		smallest = array[0];
    		
    		int largest = array[0];
    		
    		for(int x = 1; x < array.length; x++)
    		{
    			if(largest < array[x])
    			{
    				largest = array[x];
    			}
    
    		}
    		
    							
    
    		System.out.println("Smallest value is: " + smallest);
    		System.out.println("Largest value is: " + largest);
    
    
    		System.out.println("\n" + "Your sorted data is:");
    		
    		
    		for(int x = 0; x < array.length; ++x)
    			{
       			System.out.println(array[x]);
    			}
    
    		int result = array.length % 2; 
    
    		double median = 0; 
    
    
    		if (result == even) 
    		{ 
    			int rightNumber = array.length / 2; 
    			int leftNumber = rightNumber - 1; 
    			median = (array[rightNumber] + array[leftNumber]) / 2; 
    		} 
    		else 
    
    		{
     
    		median = array[array.length / 2]; 
     
    		
    		} 
    
    
    		System.out.println("\n" + "The median is: " + median);
    
    
    		
    
    		
    	}
    }

  8. #8
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    nvm i just figured it out! now i gotta change the methods

  9. #9
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    can you elaborate on the methods some more when i do this i get a bunch of errors?

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    
    
    public class array
    {
    	
    
    	public static void main(String [] args)
    
    	{
    	
    	
    		Scanner console = new Scanner(System.in);
    		
    
    			
         		boolean good = false;
        		int arraysize;
    		int value;
    		double average = 0;
    		int smallest;
    		double even = 0;
    		String arraySizeString = null;
    		String Stgarray = null;
    		
    		
    
    		 while(!good)
         			{
          				good = true;
            			System.out.print("Enter size of array: ");
              			arraySizeString = console.nextLine();
    
         				for(int x=0; x<arraySizeString.length(); ++x)
    					
         				
    				if(!Character.isDigit(arraySizeString.charAt(x)))
             			good=false;
    			
    
    
           				if(!good)
             			System.out.println("Invalid array size - please enter an integer value");
    					
         						
    			}
    	
    
    
    
    
    		
    					
    			arraysize = Integer.parseInt(arraySizeString);
    
    			int[] array = new int[arraysize];
    
    
    
    			good = false;
    
    		for(int i = 0 ; i < array.length; ++i)
    		{
    
    		while(!good)
         			{
    
    				good = true;
            			System.out.print("Enter Value #" + (i+1) + ":");
    				
              			arraySizeString = console.nextLine();
    
         				for(int x=0; x<arraySizeString.length(); ++x)
    					
         				
    				if(!Character.isDigit(arraySizeString.charAt(x)))
             			good=false;
    			
    
    
           				if(!good)
             			System.out.println("Invalid array size - please enter an integer value");
    					
    			}
    			
    					
    			array[i] = Integer.parseInt(arraySizeString);
     			good = false;
    
        			
    	
    		}
    		
    			System.out.println("\n" + "Your data is:");
    		
    			for(int x = 0; x < array.length; ++x)
    			{
      					System.out.println(array[x]);
    
    			}
    
    				
    
    		Arrays.sort(array);
    
    
    
    
    		}
    		private static void printAverage (int [] array) {
    		int average = 0;
    		for(int x = 0; x < array.length; ++x)
    			{
    				average=average + array[x];
    			}
    	
    		System.out.println("\n"+"Average is: " + average/array.length);
    		}
    
    
    
    		
    			
    
    
    
    
    
    		public static void main(String [] args)
    
    	{
    
    		smallest = array[0];
    		
    		int largest = array[0];
    		
    		for(int x = 1; x < array.length; x++)
    		{
    			if(largest < array[x])
    			{
    				largest = array[x];
    			}
    
    		}
    		
    							
    
    		System.out.println("Smallest value is: " + smallest);
    		System.out.println("Largest value is: " + largest);
    
    
    		System.out.println("\n" + "Your sorted data is:");
    		
    		
    		for(int x = 0; x < array.length; ++x)
    			{
       			System.out.println(array[x]);
    			}
    
    		int result = array.length % 2; 
    
    		double median = 0; 
    
    
    		if (result == even) 
    		{ 
    			int rightNumber = array.length / 2; 
    			int leftNumber = rightNumber - 1; 
    			median = (array[rightNumber] + array[leftNumber]) / 2; 
    		} 
    		else 
    
    		{
     
    		median = array[array.length / 2]; 
     
    		
    		} 
    
    
    		System.out.println("\n" + "The median is: " + median);
    
    
    		
    
    		
    	}
    }

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

  11. #11
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    [B]Can anyone help me with the methods on why they dont work?

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    
    
    public class array
    {
    	
    	public static void main(String [] args)
    
    	{
    		
    		Scanner console = new Scanner(System.in);		
         		boolean good = false;
        		int arraysize;
    		int value;		
    		String arraySizeString = null;
    		String Stgarray = null;
    				
    
    		while(!good)
         			{
          				good = true;
    
            			System.out.print("Enter size of array: ");
    
              			arraySizeString = console.nextLine();
    
         				for(int x=0; x<arraySizeString.length(); ++x)
    					
         				if(!Character.isDigit(arraySizeString.charAt(x)))
    
             			good=false;
    			
           				if(!good)
    
             			System.out.println("Invalid array size - please enter an integer value");					
         						
    			}
    	
    
    		
    					
    			arraysize = Integer.parseInt(arraySizeString);
    
    			int[] array = new int[arraysize];
    
    
    
    			good = false;
    		
    			for(int i = 0 ; i < array.length; ++i)
    
    			{
    
    			while(!good)
         				{
    
    					good = true;
    
            				System.out.print("Enter Value #" + (i+1) + ":");
    				
              				arraySizeString = console.nextLine();
    
         					for(int x=0; x<arraySizeString.length(); ++x)
    					
         				
    					if(!Character.isDigit(arraySizeString.charAt(x)))
    
             				good=false;
    			
           					if(!good)
    
             				System.out.println("Invalid array size - please enter an integer value");
    					
    				}
    			
    					
    			array[i] = Integer.parseInt(arraySizeString);
    
     			good = false;
    
        			
    	
    			}
    		
    			System.out.println("\n" + "Your data is:");
    		
    			for(int x = 0; x < array.length; ++x)
    			{
      					System.out.println(array[x]);
    
    			}			
    
    
    
    		Arrays.sort(array);
    
    	
    	
    		}
    			
    	
    		public static double printAverage (int [] array)
    			{
    				
    				
    				double average = printAverage(array);
    
    				for(int x = 0; x < array.length; ++x)
    					{
    
    						average=average + array[x];
    
    
    					}
    	
    					
    
    				
    				System.out.println("\n"+"Average is: " + average/array.length);
    				
    				
    
    				return average;
    			}
    
    			
    
    
    
    
    
    		
    		
    		public static int printSmallest (int [] array)
    		{
    		
    			int smallest;
    		
    			smallest = array[0];
    
    		
    			System.out.println("Smallest value is: " + smallest);
    
    			
    			return smallest;
    		}
    
    
    
    		public static double printLargest (int [] array)
    		{
    			int largest = array[0];
    		
    			for(int x = 1; x < array.length; x++)
    				{
    					if(largest < array[x])
    						{
    				
    							largest = array[x];
    						}
    
    				System.out.println("Largest value is: " + largest);
    
    				}
    			
    			return largest;
    		}
    							
    		
    		public static int[] printSorted (int [] array)	
    		{
    
    			System.out.println("\n" + "Your sorted data is:");
    		
    		
    			for(int x = 0; x < array.length; ++x)
    				{
       					System.out.println(array[x]);
    				}
    			
    			
    			return array;
    
    		}
    
    
    		public static double printMedian (int [] array)
    		
    		{
    			double even = 0;
    
    			int result = array.length % 2; 
    
    			double median = 0; 
    
    
    			if (result == even) 
    				{ 
    					int rightNumber = array.length / 2; 
    					int leftNumber = rightNumber - 1; 
    					median = (array[rightNumber] + array[leftNumber]) / 2; 
    					System.out.println("\n" + "The median is: " + median);
    				}
     
    			else 
    
    				{
     
    					median = array[array.length / 2]; 
     
    		
    				} 
    
    			
    			return median;
    			
    
    		}
    	
    }

  12. #12
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

Similar Threads

  1. Replies: 2
    Last Post: 11-22-2009, 06:24 PM
  2. Replies: 3
    Last Post: 11-21-2009, 06:48 PM
  3. how to create list of list in java ???
    By ilayaraja in forum Advanced Java
    Replies: 1
    Last Post: 10-26-2009, 05:30 PM
  4. Replies: 9
    Last Post: 06-25-2008, 05:48 AM
  5. i have a bunch of strings in an arraylist and...
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-11-2008, 01:51 AM

Posting Permissions

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