CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    18

    Need some help with swap array elements location based on the user input.

    I have an array of 5 strings which the user provides via the scanner class. However, I need to prompt the user to select two indexes of the array and swap them accordingly. I'm exactly sure how to do this... would I use a bubblesort algorithm? But again, it needs to be based on user input.

    Here's my code so far:

    Code:
    import java.util.Scanner;
    public class PersonArray {
    
     public static void main(String args[]) {
         
         String Person[] = new String[5];
     
         Scanner input = new Scanner(System.in);
         System.out.println();
         System.out.println("You will be prompted to enter in the names of 5 people for the array.");
         System.out.println();
         System.out.print("First person: ");
         Person[0] = input.next();
         System.out.print("Second person: ");
         Person[1] = input.next();
         System.out.print("Third person: ");
         Person[2] = input.next();
         System.out.print("Fourth person: ");
         Person[3] = input.next();
         System.out.print("Fifth person: ");
         Person[4] = input.next();
         
         System.out.println();
         System.out.println("Thank you. The names in the array are: ");
         
         for(int i = 0; i < Person.length; i++) {
             
          System.out.println();
          System.out.print(Person[i]);
          
         }
         
         System.out.println();
         System.out.println();
         System.out.print("Would you like to switch any two names in the list? Y/N: ");
         String ans = input.next();
         
         if(ans.equalsIgnoreCase("y")||(ans.equalsIgnoreCase("yes"))) {
             
         // This is where I need to prompt the user for which indexes and swap them.
             
         }
         
         else if(ans.equalsIgnoreCase("n")||(ans.equalsIgnoreCase("no"))) {
         
          System.out.println();   
          System.out.println("No array elements will be swapped.");
          System.exit(1);
          
         }
         
       }
       
    }

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Need some help with swap array elements location based on the user input.

    Why would you need a bubble sort? All you are doing is swapping two values. So, start by getting the two indexes. Once you have them it is relatively easy to swap them, I'll give you a hint: you will need a temp variable to store one of the values.

  3. #3
    Join Date
    Nov 2009
    Posts
    18

    Re: Need some help with swap array elements location based on the user input.

    Quote Originally Posted by ProgramThis View Post
    Why would you need a bubble sort? All you are doing is swapping two values. So, start by getting the two indexes. Once you have them it is relatively easy to swap them, I'll give you a hint: you will need a temp variable to store one of the values.
    Thanks I was thinking of some kind of sorting method because my professor was discussing it with us in class, but this might be a more understandable approach.

    Does something like this look about right?

    Code:
    // Somewhere up top the program.
    String temp;
    
     if(ans.equalsIgnoreCase("y")||(ans.equalsIgnoreCase("yes"))) {
             
         System.out.println();
         System.out.print("Enter in the first name: ");
         name1 = input.next();
         System.out.print("Enter in the second name to swap with: ");
         name2 = input.next();
    
            if(name1.equals(Person[0])&&(name2.equals(Person[1]))) {
    
            temp = Person[0];
            Person[0] = Person[1];
            Person[1] = temp;
    
            }
             
     }
    ^ And then do this for every possible combination? There has to be an easier way than this...
    Last edited by XDan; December 3rd, 2009 at 10:55 AM.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need some help with swap array elements location based on the user input.

    Your first post said the user entered the indices to swap but your code shows the user enters the values to swap - which is correct?

    Assuming the user needs to enter the values to swap, as per your code: you need to find the index of each entered value before trying to swap them.

    A simple way of doing this is to write a method that takes the array and value as parameters and returns the index the value is found at. Inside the method you just iterate over the array until you find the first occurance of the value. Remember to return an illegal value (-1) or throw an exception if the value isn't found.

    Call this method twice, once for each input value, and then use the returned indices in your swap code (which is correct).

  5. #5
    Join Date
    Nov 2009
    Posts
    18

    Re: Need some help with swap array elements location based on the user input.

    Okay I solved it now. Thanks everyone.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured