Dan Dan - 5 months ago 120
Java Question

How to read and save an Integer, Double, and String to a variable?

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

Answer

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