This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How do I input a number and print it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I input a number and print it?" Watch "How do I input a number and print it?" New topic
Author

How do I input a number and print it?

John Chong
Greenhorn

Joined: Jan 18, 2001
Posts: 16
Pls help me is this correct?
public class TestIO(){
int a;
{
TestIO(){
}
main(){
print("input a number");
catch(a);
a=new TestIO();
a=a/2;
a.out(a);
}
}
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
does it compile ?


Val SCJP <BR>going for SCJD
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219

I think that's what you were trying to do, anyway. Java syntax is much different than c++ syntax...I suggest going through the tutorial at Sun's site (http://java.sun.com/docs/books/tutorial/index.html).
~Ryan

[This message has been edited by ryan burgdorfer (edited January 31, 2001).]


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Siobhan Murphy
Ranch Hand

Joined: Oct 19, 2000
Posts: 72
This is what I usually do. I hope it helps.

//Put the following line at the top of your program
import java.io.*;

//Prompt user for number
System.out.print("Input a number: ");
//Read in the number. It will be a string, so convert it to int
int size;
BufferedReader keyboardNumber = new BufferedReader(new InputStreamReader (System.in));
size = Integer.parseInt( keyboardNumber.readLine() );

Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
There are many ways to capture the user input from the command line.
1. use the package corejava ( import corejava.Console
I have done assignment #2 EvenOrOdd , here is the sample code, hope it will help.
import corejava.Console;
public class EvenOrOdd
{
public static void main(String[] args)
{
int intD = 2;
int intResult;
boolean blnEorO;
String strBO;
strNo = Console.readInt
("Please enter a number");
int intNo = Integer.parseInt(strNo);
intResult = intNo % strNo;
System.out.println("The number you have entered is: " + intResult );
}
}
2. You can also use String args.However, when you execute the class you have to enter the parameter follow the class name too.
The following code is another way to do assignment #2.
public class EvenOrOdd
{
public static void main(String[] args)
{
int intD = 2;
int intResult;
int intNo;
int intZero = 0;
if ( args.length == 0 )
{
System.out.print( "You have entered no input please re-run the program." );
}
else
{
intNo = Integer.parseInt(args[0]);
intResult = intNo % intD;
if (intResult == intZero)
{
System.out.println("The number you have entered is an Even Number");
} else
{
System.out.println("The number you have entered is an Odd Number");
}
}
}
}
3. Another way is to use the System.in.read(); this method is only can accept on character at a time.
public class classname
{
public static void main(String[] args) throws Exception
{
char userInput;
System.out.println("Please enter a character ");
userInput = (char)System.in.read();
System.out.println("You entered " + userInput);
}
*This is my way to do it and I don't know the best way. Anyway, hope this will help.

Mindy Wu
}
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Mindy,
I have many questions about your first example:

import corejava.Console;
public class EvenOrOdd
{
public static void main(String[] args)
{
int intD = 2;
int intResult;
boolean blnEorO;
String strBO;
strNo = Console.readInt
("Please enter a number");
int intNo = Integer.parseInt(strNo);
intResult = intNo % strNo;
System.out.println("The number you have entered is: " + intResult );
}
}

1. What is this code intended to output? (The number entered by the user, "even" or "odd", etc.)
2. What is the purpose of the variables intD, blnEorO, strBO? (they are declared and then never used)
3. I am just generally confused on what the code is supposed to do, or if it is just a snippet from your version of the EvenOrOdd assignment, or what...(the latter would explain alot)
Thanks,
~Ryan
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Sorry, I did not intend to confuse you. I missed something in the first method. You need to add a if statement in it. I don't think method one is a good way to do that. Try method #2 and 3.
Thanks!
Mindy
 
 
subject: How do I input a number and print it?
 
Similar Threads
Exception question big issue
Error while using packages
a program question
a program question
Questions related to loop