Results 1 to 12 of 12

Thread: up up and Array

  1. #1
    punlo is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default up up and Array

    I have an annoying problem with the array I made

    When I run the program it does not print nicely and on different lines. Its just one long line. When I add "+ " " " to the println it puts a space between every number in the array which is bothering me.

    where did I go wrong?
    PHP Code:
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Sorter {
    
    	
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Input array size");
    		int arraySize = input.nextInt();
    		int originalArray[] = new int[arraySize];
    		for (int i = 0; i < originalArray.length; i++){
    			originalArray[i] = (int)(Math.random() * 101);
    			System.out.print(originalArray[i]); //print#1
    			System.out.println();
    		}
    		
    		int selectionSortedArray[] = new int[arraySize];
    		for (int j = 0; j < selectionSortedArray.length; j++){
    			selectionSortedArray[j] = (int)(Math.random() *101);
    			performSelectionSort(selectionSortedArray);
    			System.out.print(selectionSortedArray[j]); // print number 2
    			System.out.println();
    		}
    		int utilSortedArray[] = new int[arraySize];
    		for (int s = 0; s < utilSortedArray.length; s++){
    			utilSortedArray[s] = (int)(Math.random() * 101);
    			Arrays.sort(utilSortedArray);
    			Arrays.equals(selectionSortedArray, utilSortedArray);
    			System.out.print(utilSortedArray[s]); // print #3
    			System.out.println();
    		}
    		
    		}
    	
    	
    	public static void performSelectionSort(int[] a) {
    		int i, j;
    		int minIndex = 0;
    		int minValue;
    		int temp;
    		for (i = 0; i < a.length; i++) {
    			minValue = a[i];
    			minIndex = i;
    			for (j = i; j < a.length; j++) {
    
    				if (a[j] < minValue) {
    					minValue = a[j];
    					minIndex = j;
    				}
    			}
    			
    			temp =a[i];
    			a[i]=a[minIndex];
    			a[minIndex] = temp;
    			
    		}
    	}
    
    }
    Last edited by punlo; 01-28-2011 at 11:35 PM.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    What output do you expect?
    and what is the actual output?

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Really? Here's my output when I run your program verbatim:
    Java Code:
    run:
    Input array size
    6 [b]// This is my input.[/b]
    78
    7
    14
    83
    100
    75
    0
    0
    0
    28
    87
    87
    0
    0
    0
    2
    3
    56

  4. #4
    punlo is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I expect the output too look like this
    Output 1
    Output 2
    Output 3.

    Instead the output looks like the above post(Zacks post). all the outputs together, every number on a new line.

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

    Default

    I expect the output too look like this
    Output 1
    Output 2
    Output 3.

    You don't expect the word "Output" to be printed do you? Programmers are very literal minded. A better elaboration of the problem would be to say something like:

    I expect output like the following:

    Java Code:
    Input array size
    3
    29 64 57 
    0 70 81 
    0 8 96

    ----------------------------------

    You are printing a newline inside each of those for loops (just before the end of the loop) so it no wonder that everything is going on a new line. Don't do that: remove the System.out.println() calls within the for loops.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Where you want the println statement is just AFTER the loop, not inside of it.

    You will also want that +" " or similar in your print(), or else it will print out something like 296457 (from brock's post above).

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

    Default

    Aww...

    I wanted the OP to have another go at describing erroneous output ;)

  8. #8
    punlo is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you. I will try to explain my problem better next time ;).

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

    Default

    So, are you getting the output the way you want it now?

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Quote Originally Posted by pbrockway2 View Post
    Aww...

    I wanted the OP to have another go at describing erroneous output ;)
    My bad! We need secret codewords to put in posts that describe the intent of the post to regulars here. :|

  11. #11
    punlo is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    So, are you getting the output the way you want it now?
    yes sir I am

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

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •