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.

Recommended Answers

All 3 Replies

It might help to know what errors. I am not going to play compiler and even try to figure them out when you could just post them. And fix your indentation, how do you expect anyone to be able to read that properly?

Yeah I'm sorry about not tagging it mate a bit new here. Here is the error I'm getting that I can't get to understand why.

The first line where it says

class Sorted strings{

it tells me:

'{' expected

but I already have it.

That line should be structured

[access modifier] class <classname> [extends <classname>] [implements <interfacename>] {

where does "strings" fit into that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.