Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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
0
travishaberman
Asked:
travishaberman
  • 3
  • 3
  • 3
2 Solutions
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now