Solved

user input

Posted on 2003-11-19
9
378 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 82
more than one jdk and one jre 1 41
pagenation logic how it is working in my code 1 30
Is there a simpler dropbox system? 10 22
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

777 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