6 Replies - 1335 Views - Last Post: 18 September 2011 - 03:00 PM Rate Topic: -----

#1 aj_lavaca  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 18-February 11

Sorting a String array

Posted 17 September 2011 - 08:00 PM

In my program I am suppose to read a text file, put the read names into an array based on a number randomly generated number, sort the arrays after the names are inserted, and then export the arrays to a new file. Right now it will read the text file, put them in arrays based on the number, and export it to another file. I cannot figure out how to sort a string array, since > and < only work with numbers and not letters. I was wondering what would be a better way of doing this.

1. Use a different type of array, maybe a short array. Not sure if that would make a difference.
2. Read the first letter of the slots of the array in question, convert it to a binary number, compare the numbers (Keeping in mind the differece in lowercase and uppercase numbers), and continue until it is decided which is greater in sorting, then either reconvert the numbers and put them back into the original array, or use an outside read and test to decide where to place the number.

I don't want the code, I just want someone to push me in the right direction or tell me that the above text is a good way of doing it. I'm certain that there is a better way than what I'm thinking, but neither my books nor research are coming up with anything.

Notes: The array will always be devisible by 4. The code will fail if you don't turn lines 96 through 109 to a note. The program will compile and run correctly after that.
/*		Author: Anthony Michael
		Class: Adv Prog.
		Assignment 1: Soccer
		
		------Notes------
		outFile.printf or -print would not take my \n command, currently using an added
			outFile.println("") to solve this problem.
		Exit Code (2) - Random number generated was not between 1-4.
		
		------Methods------
		getradnum() = Generates random number

*/

import java.io.*;
public class Ajm01soccer
{
	public static void main(String args[]) throws IOException
	{
		FileReader diskIn = new FileReader("girls40.txt");
		BufferedReader inFile = new BufferedReader(diskIn);
		FileWriter diskFile = new FileWriter("SoccerAssg1.txt");
		PrintWriter outFile = new PrintWriter(diskFile);
		
		String team1[] = new String [20];				//Holds Team1
		String team2[] = new String [20];				//Holds Team2
		String team3[] = new String [20];				//Holds Team3
		String team4[] = new String [20];				//Holds Team4
		String temp1[] = new String [20];				//Temporary Array for sorting (For Testing)
		int radnum = 0;										//Holds Random Number
		int slotnum = 0;										//Holds for Name Insertion Number
		String read1;											//Holds First Read Line
		String read2;											//Holds Second Read Line
		String read3;											//Holds Third Read Line
		String read4;											//Holds Forth Read Line
		String temp;											//Placeholder for temporary storage in sorting
		int r = 0;												//For sequence in sorting arrays
		int reps = 0;											//For repetition of sorting arrays
		
		outFile.println("===============================================");
		outFile.println("Proj 1 Anthony Michael      ");			//Import date program
		outFile.println("===============================================");
		outFile.println("       Lake Wobegon Girl's Soccer League       ");
		outFile.println("");
		outFile.println("No.   Team 1     Team 2     Team 3     Team 4  ");
		outFile.println("--- ---------- ---------- ---------- ----------");
		
		
		do 
		{
			//Get 4 names
			read1 = inFile.readLine();
			read2 = inFile.readLine();
			read3 = inFile.readLine();
			read4 = inFile.readLine();
			System.out.println(read1);
			System.out.println(read2);
			System.out.println(read3);
			System.out.println(read4);
			

			//Random Number Method
			radnum = radnumgen();
			System.out.println(radnum);
		
			//If statements to determine teams
			if (radnum == 1)
			{
				team1[slotnum] = read1;
				team2[slotnum] = read2;
				team3[slotnum] = read3;
				team4[slotnum] = read4;
			}
			if (radnum == 2)
			{
				team1[slotnum] = read4;
				team2[slotnum] = read1;
				team3[slotnum] = read2;
				team4[slotnum] = read3;
			}
			if (radnum == 3)
			{
				team1[slotnum] = read3;
				team2[slotnum] = read4;
				team3[slotnum] = read1;
				team4[slotnum] = read2;
			}
			if (radnum == 4)
			{
				team1[slotnum] = read2;
				team2[slotnum] = read3;
				team3[slotnum] = read4;
				team4[slotnum] = read1;
			}
			
			//Sort Array Method
			for (r=0; r<20; r++)
				temp1[r] = team1[r];
				
			for(reps=1; reps<20; reps++)
				for (r=0; r<19; r++)
					if (temp1[r] > temp1[r+1])
					{
						temp = temp1[r];
						temp1[r] = temp1[r+1];
						temp1[r+1] = temp;
					}
			
			//End Sort Array Method
			
			
			//Test - Printf format - \n is not forming a new line, outFile.println(""); is being used as a temp.
			outFile.printf("%3s %-10s %-10s %-10s %-10s\n",slotnum + ":", team1[slotnum], team2[slotnum], team3[slotnum], team4[slotnum]);
			outFile.println("");
			//End Test
			
			slotnum++; 			//Increases the girl number.
			
		}
		while(inFile.ready() != false);
		
			//Test
			System.out.println(team1[1] + " During test");
			System.out.println(team2[1] + " During test");
			System.out.println(team3[1] + " During test");
			System.out.println(team4[1] + " During test");
			for (r=0;r<20;r++)
				System.out.println(temp1[r]);
			//End Test
			
		
		outFile.close();
	}
	
	public static int radnumgen()
	{
		int radnum = 0;
		radnum = 1 + (int)(Math.random() * 4);
			//Random Number Failure
			if (radnum > 4 || radnum < 0)
			System.exit(2);
			//Test - Print rad num
			System.out.println(radnum);
		return radnum;
	}
	
}
	/* Notes-
	
			

	*/ 

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a String array

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: Sorting a String array

Posted 17 September 2011 - 08:15 PM

Strings have a compareTo() method, which will return a negative, 0, or positive number depending if the stringOne <, equal to, or > stringTwo. You can use that for your comparisons. Or you could just use Arrays.sort().
Was This Post Helpful? 2
  • +
  • -

#3 aj_lavaca  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 18-February 11

Re: Sorting a String array

Posted 18 September 2011 - 12:42 AM

The problem is that I have to code my own sort utility, not anything from another method. That's where I'm wondering how I should go about this.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Sorting a String array

Posted 18 September 2011 - 02:03 AM

Then it seems you'd order the strings based on the characters they contain. Find all that start with 'a' or 'A', and they are in the first group; all that start with 'b' or 'B', and they are in the second group, and so on. Then you'll have to refine the ordering by considering the characters that occur after the first.

Here's a silly example to get you started. I hope you're allowed to use methods like charAt() and substring():

import java.util.Scanner;


public class Sort2Strings
{
    public static void main( String[] args )
    {
        Scanner keyboard = new Scanner( System.in );
        System.out.println( "Enter 2 strings: " );
        String string1 = keyboard.next();
        String string2 = keyboard.next();

        stringSort( string1, string2 );
    }
    
    public static void stringSort( String string1, String string2 )
    {
        if ( string1.charAt( 0 ) < string2.charAt( 0 ) )
        {
            System.out.printf( "The strings in order are '%s' and '%s.'", string1, string2 );
        }
        else
        {
            System.out.printf( "The strings in order are '%s' and '%s.'", string2, string1 );
        }
    }
}


This post has been edited by GregBrannon: 18 September 2011 - 03:14 AM

Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: Sorting a String array

Posted 18 September 2011 - 08:38 AM

The compareTo() method returns an int you can compare. Use that in your own sorting method.
Was This Post Helpful? 1
  • +
  • -

#6 aj_lavaca  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 18-February 11

Re: Sorting a String array

Posted 18 September 2011 - 02:59 PM

Thanks you guys, you've given me plenty of options to work with and I appreciate it.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: Sorting a String array

Posted 18 September 2011 - 03:00 PM

Glad we could help! :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1