# Thread: Prompt algorithm

1. Member
Join Date
Jul 2011
Posts
10
Rep Power
0

## Prompt algorithm

Input file:
Java Code:
```ddd
bbb
ccc
aaa
bbb
aaa
bbb```
output file:
Java Code:
```bbb -3
aaa - 2```
duplicate lines, indicating the number of repetitions
what algorithm?

2. 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.

3. Member
Join Date
Jul 2011
Posts
10
Rep Power
0
??
I can not think of an algorithm(

4. 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.

5. To start: write a small program that reads in the input file and prints its contents to the screen.

6. Member
Join Date
Jul 2011
Posts
10
Rep Power
0
Java 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]);
}
}
}```

7. That will do for testing.
Now how are you going to count the repetition of each String?

8. Member
Join Date
Jul 2011
Posts
10
Rep Power
0
Java 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
}
}```

9. 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?

10. Member
Join Date
Jul 2011
Posts
10
Rep Power
0
Java 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
Java Code:
```Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at SortString.main(gg.java:91)```

11. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•