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
Solved

user input

Posted on 2003-11-19
9
379 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

829 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