wood burning stoves
The moose likes Beginning Java and the fly likes very basic java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "very basic java" Watch "very basic java" New topic

very basic java

william roberts

Joined: Sep 25, 2001
Posts: 3
I've just started a java correspondence course, and my tutor is away for a week, so please can anyone give me guidance on how to write the program shown below.
I'm trying to learn, so guidance only please, not a line by line solution.
"write a program which prompts the user to enter a sequence(I take this to be a very simple sequence, say 1-10) of 10 integers which are summed and the total displayed. Negatives are ignored i.e not summed and no more than ten numbers(including any negatives may be entered."
I speak java with all the fluency of a two year old, so single syllable replies.
dw milsom

Joined: Sep 06, 2001
Posts: 1
Hi William,
i am fairly new to the language myself, so hopefully my answer won't sound like gobbledygook.
the command line input is an array of strings [ie. the args[] in public static void main(Sting[] args) ].
you'd need a loop to go through the array, determine if each number [ie. args[0], args[1], etc] is a non-negative, then add it to the sum. Then you just print it out!
I hope this makes sense...:-)
[This message has been edited by dw milsom (edited September 25, 2001).]
[This message has been edited by dw milsom (edited September 25, 2001).]
John Kilbourne
Ranch Hand

Joined: Aug 22, 2001
Posts: 30
Start with what you do know:
1.Prompt the user: (possibly involves "println()"
2.Receive input: from comamnd line, this will be obtained from the args[] array of strings -
"public static void main(String [])"
args[3] would return the fourth string entered by the user at the command line, becoause indexing starts at args[0].
You need to convert the String to an int: (possibly involves a static method from the Integer class)
and so on...
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
is how you get keyboard input
if you use args the user cannot be prompted
- start the program print to the console
please enter a number
get that number repeat for 10 numbers
add up the numbers print the total

Johannes de Jong

Joined: Jan 27, 2001
Posts: 5089
William you might want to have a look at the Cattle Drive. If you select the individual assigments you see there are quite a basic tutorials/examples that teaches you the basics to solve the assignments.
Off course you can always dfecide to take the Cattle drive but pse read About the Cattle Drive

Good luck learning Java
Mohan Panigrahi
Ranch Hand

Joined: Sep 28, 2001
Posts: 142
Hi William,
You might find the following code useful for getting user's response.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String strStringEnteredByUser = br.readLine();
Your need to put input java.io.*; in your import statements at the top.
I agree. Here's the link: http://aspose.com/file-tools
subject: very basic java
It's not a secret anymore!