0

I use the bubble sort method to sort the input numbers in order (least to greatest), and I have it in its own method. Then, in the next method (where I need to have the numbers in order) it seems to ignore that I have sorted the numbers out in the previous method. Am I not calling out the method correctly or what? Thanks!

import java.util.Scanner;
public class hw6 {
public static int getOrder(int n1, int n2, int n3, int n4, int n5) {
    Scanner in = new Scanner(System.in);

    if (n1>n2){
        int t = n1;
        n1 = n2;
        n2 =t;
    }
    if (n2>n3){
        int t = n2;
        n2 = n3;
        n3 =t;
    }
    if (n3>n4){
        int t = n3;
        n3 = n4;
        n4 =t;
    }
    if (n4>n5){
        int t = n4;
        n4 = n5;
        n5 =t;
    }
    if (n1>n2){
        int t = n1;
        n1 = n2;
        n2 =t;
    }
    if (n2>n3){
        int t = n2;
        n2 = n3;
        n3 =t;
    }
    if (n3>n4){
        int t = n3;
        n3 = n4;
        n4 =t;
    }
    if (n1>n2){
        int t = n1;
        n1 = n2;
        n2 =t;
    }
    if (n2>n3){
        int t = n2;
        n2 = n3;
        n3 =t;
    }
    if (n1>n2){
        int t = n1;
        n1 = n2;
        n2 =t;
    }
    System.out.println(n1+" "+n2+" "+n3+" "+n4+" "+n5);

    return n1+n2+n3+n4+n5;//cards now in order from least to greatest
 }
 public static int getRank(int n1, int n2, int n3, int n4, int n5) {
 int order;
 int rank = 1;
 order = getOrder(n1, n2, n3, n4, n5);

 if (n1 == n2 && n1 == n3 && n1 == n4 || 
     n2 == n3 && n2 == n4 && n2 == n5)
     System.out.println("Four of a kind");
 else if (n1 == n2 && n1 == n3 && n4 == n5 ||
          n1 == n2 && n3 == n4 && n3 == n5)
     System.out.println("Full house");
 else if (n2 == n1+1 && n3 == n2+1 && n4 == n3+1 && n5 == n4+1)
     System.out.println("Straight");
 else if (n1 == n2 && n1 == n3 || n2 == n3 && n2 == n4 ||
          n3 == n4 && n3 == n5)
     System.out.println("Three of a kind");
 else
     System.out.println("A high card");

 return rank;
 }       
 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n1, n2, n3, n4, n5; //input 
    int order;
    int rank;

    System.out.print("Enter  (1-13):");
    n1 = in.nextInt();
    System.out.print("Enter  (1-13):");
    n2 = in.nextInt();
    System.out.print("Enter  (1-13):");
    n3 = in.nextInt();
    System.out.print("Enter  (1-13):");
    n4 = in.nextInt();
    System.out.print("Enter  (1-13):");
    n5 = in.nextInt();

    order = getOrder(n1, n2, n3, n4, n5);
    rank = getRank(n1, n2, n3, n4, n5);
}

}

ToonLink
  • 235
  • 1
  • 2
  • 9
  • To be honest, not completely sure. It was what I was taught to do. Never tried it any other way. – ToonLink Oct 09 '13 at 22:54
  • BTW you should consider following Java naming conventions "Class name should begin with capital letter" – Prateek Oct 09 '13 at 23:03

1 Answers1

2

Java is pass-by-value, so the getOrder method receives copies of the values. The local copies are changed, but the original variables from main are unchanged.

To get the changes to persist, I would create an array or an ArrayList to contain the values. Then pass that to your method to change those values. The method will receive a copy of the reference to the array or ArrayList object, so the changes will persist in the object.

rgettman
  • 176,041
  • 30
  • 275
  • 357