-1

I must do an exercise from the book. The exercise is:

Write a filter that reads in a sequence of integers and prints the integers, removing repeated values that appear consecutively. For example , if the input is 1 2 2 1 5 1 1 7 7 7 7 1 1 1 1 1 1 1 1 1 , your program should print 1 2 1 5 1 7 1.

public class B1_5_6
{
  public static void main(String[] args)
  {
    int waarde1 = StdIn.readInt(); //eerste waarde die je invult
    String reeks = waarde1 + " "; //die mag dus altijd geprint worden

    while (!StdIn.isEmpty())
    {
      int waarde2 = StdIn.readInt(); //volgende waardes die je invult
      if (!(waarde1 == waarde2)) // als die niet hetzelfe is dan de voorgaande waarde, mag je dat getal ook uitprinten
      {
        waarde1 = waarde2;
        reeks = reeks + waarde2 + " "; // reeks is de waardes die al uitgeprint mogen worden en daar komt de nieuwe ingevulde waarde bij
      }
    }
    StdOut.println(reeks);
  }
}

My question is:

I do not understand the second line: String reeks = waarde1 + " "; //die mag dus altijd geprint worden

What does this " " mean? It means you can fill whatever you want in right? Or does it mean empty??

Maybe a simple thing to understand for people, but I do not understand this hole line and why you make it. I get it that you need the numbers in sequence after each other without getting a number equal to the number in front of it.. like 2 2 makes 2. but I do not understand what this basic

" " mean. i made alot of exercises alrdy but I still do not get what this means, and I can not find it anywhere.

Thanks for the detailed description and explanations.

María Antignolo
  • 388
  • 4
  • 17
  • 1
    javascript and java are two different language. – Abhishek Anand Nov 21 '19 at 12:06
  • 1
    ```" "``` is a space (een spatie in het Nederlands). The double quotes delimite the start and the end of the string, so the value is what's between the quotes. And it is a space. – Natrium Nov 21 '19 at 12:07
  • You're building up a string in the format: `number + space + number + space + number`. " " is a String of 1 space – CocoNess Nov 21 '19 at 12:18

3 Answers3

1

It’s used as a separator in a string. Basically numbers separated by a space

nickolay.laptev
  • 2,253
  • 1
  • 21
  • 31
0

The String 'reeks' creates a string with the value and adds a space after it. The string " " is what includes the space. It's just a separator to make it easier to read.

Sverre
  • 54
  • 4
0

Let's say you have three strings contains numbers.

String a = "123", b = "456", c= "789";

If you want to merge this strings you need to use + operator.

String d = a + b; // 123456
String e = a + c  // 123789

Sometimes you need to add a separator between those. So let's say we want to add an empty space

String f = a + " " + b // 123 456
String g = a + " " + c // 123 789

It's easy to add such that separators but when you use loops such as for and while It's harder to understand. Let's say you have an array contains numbers as strings.

String[] arr = {"12", "34", "56", "78","9"};

and you want to merge them using for

String merged = "";
for (int i = 0; i < arr.length; i++) {
    merged = merged + arr[i];
}
System.out.println(merged) // 123456789

but if you want a empty space between each merge. You need to add empty space after merging the next element. You can see the pattern below.

merged = merged + arr[i] + " ";

//In iteration 1
merged = "" + "12" + " "; // "12 "

//In iteration 2
merged = "12 " + "34" + " "; // "12 34 ";

//In iteration 3
merged = "12 34 " + "56"+ " "; // "12 34 56 "; 
Doğukan HAN
  • 123
  • 2
  • 6