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
travishabermanAsked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
>int size = System.in.read();
The error is due this line has returned a negative number
Try with:


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();
           
            String line = dataIn.readLine();
            int size = Integer.parseInt(line);
            if (size<1) {
                   ... manage error here....
            }

            Morg[] array_of_morgs = new Morg[size];
            for (int i = 0; i<size; i++)
                array_of_morgs[i] = new Morg();

....etcetera....
0
 
Jaime OlivaresSoftware ArchitectCommented:
BufferedReader dataIn  = new BufferedReader(new InputStreamReader(System.in));
String line = dataIn.readLine();
int i=Integer.parseInt(line);
0
 
Jaime OlivaresSoftware ArchitectCommented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DhaestCommented:
import java.io.*;
import java.lang.*;

public class foo
{
     public static void main(String[] args)
     {
          try
          {
               int[] test = new int[80];
               System.out.println("Type something: ");

               for (int i = 0; i < test.length; i++)
               {
                    int ch = System.in.read();
                    if (ch == -1)
                    {
                         test[i] = 0;
                         break;
                    }
                    test[i] = ch;
               }

               System.out.println("You've typed ");
               for (int j = 0; j < test.length; j++)
               {
                    if (test[j] == 0)
                         break;

                    System.out.print((char)test[j]);
               }

          }
          catch (IOException e)
          {
               System.err.println("Failed");
          }
     }
}

0
 
travishabermanAuthor Commented:
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");
        }
    }
}
0
 
travishabermanAuthor Commented:
any chance you could tell me how to handel the exception?  

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


http://www.experts-exchange.com/Programming/Q_21378096.html


-TH
0
 
DhaestCommented:
PS: try adding your java-question in the java-topic...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.