# Prompt algorithm

• 07-14-2011, 04:58 PM
eIO
Prompt algorithm
Input file:
Code:

```ddd bbb ccc aaa bbb aaa bbb```
output file:
Code:

```bbb -3 aaa - 2```
duplicate lines, indicating the number of repetitions
what algorithm?:frusty:
• 07-14-2011, 05:09 PM
KevinWorkman
You might want to see the link in my signature on asking smart questions. How would you do this by hand, with a piece of paper and a pencil and no computer? Write that out, and there's your algorithm.
• 07-14-2011, 05:15 PM
eIO
??
I can not think of an algorithm(
• 07-14-2011, 05:19 PM
KevinWorkman
We aren't going to think of it for you, that's not really how this works.

Think about it logically- write this out on a piece of paper, and solve it. How did you do it? How did you come up with the expected output in your original post? Think about it step by step.
• 07-14-2011, 06:21 PM
Norm
To start: write a small program that reads in the input file and prints its contents to the screen.
• 07-14-2011, 06:37 PM
eIO
Code:

```class SortString {         static String arr[] =     {"ddd", "sss", "    aaa", "ggg", " aaa ", "bbb",        "ddd", "ccc",  };   public static void main(String args[]){   for(i=0;i<=arr.length;i++){   System.out.println(""+arr[i]); } } }```
• 07-14-2011, 06:41 PM
Norm
That will do for testing.
Now how are you going to count the repetition of each String?
• 07-14-2011, 07:12 PM
eIO
Code:

```class SortString {         static String arr[] =     {"ddd", "sss", "    aaa", "ggg", " aaa ", "bbb",        "ddd", "ccc",  };   public static void main(String args[]){   for(i=0;i<=arr.length;i++){   System.out.println(""+arr[i]); int k = o;   for(j=0;j<=arr.length;j++) {if (arr[0].equals(arr[j]))   //counter  k++;//or  k=k+1;// i do not know(( } int p = o;   for(j=0;j<=arr.length;j++) {if (arr[1].equals(arr[j]))   //counter  p++;//or  p=p+1;// i do not know(( } //etc } }```
• 07-14-2011, 07:20 PM
Norm
Before you try to write code, you need to design what the code is supposed to do.
Can you explain the steps your code will need to take to solve your problem?
• 07-14-2011, 11:15 PM
eIO
Code:

```int k=o; for(i=0;i<=arr.length;i++){     for(j=0;j<=arr.length;j++){           if(arr[i].equals(arr[j])) {               g=g+1;          \\after each comparison, increment counter                           }             System.out.println(g);          \\print count after all the iterations of the j             System.out.println(arr[i]);            \\print the current element       } \\ след. i = 1 и т.д. }```
Console
Code:

```Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at SortString.main(gg.java:91)```
:=(:
• 07-14-2011, 11:34 PM
Norm
Before you try to write code, you need to design what the code is supposed to do.
Can you explain the steps your code will need to take to solve your problem?

What is the size of the array at line 91? The index value of 7 is past the end of the array.