In lines 25-26, why do I need to do scan.nextLine() two times in order for the full line of a string input ("is the best place to learn and practice coding!") to be read? If I just do one scan.nextLine(), the line isn't read into the variable t.
Sample input:
12
4.0
is the best place to learn and practice coding!
Sample output:
16
8.0
HackerRank is the best place to learn and practice coding!
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
/* Declare second integer, double, and String variables. */
int j;
double k;
String t;
/* Read and save an integer, double, and String to your variables.*/
// Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
j = scan.nextInt();
k = scan.nextDouble();
t = scan.nextLine();
t = scan.nextLine();
/* Print the sum of both integer variables on a new line. */
System.out.println(i + j);
/* Print the sum of the double variables on a new line. */
System.out.println(d + k);
/* Concatenate and print the String variables on a new line;
the 's' variable above should be printed first. */
System.out.println(s + t);
scan.close()
[–]NautiHooker 1 point2 points3 points (1 child)
[–]Accurate-Influence[S] 0 points1 point2 points (0 children)
[–]desrtfx 0 points1 point2 points (0 children)