3

Input Format

  • The first line contains an integer, .
  • The second line contains a double, .
  • The third line contains a string, .

Output Format

Print the sum of both integers on the first line, the sum of both doubles (scaled to decimal place) on the second line, and then the two concatenated strings on the third line.Here is my code

package programs;

import java.util.Scanner;


public class Solution1 {

    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "Programs ";

        Scanner scan = new Scanner(System.in);
        int i1 = scan.nextInt();
        double d1 = scan.nextDouble();
        String s1 = scan.next();

        int i2 = i + i1;
        double d2 = d + d1;
        String s2 = s + s1;
        System.out.println(i2);
        System.out.println(d2);
        System.out.println(s2);
        scan.close();
    }
}

Input (stdin)

12
4.0
are the best way to learn and practice coding!

Your Output (stdout)

16
8.0
programs are

Expected Output

16
8.0
programs are the best place to learn and practice coding!
Cœur
  • 32,421
  • 21
  • 173
  • 232
Ketan G
  • 487
  • 1
  • 4
  • 19
  • 1
    It's great that you've provided a complete program here - in future, it would be good if you could reduce it to a *minimal* example. Given that the bits reading numbers already work, you could have reduced this to basically `Scanner scan = new Scanner(System.in); String s1 = scan.next(); System.out.println(s1);` – Jon Skeet Sep 22 '16 at 06:15
  • A [Scanner](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()) breaks its input into tokens **using a delimiter pattern, which by default matches whitespace**.. public String next() Finds and returns the next complete token from this scanner. **A complete token is preceded and followed by input that matches the delimiter pattern**. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true. – Tibrogargan Sep 22 '16 at 06:16
  • i can't see how come this question is with +3 ,have seen far better question going down with a negative repo without any reasons , seems like the guy did his magic again. – Pavneet_Singh Sep 22 '16 at 06:49

4 Answers4

3

Scanner.next() reads the next token. By default, whitespace is used as a delimited between tokens, so you're only getting the first word of your input.

It sounds like you want to read a whole line, so use Scanner.nextLine(). You need to call nextLine() once to consume the line break after the double though, as per this question.

// Consume the line break after the call to nextDouble()
scan.nextLine();
// Now read the next line
String s1 = scan.nextLine();
Community
  • 1
  • 1
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • if i am using Scanner.nextLine() then it is also giving me unexpected ouput. please check it out – Ketan G Sep 22 '16 at 06:22
  • @KetanGupta: What do you mean by "please check it out"? Check what out? You haven't told us what you're getting... – Jon Skeet Sep 22 '16 at 06:23
  • Now i am getting this 12 4.0 16 8.0 programs as my output – Ketan G Sep 22 '16 at 06:24
  • @KetanGupta: Ah - that's due to http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo. Will edit. – Jon Skeet Sep 22 '16 at 06:25
3

You are using scan.next() which reads value each with space as delimiter.

But here you need to read complete line so use

String s1 = scan.nextLine();
Naruto
  • 3,847
  • 1
  • 18
  • 30
1

All you need to do is change

String s1 = scan.next();

to

    String s1 = scan.nextLine();
Sumit Badaya
  • 4,599
  • 2
  • 14
  • 25
1

You need to use scan.nextLine() which will read a complete line and scan.next() read value each with space as delimiter.

package programs;

import java.util.Scanner;


public class Solution1 {

    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "Programs ";

        Scanner scan = new Scanner(System.in);
        int i1 = scan.nextInt();
        double d1 = scan.nextDouble();
        String s1 = scan.nextLine();

        int i2 = i + i1;
        double d2 = d + d1;
        String s2 = s + s1;
        System.out.println(i2);
        System.out.println(d2);
        System.out.println(s2);
        scan.close();
    }
}
Farhan Yaseen
  • 1,971
  • 2
  • 17
  • 31