0

For a online course I'm taking,I'm trying to save the 2nd set of integer, double, and string that I defined to variables, after reading them (the 2nd set) using a scanner. The problem is I don't know how to do that to the 2nd set of variables that I defined. I've tried instantiating them to a new variable,but I keep running into errors. I need help to read each variable and then save them.

 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 ";


    int j = 4;
    double y = 9.0;
    String k = "is the best place to learn and practice coding!";


    int j = new j();
    double y = new y();
    String k = new k();
    j.scanner.nextInt();
    y.scanner.nextDouble();
    k.scanner.nextLine();


    System.out.print(j + i);
    System.out.print(d + y);
    System.out.print(s + k);
dabberson567
  • 43
  • 2
  • 2
  • 11

9 Answers9

3

You use assignment without declaring the type again.

int j = 4;
double y = 9.0;
String k = "is the best place to learn and practice coding!";

j = scanner.nextInt();
y = scanner.nextDouble();
k = scanner.nextLine();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2
    int j = 12;
    double y = 4.0;
    String k = "is the best place to learn coding!";


    Scanner scanner = new Scanner( System.in );

    j = Integer.parseInt(scanner.nextLine());
    y = Double.parseDouble(scanner.nextLine());
    k = scanner.nextLine();

    //Ensure you print in new line
    System.out.print(j + i);
    System.out.print("\n");
    System.out.print(d + y);
    System.out.print("\n");
    System.out.print(s + k);
k9x
  • 81
  • 5
2

A call to nextLine() may return an empty string if there are no characters between the end of the last read and the beginning of the next line.

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

So to complete that challenge use the above code when you read the input from the user. And the entire code goes as follows.

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);
int i1;
double d1;
String s1;

i1 = scan.nextInt();
d1 = scan.nextDouble();
s1 = scan.nextLine();
s1 = scan.nextLine();

System.out.println(i+i1);
System.out.println(d+d1);
System.out.println(s+s1);
scan.close();
}
}
0

All you need to do is the following:

Scanner scanner = new Scanner(System.in);//instantiate a Scanner object

int j = scanner.nextInt();//Use the Scanner object to read an int value from the user
double y = scanner.nextDouble();//Use the Scanner object to read an double value from the user
String k = scanner.nextLine();//Use the Scanner object to read an line  from the user
theVoid
  • 743
  • 4
  • 14
0
     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 ";


        int j = 4;
        double y = 9.0;
        String k = "is the best place to learn and practice coding!";

Scanner s = new Scanner(System.in);    

        j = Integer.parseInt(s.nextLine());
        y = Double.parseDouble(s.nextLine());
        k = s.nextLine();


        System.out.print(j + i);
        System.out.print(d + y);
        System.out.print(s + k);
}
}
Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58
0
int k = scan.nextInt();
double l = scan.nextDouble();
scan.nextLine();
String f = scan.nextLine();
System.out.println(k + i);
System.out.println(l + d);
System.out.println(s+f);
0

The following code will give you the result

int j;
double y; 
String k=null; 

Scanner scan = new Scanner(System.in);
j= scan.nextInt();
y=scan.nextDouble();

while(scan.hasNext()){
k =scan.nextLine();
}

System.out.println(i+j);
System.out.println(d+y);
System.out.println(s+k);   
CdrAshw
  • 1
  • 1
0
            int i1= sc.nextInt();
            double d1 = sc.nextDouble();
            String s1 = sc.next();
            System.out.println(i+i1);
            System.out.println(d+d1);
            System.out.println(s+s1);

you can also follow this if u don't have any spaces for your s1 string.

0
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);


    int j = 4;
    double y = 9.0;
    String k = "is the best place to learn and practice coding!";

    Scanner f = new Scanner(System.in);    

    j = Integer.parseInt(f.nextLine());
    y = Double.parseDouble(f.nextLine());
    k = f.nextLine();


    System.out.println(j + i);
    System.out.println(d + y);
    System.out.println(s + k);


           scan.close();
}

}

  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Dec 20 '16 at 19:52