• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

very basic java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
His brain is the size of a cherry pit! About the size of this ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic