0
0
Javaprogramming~20 mins

Using Scanner class in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scanner Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Reading an integer and a string using Scanner
What is the output of this Java program when the user inputs:
42
Hello
Java
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    sc.nextLine();
    String text = sc.nextLine();
    System.out.println("Number: " + num);
    System.out.println("Text: " + text);
  }
}
A
Number: 42
Text: Hello
B
Number: 42
Text: 
C
Number: 42
Text: \nHello
D
Number: 42
Text: null
Attempts:
2 left
💡 Hint
Remember that nextInt() does not consume the newline character.
Predict Output
intermediate
2:00remaining
Reading multiple tokens with Scanner
What will be the output of this program if the user inputs:
John 25 true
Java
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String name = sc.next();
    int age = sc.nextInt();
    boolean isStudent = sc.nextBoolean();
    System.out.println(name + ", " + age + ", " + isStudent);
  }
}
AJohn 25 true
BCompilation error
CJohn, 25, false
DJohn, 25, true
Attempts:
2 left
💡 Hint
next() reads one word, nextInt() reads an integer, nextBoolean() reads true/false.
🔧 Debug
advanced
2:00remaining
Why does this Scanner code skip input?
This code is supposed to read an integer and then a full line of text. But it skips reading the text input. Why?
Identify the problem and select the correct explanation.
Java
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number: ");
    int n = sc.nextInt();
    sc.nextLine();
    System.out.print("Enter text: ");
    String line = sc.nextLine();
    System.out.println("Number: " + n);
    System.out.println("Text: " + line);
  }
}
AnextLine() cannot be used after nextInt() because it causes an infinite loop.
BnextInt() leaves a newline in the buffer, so nextLine() reads it as empty.
CThe Scanner object must be closed before reading the next line.
DThe program needs to call sc.next() before nextLine() to clear the buffer.
Attempts:
2 left
💡 Hint
Think about what happens to the newline character after nextInt().
📝 Syntax
advanced
2:00remaining
Identify the syntax error in Scanner usage
Which option contains a syntax error when trying to read a double value using Scanner?
Adouble d = sc.nextDouble();
Bdouble d = sc.nextDouble("");
Cdouble d = sc.nextDouble;
Ddouble d = sc.nextdouble();
Attempts:
2 left
💡 Hint
Method names in Java are case-sensitive.
🚀 Application
expert
2:00remaining
How many tokens are read by Scanner?
Given this input line:
apple, banana; cherry orange
and this code:
Scanner sc = new Scanner(System.in); sc.useDelimiter(",|;| "); int count = 0; while(sc.hasNext()) { sc.next(); count++; } System.out.println(count);
What is the output?
Java
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    sc.useDelimiter(",|;| ");
    int count = 0;
    while(sc.hasNext()) {
      sc.next();
      count++;
    }
    System.out.println(count);
  }
}
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Count the words separated by comma, semicolon, or space.