Trouble with arrays and runs

So what I have is a program that takes command line arguments and breaks them into runs based on alphabetical order (if the next value is higher in the alphabet, it becomes part of the next run).

What I must do now is find the lowest (highest in the alphabet) value to occurs consecutively and how many times it does. Here is a sample output:

java A0 ca ba ba ba an an ba ba

Produces:

(ca) (ba ba ba) (an an ba ba)

Number runs: 3

Length longest run: 4

Smallest element which occurs most times consecutively: ba

Max times it occurs consecutively: 3

I have it all done until the consecutive entries part. Please note I am a Java newbie and have only been doing this for a little over a month! Many thanks!

[768 byte] By [Zortoflavena] at [2007-11-27 8:48:18]
# 1
Code?Paste your codeHighlight itClick code button
floundera at 2007-7-12 20:54:59 > top of Java-index,Java Essentials,Java Programming...
# 2

Oops! Of course that would help!

public class A0{

public static void main(String args[]){

System.out.println("There are "+args.length+" arguments.");

System.out.println("Runs: ");

//Prints Runs, counts the length of largest and counts runs

int run_length=1;

int count=1;

int runs=1;

if (args.length>0)

{

System.out.print("(");

for (int i=0;i<(args.length-1);i++)

{

if(args[i].compareToIgnoreCase(args[i+1])<=0)

System.out.print(args[i]+" ");

else

System.out.print(args[i] + ") (");

if(args[i].compareToIgnoreCase(args[i+1])<=0)

{

count++;

if(count>=run_length)

run_length=count;

}

else

count=1;

if(args[i].compareToIgnoreCase(args[i+1])>0)

runs++;

}

System.out.println(args[args.length-1] + ")");

}

//Output

System.out.println("Number of Runs: " + runs);

System.out.println("Length of longest run: " + run_length);

System.out.println("Smallest element that occurs consecutivly: " + value);

System.out.println("Times it occurs: " + count);

}

}

That will work for the first part. Just need to work something to do what I said previously. I know there are two non-existant variable used at the end. Just a place holder.

Message was edited by:

Zortoflaven

Message was edited by:

Zortoflaven

Zortoflavena at 2007-7-12 20:54:59 > top of Java-index,Java Essentials,Java Programming...
# 3

I strongly recommend that you don't have disjointed if / else blocks. You show several if blocks that check for the same thing, as if one was created, then the next one was created as an after-thought. Don't do that. If they can be combined into one if block do it. Then you have elses, then another if block that is equivalent to either of the two else's above -- again disjointing things. Combine them all together.

For instance, not this:

if (a >= b)

{

doA();

}

else

doB();

if (a >= b) // same if statement as above

{

doC();

}

else

doD();

if (a < b) // same thing as the else above! if not >=, then it's <

{

doF();

}

but THIS:

if (a >= b)

{

doA();

doC();

}

else

{

doB();

doD();

doF();

}

Also, I strongly recommend that you enclose all of your post if and post else statements in curly brackets, even if it's only a single statement.

Don't do this:

if (a >= b)

{

doA();

doC();

}

else

doB();

but rather do this:

if (a >= b)

{

doA();

doC();

}

else

{

doB();

}

This will prevent you from making subtle mistakes later.

Message was edited by:

petes1234

petes1234a at 2007-7-12 20:54:59 > top of Java-index,Java Essentials,Java Programming...
# 4

Right now you are looking at two possible cases

if (args[i].compareToIgnoreCase(args[i + 1]) <= 0) // case 1

{

System.out.print(args[i] + " ");

count++;

if (count >= run_length) run_length = count;

}

else // case 2

{

System.out.print(args[i] + ") (");

count = 1;

runs++;

}

To solve your little dilema, you will need to divide the blocks to look at 3 cases:

if (.......) // case 1

{

// do stuff here for case 1

}

else if (....) // case 2

{

// do stuff here for case 2

}

else // case 3

{

// do stuff here for case 3

}

You are going to have to figure out how to spit this up though.

petes1234a at 2007-7-12 20:54:59 > top of Java-index,Java Essentials,Java Programming...
# 5
Cheers. I appreciate the tips as well! :)
Zortoflavena at 2007-7-12 20:54:59 > top of Java-index,Java Essentials,Java Programming...