#Scanner acting up

23 messages · Page 1 of 1 (latest)

cedar sonnet
#

Either I'm stupid or something ain't right

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        Double d = scan.nextDouble();
        String s = scan.nextLine();


        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}```
rotund depotBOT
#

This post has been reserved for your question.

Hey @cedar sonnet! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

glossy meadow
#

does not seem like a problem to me

#

o

#

found

#

use .next()

cedar sonnet
#

Yeah, and if I add .useDelimiter(" ") it gets the string but also spawns a bunch of errors

cedar sonnet
glossy meadow
#

one word?

#

i dont know why its doing that, it is working here

warm jasper
#

since after nextDouble there is leftover \n in scanner's buffer, as you call nextLine() it reaches the \n and immediately returns empty line. To avoid it you need to consume that leftover first, calling nextLine() once, and then call it again with assigning it's value to variable

glossy meadow
#

just shift .next() before .nextInt and .nextDouble

#
        Double d = scan.nextDouble();
        String s = scan.nextLine();```
#
String s = scan.next();
int i = scan.nextInt();
Double d = scan.nextDouble();```
cedar sonnet
glossy meadow
#

oh yeah it says there

#

follow alexanderrs advice then

rotund depotBOT
#

If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.

rotund depotBOT
# cedar sonnet Works, thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.