Link to home
Start Free TrialLog in
Avatar of travishaberman
travishaberman

asked on

How do I take input from standard in in java?

I have been trying to do the most basic thing I can think of and just read a simple line of text from the keyboard into a main program and I just can't seem to make it happen...  All I want to do is what C++ does by saying    ...  cin >> x;   Why is this so hard in JAVA..   Here is my java code..


            BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

            size = Integer.parseInt(strSize = dataIn.readLine());


but I get this error..


Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:415)

I just need to get the input from the keyboard and I can't seem to make it happen... CAN ANYONE HELP???  PLEASE?

-TH
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

BufferedReader dataIn  = new BufferedReader(new InputStreamReader(System.in));
String line = dataIn.readLine();
int i=Integer.parseInt(line);
SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of travishaberman
travishaberman

ASKER

Here is my class.. I have tried your suggestion Dhaest and I get this error..

Exception in thread "main" java.lang.NegativeArraySizeException
    at main.main(main.java:34)

==========================================================================




import java.io.*;
import java.lang.*;

public class main
{
   
    public static void main(String[] args)
    {
        try        
        {
            BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
         
            System.out.println(" Welcome the the Mogrification Simulation");
            System.out.println();
            System.out.println(" Enter the number of Morg Objects you wish to create. ");
            System.out.println();
           
            int size = System.in.read();
            Morg[] array_of_morgs = new Morg[size];
            for (int i = 0; i<size; i++)
                array_of_morgs[i] = new Morg();
           
            boolean done = false;
            while (done == false)
            {
           
                System.out.println();
                System.out.println(" Please select a menu option ");
                System.out.println("   (1) Transmorgrify all objects");
                System.out.println("   (2) Display objects name & level");
                System.out.println("   (3) Display objects name & transmogrification count");
                System.out.println("   (4) Exit Program");
           
                int choice = System.in.read();
                switch(choice)
                {
                    case 1:
                        System.out.println("    Please enter an integer amount");
                        int amount = System.in.read();
                        for (int i=0; i<size; i++)
                            array_of_morgs[i].transmogrify(amount);
                        break;
               
                    case 2:
                        System.out.println(" Below are the Object's name & level, respectively.");
                        for (int i=0; i<size; i++)
                            System.out.println("    " + array_of_morgs[i].level());
                        break;
               
                    case 3:
                        System.out.println(" Below are the Object's name & transmogrification count, respectively");
                        for (int i=0; i<size; i++)
                            System.out.println("    " + array_of_morgs[i].transMog_count());
                        break;
                       
                    case 4:
                        done = true;
                        break;
                }
            }
        }
   
        catch (IOException e)
        {
            System.err.println("Failed");
        }
    }
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
any chance you could tell me how to handel the exception?  

-TH
I have done all that is asked and now I have a new 500 pt problem.. any takers could please visit this new post....


https://www.experts-exchange.com/questions/21378096/Java-help-Please-Why-is-the-standard-in-not-prompting-me-for-input.html


-TH
PS: try adding your java-question in the java-topic...