?
Solved

user input

Posted on 2003-11-19
9
Medium Priority
?
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 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