Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do I just read a users input back out to them?

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I just read a users input back out to them?

    i need to output the unsorted array as well as the sorted array...the sorted array works but i can not get the unsorted array to work. please help!


    package Lab7;
     
    import java.util.Scanner;
    public class lab10 {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Welcome to the insertion sorter!");
    		System.out.println("Please enter the number of values you would like to sort");
    		int size = keyboard.nextInt();
    		int[] a = new int[size];
    		//populate the array
    		for(int i=0;i<a.length;i++)
    		{
    			System.out.println("Enter the number at position "+i);
    			a[i] = keyboard.nextInt();
    		}
    		//Selection sort!
    		for(int i=0;i<a.length;i++)
    		{
    			int smallestIndex = i;
    			for(int j=i;j<a.length;j++)
    			{
    				//Find the index of the next smallest value
    				if(a[j]<a[smallestIndex])
    				{
     
    					smallestIndex = j;
    				}
     
    			}
    			if(smallestIndex != i)//Swap the values
    			{
    				int temp = a[i];
    				a[i] = a[smallestIndex];
    				a[smallestIndex] = temp;
    			}
    		}
    		//Print out the unsorted and sorted array
    		System.out.println("The unsorted array is ");
    		for(int i=0;i<a.length;i++)
    		{
    			System.out.print(a[i]);
    		}
    		System.out.println("\nThe sorted array is ");
    		for(int i=0;i<a.length;i++)
    		{
    			System.out.print(a[i]+" ");
    		}
    	}
     
    }


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: How do I just read a users input back out to them?

    Well first you have both the print loops one after the other. You want to put the first loop to print the arrays unsorted before the actual sorting code. Then, after you do the sort, print out the array.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: How do I just read a users input back out to them?

    Your code looks okay but it's all out of order. You're sorting the array, then printing it twice after the sort. When you run the array through that sorting loop, you're completely changing the array.

Similar Threads

  1. [SOLVED] Read String to List<Integer> and back
    By Cornix in forum Java Theory & Questions
    Replies: 2
    Last Post: February 14th, 2014, 06:15 PM
  2. Replies: 6
    Last Post: February 8th, 2014, 03:59 AM
  3. keyboard scanner to take users input and enter students information method
    By foresboo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 7th, 2013, 03:39 PM
  4. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  5. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 02:21 PM