Question

I am not a total newbie but I got caught in a strange problem while taking input through console in java! Here is my code:

package com.test;
import java.io.*;
import java.util.Scanner;

public class EvenSum {

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int a1 = Integer.parseInt(br.readLine());
    // tried through scanner class even
    Scanner scan = new Scanner(System.in);
    int a3 = scan.nextInt();

    String input = br.readLine();
    int a2 = Integer.parseInt(br.readLine());

    DataInputStream in = new DataInputStream(System.in);
    int a = in.readInt();

    scan.close();
}
}

I have tried doing it by all the four ways separately but the execution of the code simply doesn't end it keeps going on and on. Did I break something? Thanks in advance!

Was it helpful?

Solution

you have used br.readLine() 4 times and scan.nextInt(); 1 time your code will ask to input value 5 times, instead create object of Scanner and read int or String according to your requirement.

OTHER TIPS

Try this:

Scanner input = new Scanner(System.in);
int num = input.nextInt();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top