Results 1 to 12 of 12
Thread: up up and Array
- 01-28-2011, 11:31 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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.
- 01-29-2011, 01:01 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
What output do you expect?
and what is the actual output?
- 01-29-2011, 01:02 AM #3
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
- 01-29-2011, 11:28 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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.
- 01-29-2011, 11:40 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 01-29-2011, 11:45 PM #6
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).
- 01-29-2011, 11:53 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Aww...
I wanted the OP to have another go at describing erroneous output ;)
- 01-30-2011, 01:33 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Thank you. I will try to explain my problem better next time ;).
- 01-30-2011, 01:55 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
So, are you getting the output the way you want it now?
- 01-30-2011, 05:02 AM #10
- 01-30-2011, 10:22 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
- 01-30-2011, 10:26 PM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks