?
Solved

user input

Posted on 2003-11-19
9
Medium Priority
?
385 Views
Last Modified: 2010-03-31
hey,

i was just wondering if somebody could give me a code example and syntax for gtting user input in a java application through dos?

i saw a way of doing it on a site but it looked a bit overdone. i just want it nice and simple. if possible as simple as in vb! also fromwhat i could make out the input is has to be equal to a string format. So how so i convert this string into an int and long etc for calculations? is there a parseint or something. another example would be handy.

as ever all input it welcom and appreciated

thanks

dinny
0
Comment
Question by:dirtydinny
  • 6
  • 2
9 Comments
 
LVL 1

Expert Comment

by:sre23
ID: 9783411
public class Test
{
     public static void main(String args[])
     {
          System.out.print("What is your age?");
          BufferedReader r = new BufferedReader(new InputStreamReader(System.in));  
          int age = Integer.parseInt(r.readLine());
     }
 


}
0
 
LVL 1

Expert Comment

by:sre23
ID: 9783416
this prompts the user for their age and waits for them to enter it and press enter.  When the user hits enter the value they have typed gets assigned to "age"  You will also need to make sure they have typed a number, otherwise you will get an error
0
 
LVL 1

Expert Comment

by:sre23
ID: 9783440
Actually this code will compile

import java.io.*;

public class Test
{
    public static void main(String args[])
    {
       try
       {
         System.out.print("What is your age?");
         BufferedReader r = new BufferedReader(new InputStreamReader(System.in));  
         int age = Integer.parseInt(r.readLine());
         System.out.println("Your Age: " + age);
       }
       catch(IOException e)
       {
           System.out.println("You must enter a number!");
       }
    }



}
0
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.

 
LVL 5

Expert Comment

by:lwinkenb
ID: 9783598
A small change to sre23's code:

import java.io.*;

public class Input
{
    public static void main(String args[])
    {
       try
       {
         System.out.print("What is your age?");
         BufferedReader r = new BufferedReader(new InputStreamReader(System.in));  
         int age = Integer.parseInt(r.readLine());
         System.out.println("Your Age: " + age);
       }
       catch(NumberFormatException e)
       {
           System.out.println("You must enter a number!");
       }
       catch(IOException e) {
             System.out.println(e);
       }
    }
}
0
 
LVL 1

Expert Comment

by:sre23
ID: 9784213
good point lwinkenb :)
0
 

Author Comment

by:dirtydinny
ID: 9786251
hey lwinkenb


can u comment ur code, just so i know whats going on? i've a fair idea in some parts what it happening but just to get the full picture

thnaks

dinny
0
 
LVL 1

Accepted Solution

by:
sre23 earned 120 total points
ID: 9788925
Here I'll do it for ya:

A small change to sre23's code:

import java.io.*;

public class Input
{
    public static void main(String args[])
    {
       try
       {
         System.out.print("What is your age?");  //prints this to the screen and waits for users response
         BufferedReader r = new BufferedReader(new InputStreamReader(System.in));   //creates a BufferedReader object to  
                                                                                                                             //read in from the keyboard (System.in)
         int age = Integer.parseInt(r.readLine());  //when the user hits enter this assigns what they typed to the variable "age"
                                                                     //Integer.parseInt tries to convert what they typed to an Integer
         System.out.println("Your Age: " + age);   //this prints out what number the person entered if it was an integer
       }
       catch(NumberFormatException e)
       {
           System.out.println("You must enter a number!");  //this error is caught if they did not enter an integer
       }
       catch(IOException e) {
            System.out.println(e);   //this error is caught if there was a problem reading in from the keyboard
       }
    }
}
0
 

Author Comment

by:dirtydinny
ID: 9795916
just one final ammendment!

if i have a double lets cll it sAnswer how do i convert it back to a int before i output it with a preintln statement?

thankd

dinny
0
 
LVL 1

Expert Comment

by:sre23
ID: 9798164
you just need to typecast

int integerValue = (int)sAnser;

putting (int) in front of the value converts it to an integer.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question