Solved

user input

Posted on 2003-11-19
9
375 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:lwinkenb
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Expert Comment

by:sre23
Comment Utility
good point lwinkenb :)
0
 

Author Comment

by:dirtydinny
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
you just need to typecast

int integerValue = (int)sAnser;

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now