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);
4
  • Please share the error message you saw and how do you want variables to be saved. Commented Apr 24, 2016 at 7:22
  • 1
    Doesn't your IDE show errors at int j = new j(); double y = new y(); String k = new k();? Because int j, double y and String k are already defined in the scope. Commented Apr 24, 2016 at 7:22
  • @Apurva frighteningly a high percentage on SO don't use an IDE :( Commented Apr 24, 2016 at 7:23
  • 1
    I think you need to learn more about how to declare and assign variable in Java Commented Apr 24, 2016 at 7:23

9 Answers 9

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();
Sign up to request clarification or add additional context in comments.

Comments

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();
}
}

Comments

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

Comments

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

Comments

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

1 Comment

@PeterLawrey , Sorry sir. I missed that.
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);

Comments

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

Comments

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.

Comments

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();
}

}

1 Comment

While this code snippet may solve the question, including an explanation 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.