Java program cannot read string

import java.util.*;

class DataTypes {

    public static void main(String args[]) {
        int i = 4;
        double d= 4.0;
        String s = "Hackerrank";
        Scanner scan = new Scanner(System.in);
        int j = scan.nextInt();
        double d1 = scan.nextDouble();
        String s1 = new String();
        s1 = scan.nextLine();
        j = j+i;
        System.out.println(j);
        d1 = d1+d;
        System.out.println(d1);
        System.out.println(s+" "+s1);
        scan.close();
    }
}

See the code above, it is free from errors, but after entering the binary code, the program does not read the line that directly displays the output. But when I comment on the scan instructions int and double, then the programs read my line. Why is this so?

+3
source share
7 answers

You do not read the line as you want, just use the code below to make it work the way you want.

s1 = scan.next();

For more information see the link

EDIT:
If your input contains a string with spaces, you can do something like:

scan.nextLine();
s1 = scan.nextLine();

- nextDouble(), , , , nextLine(). newLine() String , .

+7

s1 = scan.nextLine();

s1 = scan.next();
0

:

import java.util.*;
class Demo{
public static void main(String args[]){
  int i = 4;
  double d= 4.0;
  String s = "Hackerrank";
  Scanner scan = new Scanner(System.in);
  int j = scan.nextInt();
  double d1 = scan.nextDouble();
  String s1  = scan.next();
  j = j+i;
  System.out.println(j);
  d1 = d1+d;
  System.out.println(d1);
  System.out.println(s+" "+s1);
  scan.close();
 }
}
0

 s1 = scan.nextLine();

 s1 = scan.next();

.

0

- , :, , , :

int j = scan.nextInt(); scan.nextLine();

import java.util.*;

class DataTypes {

    public static void main(String args[]) {

        int i = 4;
        double d = 4.0;
        String s = "Hackerrank";
        Scanner scan = new Scanner(System.in);

//      System.out.print("Enter j: ");
        int j = scan.nextInt();
        scan.nextLine();
//      System.out.print("Enter dl: ");
        double d1 = scan.nextDouble();
        scan.nextLine();

        String s1 = new String();
//      System.out.print("Enter s1: ");
        s1 = scan.nextLine();
        j = j + i;
        System.out.println(j);
        d1 = d1 + d;
        System.out.println(d1);
        System.out.println(s + " " + s1);
        scan.close();
    }
}
0

double d1 = Double.parseDouble(scan.nextLine());
0

- , nextDouble , .

:

  • 4 : 0.1\n
  • 0.1\n
  • , , \n
  • : \n
  • , .

:

:

1 3.5I'm a short string

:

        double d1 = scan.nextDouble();
        scan.nextLine();
        String s1 = scan.nextLine();

next nextLine:

,

        double d1 = scan.nextDouble();
        String s1 = scan.next();

Read double using the call to the following line:

This will limit your program to new numbers separated by a newline, instead of allowing any delimiter:

int j = Integer.parseInteger(scan.nextLine());
double d1 = Double.parseDouble(scan.nextLine());
String s1 =scan.nextLine();
0
source

Source: https://habr.com/ru/post/1690424/


All Articles