Problem solving

  • 12
  • Locked
Replace the main method's System.out object with your own reader wrapper similar to the one shown in the lesson. Your reader wrapper should output the solution to the console. Call testString's existing printSomething() method. Restore the System.out variable back to the original stream. Possible o
You can't complete this task, because you're not signed in.
Comments (11)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
LennyMan
Level 25 , Lucca, Italy
9 January 2021, 14:16
Nice task! I suggest to use the split() method
Mateusz
Level 29 , Poland
17 September 2020, 11:52
There is a bug in the verification process. For some reason it accepts: "3 + 6 = 9" instead of "3 + 6 = 9" in one line.
Adrian
Level 27 , Spanish Town, Jamaica
1 September 2020, 06:41
Here's the thing guys. The printSomething method makes a call to sys.out.println. println inserts a newline character into the dynamic byte array (the outputstream) (which may not be \n, on my system, it's \r\n.) you will need to remove this to get the desired output. you could split the newline characters, or simply replace them. the easiest solution would be to change the print something method from
System.out.println("3 + 6 = ");
to
System.out.print("3 + 6 = ");
but i'm not sure if the validator will like the latter.
Justin Smith
Level 29 , Greenfield, USA, United States
20 October 2021, 23:49
You don't have to do that. Assuming you're using String.split(), you have a fixed array size. It's always going to contain 5 elements (the first number, the operand, the second number, the equals sign, and the newline character). So you can reconstruct the string over array[0] to array[3], and add on your calculated result.
Attila
Level 25
27 April 2020, 01:48
The comments below suggesting a quite complicated solution (as it seems for me). I just used split(" ") on the result string, parsed the digits, and had 3 cases for the operators..
Henrique
Level 41 , São Paulo, Brazil
30 June 2020, 20:27
Very nice tip! Thanks!
Switch/Cypher
Level 25 , Bexleyheath, United Kingdom
10 October 2020, 17:36
Yup that's the way imo. Grab the digits and the operator, parse the digits and crack out a switch statement.
Yiheng Niu
Level 26 , Beijing, China
3 March 2020, 17:35
Just like Weichen said:
result = rawString.substring(0, rawString.length() - 1) + c;  // rawString is "3 + 6 = ", c is the addition result.
Maybe there is a bug with CodeGym... Doing so dosen't give me the required output (display "3 + 6 = 9"), but it does pass the verification.
Andrew
Level 29 , Seattle, United States
28 February 2019, 14:16
Hint: there are carriage return and new line characters at the end of the test string. I was getting weird results and it took me a while to narrow it down to this.
Weichen Ouyang
Level 25 , San Jose, United States
18 September 2019, 02:29
I see that trick at first glance, however, I still can't get it right. I did cut off the tail (2 character), append the result, then add it back. It still did not pass the validation. Care to share that one line of code for the final output?
Weichen Ouyang
Level 25 , San Jose, United States
19 September 2019, 23:10
I solve it by cut off one character from the original string. This one is a tricky one.