Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Bubble Sort with a 2D array ??
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
implosion




PostPosted: Wed May 21, 2008 9:34 pm   Post subject: Bubble Sort with a 2D array ??

I have a assignment that involves me outputting a deck in order, shuffling it and then using multiple sorts to sort them back. i'm using blue j. i keep getting an error on this line

if (Deck[cardnum][cardnum].compareTo(Suit[cardnum+1])>0);

it says it finds the array but cant find the integer ?

code:

import javax.swing.*;
import java.util.*;
import java.util.Random;

class CardAssignment1{



   public static void main(String args[]){

      String temp;
      String Deck[][] = new String[4][13];
      int s,c, card;
      Random randint = new Random();
      String ans;
      int ans1;
      int numberofpass=0;
      int cardnum;
     
     
      for(int Suit = 0 ; Suit <= 3 ; Suit ++){
        for(int Number= 0; Number <= 12 ; Number ++){
           if (Suit == 0) {
            card = Number + 1;   
            Deck [Suit] [Number] =  " Hearts " + card;
           }
           if (Suit == 1) {
            card = Number + 1;   
            Deck [Suit] [Number] =  " Diamonds " + card;
           }
           if (Suit == 2) {
            card = Number + 1;   
            Deck [Suit] [Number] =  " Spades " + card;
           }
           if (Suit == 3) {
            card = Number + 1;   
            Deck [Suit] [Number] =  " Clubs " + card;
           }
        }
      }
      System.out.println("\n\n\n **********INITIALIZED********** "); 
           
      System.out.println("\n\n\n **********NOT SHUFFLED********** "); 
           
      for(int Suit = 0 ; Suit <= 3 ; Suit ++)
        for(int Number= 0; Number <= 12 ; Number ++)
                 System.out.print(Deck[Suit][Number]+"   ");
     
      System.out.println("\n\n\n **********SHUFFLING********** "); 

      for(int Suit = 0 ; Suit <= 3 ; Suit ++){
          for(int Number= 0; Number <= 12 ; Number ++){
             s= randint.nextInt(3);
             c= randint.nextInt(12);
             temp = Deck[Suit][Number];
             Deck [Suit][Number] = Deck[s][c];
             Deck [s][c] = temp;
          }
       }

        System.out.println("\n\n\n **********NOW SHUFFLED********** "); 
       
        for(int Suit = 0 ; Suit <= 3 ; Suit ++)
           for(int Number= 0; Number <= 12 ; Number ++)
                 System.out.print(Deck[Suit][Number]+"  ");
         
     
          ans = JOptionPane.showInputDialog("Which method of sorting would you like to sort the deck?\n"
                                            + "1) Bubble Sort \n"
                                            + "2) Merge Sort\n"
                                            + "3) Insertion Sort\n"
                                            + "4) Quick Sort\n"
                                            + "5) Selection Sort\n"
                                            + "6) Shell Sort\n");
                                     
                ans1 = Integer.parseInt(ans);
       
                  if (ans1 == 1) {
                   for(int Suit = 0 ; Suit <= 3 ; Suit ++){
                      while (numberofpass < 3){
                          cardnum = 0;
                    while (numberofpass < 3) {
                        if (Suit[cardnum].compareTo(Suit[cardnum+1])>0);
                        temp = Suit[cardnum +1];
                        Suit[cardnum +1] = Suit[cardnum];
                        Suit[cardnum] = temp;
                }
                ++cardnum;
                    }
                   ++numberofpass;
                   
                  for(int Number= 0; Number <= 12 ; Number ++)
                     
                    while (numberofpass < 51){
                        cardnum = 0;
                        numberofpass =0;
                   
                     while (numberofpass <51) {
                        if (Number[cardnum].compareTo (Number[cardnum+1])>0);
                        temp = Number[cardnum +1];
                        Number[cardnum +1] = Number[cardnum];
                        Number[cardnum] = temp;
                }
                ++cardnum;
                    }
                   ++numberofpass;
               
                 System.out.println("\n\n\n **********SHUFFLED********** ");
                 System.out.print("");
                }
           
//           if (ans1 == 2) { insert Merge Sort      }
//           
//           if (ans1 == 3) { insert Insertion Sort  }
//           
//           if (ans1 == 4) { insert Quick Sort      }
//           
//           if (ans1 == 5) { insert Selection Sort  }
//           
//           if (ans1 == 6) { insert Shell Sort      }
       
     
    }
    }
}



Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Wed May 21, 2008 9:45 pm   Post subject: RE:Bubble Sort with a 2D array ??

look closer at your code what is Suit[], or what is Suit,

for(int Suit = 0 ; Suit <= 3 ; Suit ++)
implosion




PostPosted: Thu May 22, 2008 7:53 pm   Post subject: Re: Bubble Sort with a 2D array ??

okay never mind. i had a ton of troubles with the 2D array... i changed my code to this
i'm not sure on how to do the rest of the sorts. is there a way to do the merge sort with a String array ? everywhere i've checked they used a int array.
code:



import javax.swing.*;
public class DECK{
   public static void main(String args[]){
     
       String [] Deck = { "Club13", "Club12", "Club01", "Club02", "Club03", "Club04", "Club06", "Club05", "Club07", "Club09", "Club08", "Club11", "Club10", "Diamond10", "Diamond02", "Diamond06", "Diamond04", "Diamond03", "Diamond12", "Diamond07", "Diamond01", "Diamond09", "Diamond05", "Diamond11", "Diamond08", "Diamond13", "Heart13", "Heart02", "Heart11", "Heart06", "Heart08", "Heart04", "Heart07", "Heart05", "Heart09", "Heart10", "Heart03", "Heart12", "Heart01", "Spade12", "Spade05", "Spade03", "Spade09", "Spade02", "Spade13", "Spade07", "Spade06", "Spade04", "Spade10", "Spade11", "Spade01", "Spade08"};

       int count;
       count =0;
    int numberofpass;
        numberofpass =0;
    int cardnum;
        cardnum=0;
    String temp;
    String ans;
    int ans1;
   
    //---------------------------------------------------------------------------------
    System.out.println ("*SHUFFLED*");
      while (count <=51){
       System.out.print (Deck[count]+"  ");
       count = count +1;
    }
   
              ans = JOptionPane.showInputDialog("Which method of sorting would you like to sort the deck?\n"
                                            + "1) Bubble Sort \n"
                                            + "2) Merge Sort\n"
                                            + "3) Insertion Sort\n"
                                            + "4) Quick Sort\n"
                                            + "5) Selection Sort\n"
                                            + "6) Shell Sort\n");
                                     
                ans1 = Integer.parseInt(ans);
   
    if (ans1 == 1) {
    while (numberofpass < 51){
                        cardnum = 0;
      while (cardnum <51) {
                        if (Deck[cardnum].compareTo (Deck[cardnum+1])>0){
                        temp = Deck[cardnum +1];
                        Deck[cardnum +1] = Deck[cardnum];
                        Deck[cardnum] = temp;
                }
                ++cardnum;
                    }
                   ++numberofpass;
                }
                    System.out.println ("\n*SORTED*");
                    count = 0;
                    while (count < 52) {
                        System.out.print(Deck[count]+"  ");
                        count += 1;
                    }
                }
         
                if (ans1 == 2) {
                   
         
                }
//           if (ans1 == 3) { insert Insertion Sort  }
//           
//           if (ans1 == 4) { insert Quick Sort      }
//           
//           if (ans1 == 5) { insert Selection Sort  }
//           
//           if (ans1 == 6) { insert Shell Sort      }
            }


syntax_error




PostPosted: Thu May 22, 2008 7:59 pm   Post subject: RE:Bubble Sort with a 2D array ??

I maybe wrong but simple have the cards in descending order along with the correct subsequent indices, and simple treat the indices like ints and play around wit them as such

that should take out the string problem, if you only deal with the indices.
Aziz




PostPosted: Fri May 23, 2008 11:41 am   Post subject: RE:Bubble Sort with a 2D array ??

> and < operators only work for numerical values.

You'll have to use the .compareTo() method of String. Read this: http://www.javabeginner.com/java-string-comparison.htm
shadowman544




PostPosted: Sat May 24, 2008 8:26 pm   Post subject: Re: Bubble Sort with a 2D array ??

i believe a way to make this a little more in depth would be to make an object called "card" with for say the variables to hold suit and value(a,2-10 and j,q,k). you should then perhaps make another obect called "deck" and give it the contents of a deck of cards (52cards a-k) and the required methods to act as a deck (ex. shuffle and deal) and an object that is a "player" and an object called "hand"(play,pickup). This may eliminate your need for a 2d array.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: