Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 3, 2007, 10:57 PM   #1
macman2790
macrumors 6502a
 
macman2790's Avatar
 
Join Date: Sep 2006
Location: Texas
Java: can someone iterate through these two loops for me:

i've iterated through them like 4 times and it still looks right to me. Could it be the brackets being in wrong places or something. im trying to sort my array of scholars in this method, printAllApplicantsByName() and then print them. The first 2 for loops dont matter, it just takes the indexes that arent null and copies them to a temporary array and copies them back so they are ready to sort.
lets say the last names are in this order(i'll only give you first letter, the other letters dont match in any way anyways):
scholar[0] = "c"; scholar[1] = "b"; scholar[2] = "w"; scholar[3] = "a";
scholar[4] = "j"; scholar[5] = "m";

also i'll give you my compareNames() method, since i already have a compareTo() for something else, but i beleive its correct, my professor went through that with me.

Code:
         public int compareNames(Object other, Object myObject){
             int result = 0;
             String myName = ((Scholar)myObject).reverseFullName();
             String otherName = ((Scholar) other).reverseFullName();
             String myFirstName = "";
             String otherFirstName = "";
             String myLastName = "";
             String otherLastName = "";
             myName = myName.toLowerCase();
             otherName = otherName.toLowerCase();
             for(int i = 0; i < myName.length(); i++){
                 
                 if(myName.charAt(i) != ',')
                    
                 myLastName += myName.charAt(i);
                 if(myName.charAt(i) == ',')
                     break;
                 System.out.print(myLastName);
                 
             }
             int counter = 0;
             for(int j = 0; j < otherName.length(); j++){
                 if(otherName.charAt(j) == ',')
                     break;
                 otherLastName += otherName.charAt(j);  
                 System.out.println(otherLastName);
                 counter++;
             }
             System.out.println('\n');
             for(int k = 0; k < myName.length(); k++){
                 
                 if(myName.charAt(k) != ' ')
                 myFirstName += myName.charAt(k);
                     System.out.println(myFirstName);
                // if(myName.charAt(k))
                 
                // System.out.print(myFirstName);
             }
             int n;
             for(int l = 0; l < otherName.length(); l++){
                
                 if(otherName.charAt(l) == ' '){
                 
                 otherFirstName += otherName.charAt(0);
                 }
             }
             
             
               if(myName.compareTo(otherName) > 0)
                    result = 1;
             else if(myName.compareTo(otherName) < 0)
                    result = -1;
             
             else if(myLastName.compareTo(otherLastName) == 0){
                 if(myFirstName.compareTo(otherFirstName) > 0)
                     result = 1;
                 if(myFirstName.compareTo(otherFirstName) < 0)
                     result = -1;
             }
             
             return result;
             
        }

public void printAllApplicantsByName(){
            StringBuffer buf2 = new StringBuffer(
                 "\nScholarship = " + scholarship + 
                //" Amount that will be awarded among winners = " + getSingleScholarshipAmount() +
                 ". Number of intended winners = " + intendedNumberAwarded);
                 //Arrays.sort(scholar, Comparator c);
         buf2.append("\n------------------------------\n");
                 

         Scholar nullScholar = new Scholar("Jon Smith", 0.0, 0, 0, false);
         int min;
         int temp2 =  0;
         int temp3 = 0;
         int counter = 0;
         Scholar temp;
         Scholar [] temporary;
         int countScan = 0;
         int count2 = 0;
         int count = 0;
         
         for(int i = 0; i < scholar.length; i++){
             if(scholar[i] instanceof Scholar)
                 count++;
             //temp3 = i;
         }
       
         temporary = new Scholar[count];
         
         for(int j = 0; j<temporary.length; j++){
            if(scholar[j] != null)
                temporary[j] = scholar[j];
                
            }
            
            //System.out.println("\n\n\n");
            /*for(int i = 0; i < temporary.length; i++){
                System.out.println(temporary[i]);
            }*/
         scholar = temporary;
        
         // start iterating here, thanks
         for (int index =0 ; index < scholar.length; index++)
        {
            min = index;
            
                for(int scan = index + 1; scan < scholar.length; scan++){
                   
                    if(compareNames(scholar[scan], scholar[min]) < 0){
                        min = scan;
                    
                    
            
            // swap
            temp = scholar[scan];
            scholar[scan] = scholar[index];
            scholar[index] = temp;
                    }
         }
           
         for(int i = 0; i < scholar.length; i++){
               buf2.append ("["  + (i+1) + "]:" + scholar[i].getFullName() + 
                       " scored " +  scholar[i].getScore() + "\n");
            }
        
        System.out.println(buf2);

        }
i put where to start iterating through in a comment, i believe its the 3rd for loop in printAllApplicantsByName()

Thanks in advance.
__________________
C2D MBP 2.33ghz 3gb ram 160gb hdd Glossy
macman2790 is offline   0 Reply With Quote
Old Apr 3, 2007, 11:22 PM   #2
macman2790
Thread Starter
macrumors 6502a
 
macman2790's Avatar
 
Join Date: Sep 2006
Location: Texas
all i had to do was make:
if(compareNames(scholar[scan], scholar[min]) > 0){
min = index


so im done, so everyone may now ignore this thread.
__________________
C2D MBP 2.33ghz 3gb ram 160gb hdd Glossy
macman2790 is offline   0 Reply With Quote
Old Apr 4, 2007, 12:27 AM   #3
toddburch
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
If you want to shorten your loop (and get rid of them), you could parse the name using the following technique.
Code:
public class StringTest { 
	public static void main(String[] args) { 
		String name = "Burch, Todd" ; 
		int i = name.indexOf(", ") ; 
		System.out.println(name) ; 
		System.out.println("Last name=" + name.substring(0,i) ) ; 
		System.out.println("First name=" + name.substring(i+2) ) ; 
	}
}
The key is using indexOf(", ") to locate the separator, and then using substring off of that to isolate the lastname and firstname.

Todd
toddburch is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 08:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 04:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 04:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 10:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 05:49 PM

Forum Jump

All times are GMT -5. The time now is 07:20 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC