This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
import java.util.Scanner;
import static java.lang.System.out;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Safeties;
out.print("Safeties:");
Safeties = input.nextInt();
out.print("Home Team:");
String HomeTeam = input.nextLine();
out.print("Touchdowns:");
double Touchdown2 = input.nextDouble();
}
}
The output is:
Safties:
Home Team:Touchdowns:
But it's supposed to be
Safties:
Home Team:
Touchdowns:
I'm assuming it's because I have an int above a string but I don't know how to fix this problem. I need them in that order. I'm completely lost. Can anyone help?
Add input.nextLine(); below out.print("Home Team:");
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was working on a project and I figured out that when I read more than one String from one obj of Scanner it may skip one and it's solved when I make more than 1 obj but why does that happen I mean what it's the cycle that processor go through !!
in this code variable z is skipped from reading and the program end so any one could help me figuring this out please and thanks in advance
package calculater;
import java.util.Scanner;
public class Calculater {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String x = scan.nextLine();
double y = scan.nextDouble();
String z = scan.nextLine();
}
}
You could try to write
double y = Double.parseDouble(scan.nextLine());
instead of
double y = scan.nextDouble();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was trying to reverse the String using pre-defined method reverse() which is available in StringBuffer
I took input from the user and while printing the reverse string I used toString() method to avoid an extra space
import java.util.*;
public class Small {
public static void main(String a[])
{
int num;
Scanner sc=new Scanner(System.in);
num = sc.nextInt();
while(num>0)
{
String t;
t=sc.nextLine();
StringBuffer sb=new StringBuffer(t);
sb.reverse();
System.out.println(sb.toString());
num--;
}
}
}
Input:
2
hello
welcome
Output:
<Empty line>
olleh
Can anyone please advise why this blank space is coming and also not getting second output?
sc.nextInt() doesn't consume the newline at the end of the line containing the number.
Add sc.nextLine(); after it:
num = sc.nextInt();
sc.nextLine();
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm trying to take numbers (integers) from a website and put them into an array for further manipulation. Here is what I have so far
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class Scores {
public static void main(String[] args) throws IOException{
URL url = new URL("http://cs.armstrong.edu/liang/data/Scores.txt");
Scanner input = new Scanner(url.openStream());
int []score = new int [100];
int i = 0;
while (input.hasNextInt()) {
score[i++] = input.nextInt();
System.out.println(score);
}
}
}
The problem is all of the numbers in the array come back as [I#6bc7c054, any help would be appreciated.
System.out.println(score[i]);
you were printing the array itself
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I'm trying to learn Java through Hackerrank and the challenge that I'm working on currently takes an int, double, and string and prints them on separate lines in reverse order, but I haven't been able to get the string to print.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
double y=sc.nextDouble();
String s=sc.nextLine();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
}
}
The input is:
42
3.1415
Welcome to Hackerrank Java tutorials!
And output is:
String:
Double: 3.1415
Int: 42
I don't know Java at all, but from the code I've seen online, I can't tell why this is wrong.
Change the first part of the code to this:
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
double y = sc.nextDouble();
sc.nextLine(); // Discard rest of current line
String s = sc.nextLine();
The way java.util.Scanner splits the input into numbers or lines is a bit weird.
The sc.nextLine(); should instead be sc.next();
This question already has answers here:
java.util.NoSuchElementException - Scanner reading user input
(5 answers)
Closed 7 years ago.
I have really tried to find the answer through the threads but still hope to get some feed back.
The code below is bad style I think but I don't know why it shoot me a
java.util.NoSuchElementException after enter the number since I make two Scanner objects for two methods and I should be able to start a new input. And if I erase the input.close() in inputAndPrintNumber(), it works and compile correctly. I really hope to know why and how to fix it if I still use two Scanner obj and without erasing the input.close() if possible.
import java.util.*;
public class t{
public static void main(String [] args){
inputAndPrintNumber();
inputAndPrintString();
}
public static void inputAndPrintNumber(){
Scanner input = new Scanner(System.in);
String s = input.nextLine();
System.out.print(s);
input.close();
}
public static void inputAndPrintString(){
Scanner input2 = new Scanner(System.in);
int a = input2.nextInt();
System.out.print(a);
}
}
I don't even sure whether the code below is better or any better idea?
import java.util.*;
public class t{
public static Scanner input = new Scanner(System.in);
public static void main(String [] args){
inputAndPrintNumber();
inputAndPrintString();
input.close();
}
public static void inputAndPrintNumber(){
String s = input.nextLine();
System.out.print(s);
}
public static void inputAndPrintString(){
int a = input.nextInt();
System.out.print(a);
}
}
When you call scanner.close() it not only closes scanner, but also stream from which it reads data, in this case System.in. So if you are going use System.in later don't close it (if it is closed, we can't reopen it and read any data from it, hence exception).
Your second code example solves this problem because Scanner is being closed when you are sure that nothing else will be read from input stream.
BTW it seems that you mixed places where nextLine and nextInt should be invoked (nextLine seems to be more appropriate for inputAndPrintString while nextInt for inputAndPrintNumber).