So I tried to write a program that creates 15 random strings then allows the user whether he wants these strings sorted in descending or ascending order. the original strings and the sorted one to be displayed accordingly as well. Here is my attempt:
class Sorted strings{
final static int MAXSTR = 15; // number of random strings
final static int STRLEN = 8; // maximum string length
public static void main(String[] args){
String strs[] = new String[MAXSTR];
String sort[] = new String[MAXSTR];
boolean more, ascend;
Int i, el, j;
for (i=0; i<MAXSTR; i++){
strs[i] = ""; // initialise string to blank
for (j=0; j<STRLEN; j++){
do{
el = (int)((Math.random()*100000))%125;
}while(el<48);
strs[i] = strs[i]+(char)(el); // add random symbol
}
}
title(); //title + instructions
copy(strs, sort); //copy random strings to “sorted” string array
do{
//ascending or descending
ascend = get_ascend_or_descend();
//display Unsorted data
kdr.sortstring(sort,ascend);
//display SORTED data
displaysort(strs, sort);
more = kdr.goagain(); //makes the user want to repeat
}while(more);
}
//copies one string array to another
static void copy(String original[], String newest[]){
int zz;
for (zz=0; zz<original.length; zz++)
newest[zz] = original[zz];
}
//procedure to display sorted data
static void displaysort(String sn[], String srt[]){
int loop;
System.out.print("\n\n");
System.out.print("Original Sorted\n\n");
for(loop = 0; loop<sn.length; loop++){
kdr.message(sn[loop],15,false);
kdr.message(srt[loop],10,true);
System.out.print(“\n”);
}
System.out.print(“\n\n\n”);
}
// finds out if the user wants to sort ascending or descending
Static boolean gets_ascend_or_descend(){
Char asds;
System.out.print (“\n\nDo you wish to sort <A>scending or <D>escending :”);
do{
asds = Keyboard.readChar();
asds = Character.toLowerCase(asds);
}while(asds!=’a’ && asds!=’d’);
if (asds == ‘d’)
return(false);
else
return(true);
}
// title page
static void little(){
System.out.print(“\n\nThe program will create 30 random strings. You”);
System.out.print(“\nwill then be asked whether you want to sort ascending”);
System.out.print(“\nor descending. The program will then display the”);
System.out.print(“\noriginal strings and sorted ones. \n\n\n\n\n\n”);
}
}
I made comments throughout the program. However when I compiled it I got tons of errors :( Help please.