Sunday, September 29, 2019

java - 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



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


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...