Hello, I am new to Java programming and I just got the NetBeans IDE. One problem that I've been having with Java is the input.

package learning;

public class Main
{
    public static void main(String[] args)
    {
        int num1 = 0, num2 = 0;

        cin >> num1 >> num2; // How do I do this in Java??

        System.out.println((num1+num2));
    }
}

How do I do that in Java? In C++, its too easy. Mainly you never need to use anything other than cin and getline... What do I use in Java??


Thanks

Recommended Answers

All 4 Replies

There is a class called BufferedReader which you can use. You can try this code, i had it lying around, so i hope it still works, havent had to do such things in a long time:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine();
    System.out.println(input);
    br.close();
commented: thanks! +1

There is a class called BufferedReader which you can use. You can try this code, i had it lying around, so i hope it still works, havent had to do such things in a long time:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine();
    System.out.println(input);
    br.close();

To use this, which libraries do I have to import? I get those red lines under the two Bufferedreader and InputStreamReader. How do I fix that?

Hi,

you need to import java.io.*, and also surround with a try/catch clause.

You can also use Scanner to read input.

Scanner sc = new Scanner(System.in);
  sc.nextLine();
  sc.close();
commented: thanks! +1

Thank you di2daer and Thirusha for your help! I finally got it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.