Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: I can't find the solution to this interesting problem

1. I can't find the solution to this interesting problem

Assume that someone dictates you a sequence of numbers and you need to write it down. For brevity, he dictates it as follows: first says the number of consecutive identical numbers and then says the number itself. E.g. The sequence 1 1 3 3 3 2 2 2 2 14 14 14 11 11 11 2 will be dictated as "Two ones, three threes, four twos, three fourteens, three elevens, one two", so you will write down the sequence 2 1 3 3 4 2 3 14 3 11 1 2. The challenge is to write the program which compresses the given sequence using this approach.

Input:
Your program should read lines from standard input. Each line is a sequence of L integers, where each integer is N, separated by a whitespace. N is in range [0, 99]. L is in range [1, 400].

Output:
For each test case, produce a single line of output containing a compressed sequence of numbers separated by a single space char.

```import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.util.Scanner;
public class Main {
/**
* Iterate through each line of input.
*/
public static void main(String[] args) throws IOException {
int number = 0;
int numberOfParagraph = 0;
int numberOfSequence = 1;
char ch;
HashMap<Integer, Integer> map = new HashMap<>();
String line;
while ((line = in.readLine()) != null) {
if(line.equals('\n'))
{
numberOfParagraph++;
}
if(numberOfParagraph < 1 || numberOfParagraph > 400)
{
break;
}
for (int index = 0; index < line.length(); index++)
{
//number = line.parseInt(line);
ch = line.charAt(index);
number = ch - '0';
if(number < 0 || number > 99)
{
continue;
}
if(!map.containsKey(number))
{
map.put(number, 1);
}
else
{
numberOfSequence++;
map.put(number, numberOfSequence);
}
}
//System.out.println(line);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " ocurrs " + entry.getValue()+ " times");
}
}
}```

2. Re: I can't find the solution to this interesting problem

Do you have any specific questions about the posted code?

If there is output, copy and paste it here with comments.

3. Re: I can't find the solution to this interesting problem

hello,
I can't outpput it to the console. I know that I have right solution when I used the hashmap. but i can't out put it in the console.
thanks

4. Re: I can't find the solution to this interesting problem

What have you tried? I see a println statement in the code.
Have you tried using a print statement?

Keep adding print statements until one of them prints. Then look at the code between the one that printed and the one that did not print to see why the second one was not executed.

5. Re: I can't find the solution to this interesting problem

Hi Norm,
Thank you so much for helping me. I am still kind of stuck but i got better result from the last post. I still don't have the desire output.

HERE IS THE INPUT
40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86 86 86 86 86 86 86 86 86 8

HERE IS MY OUTPUT
3 40
10 29
11 57
15 92
24 86

THIS IS MY DESIRE OUTPUT
4 40 8 29 2 57 5 92 10 86
```import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.util.Scanner;
import java.io.*;
public class Main {
/**
* Iterate through each line of input.
*/
public static void main(String[] args) throws IOException {
int number = 0;
int  numberOfParagraph = 0;
int numberOfSequence = 0;
char ch;
String line;
//String str = new String;
//String [] temp;
while ((line = in.readLine()) != null) {

numberOfParagraph++;
String str = new String(line);
String [] temp = str.split(" ");

if(numberOfParagraph < 1 || numberOfParagraph > 400)
{
break;
}
for (int index = 0; index < temp.length; index++)
{
number = Integer.parseInt(temp[index]);
//number = line.parseInt(line);
//ch = line.charAt(index);
//number = ch - '0';
if(number > 0 && number < 99)
{
//continue;
//}
if(!map.containsKey(number))
{
map.put(number, 1);
}
else
{
numberOfSequence++;
map.put(number, numberOfSequence);
}
}
}
//System.out.println(line);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println( entry.getValue() + " " + entry.getKey());
}
}
}```

6. Re: I can't find the solution to this interesting problem

Ok, looks like you are making progress. Time for more debugging.
Change the input String to the smallest, simplest possible to save print extra lines in the debugging.

Look at the values in the first column of the current output. The value is always increasing. That would suggest there is a variable that needs to be unique for each value vs being shared by all the values.

7. Re: I can't find the solution to this interesting problem

I got the answer that I wanted thank you very much Norm