Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

very basic java

 
william roberts
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...:-)
-d
[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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.in();
is how you get keyboard input
if you use args the user cannot be prompted
so
- 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
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Mahesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic