-1

I am trying to solve a problem in string concatenation But i don't understand it that why it only give me output like this While I am Using a "+" operator. Can anyone help me to clarify what is my problem . My code is

public static void main(String[] args) {
       int a;
       double b;
       String c;

       Scanner sc=new Scanner(System.in);
       a=sc.nextInt();
       b=sc.nextDouble();
       c=sc.nextLine();
       System.out.println(a+4);
       System.out.println(b+4.0);
       System.out.println("Hackerrank"+" "+c);


    } 

My input is:

12

4.0

is the best place to learn and practice coding!

My output is :

16

8.0

Hackerrank

But Expected Output is:

16

8.0

HackerRank is the best place to learn and practice coding!

3 Answers3

1

The problem is not with the concatenation. It's the line c=sc.nextLine();. When you use c=sc.nextLine(); JVM assigns the value in the b=sc.nextDouble(); line but after the double value.

Example: According to your input,

12

4.0 [c=sc.nextLine(); line reads this part. Just after the Double input]

is the best place to learn and practice coding!

So try this code. It skips the line which mentioned above.

public static void main(String[] args) {
       int a;
       double b;
       String c;

       Scanner sc=new Scanner(System.in);
       a=sc.nextInt();
       b=sc.nextDouble();

       sc.nextLine(); // This line skips the part, after the double value.

       c=sc.nextLine();
       System.out.println(a+4);
       System.out.println(b+4.0);
       System.out.println("Hackerrank"+" "+c);


    } 
0

Scanner moves the scanner to the next line when you call nextLine() method. But that returns the line it skips. If you provide the input like in a single line
"12 4.0 is the best place to learn and practice coding! " or "12 " in first line and press enter "4.0 is the best place to learn and practice coding!" you will get the desired result.

From JavaDOC

/** * Advances this scanner past the current line and returns the input * that was skipped. * * This method returns the rest of the current line, excluding any line * separator at the end. The position is set to the beginning of the next * line. * *

Since this method continues to search through the input looking * for a line separator, it may buffer all of the input searching for * the line to skip if no line separators are present. * * @return the line that was skipped * @throws NoSuchElementException if no line was found * @throws IllegalStateException if this scanner is closed */

Dinesh
  • 868
  • 1
  • 6
  • 17
  • Please format your answer, because currently it's a bit of a mess (because of all the asteriks `*`) – Lino Jul 20 '18 at 05:34
  • @Lino if you are unhappy with the formatting, you could make an edit yourself. – Paul Rooney Jul 20 '18 at 05:37
  • @PaulRooney of course but I don't really have time, that's why I left the comment as a suggestion – Lino Jul 20 '18 at 05:38
  • It is clearly mentioned what you provide as input in the answer. You have to either provide the complete the input in a single line seperated by spaces. Or you can provide the input in two lines, like 12 in first line and 4.0 and any text in second line. As next line moves the scanner to next line returning the text from the last line. – Dinesh Jul 20 '18 at 05:41
  • I got your answer, It is running fine. But my problem is how to get the same desired output if my Custom input won't change. – Manpreet gujral Jul 20 '18 at 05:51
0

Print only c first, it gives blank value. It is not assigning values to c. System.out.println(c);

Problem is not with + operator if you use

System.out.println("Hackerrank"+" "+sc.nextLine());

Then also you will get expected output.

Means you need to write following lines: c=sc.nextLine(); c=sc.nextLine(); Then it will consider expected line in variable c.

Vivek Pakmode
  • 847
  • 2
  • 7
  • 22