Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Complete newbie

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, everyone.  I am a complete beginner and been wanting to learn a programming skill since I was a kid back in the 90's.  I guess it's never to late.  Anyways, I stumbled on this website (hyperkill.org) and I need some help they kind of go to the test of being really easy to complete hard and I don't have the skill.  But, still want to try and learn it so I am not giving up.  Here is the problem they are asking me.  
Write a program that reads four numbers and decrements each of them.
The program must output the results in the same order separated by spaces.
Sample Input 1:
10 11 -2 -3
Sample Output 1:
9 10 -3 -4

I think I understand how to decrement, I understand how to get the numbers by using int a,b,c,d and a = 10, b = 11, c = -2, d = -3 (unless I am using variable wrong) then somehow use --a, and so forth to print out, but they want me to also include the spaces but they never teach you that.  I don't know if I am completely understanding this at all.  I don't want any answers just some help would be nice.  
 
Marshal
Posts: 75642
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can add text to anything to be printed with the + operator. If you write something + " ", that will attach the space to it in text form. Don't copy'n'paste that code fragment.
 
Erik Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, I tried that as well and apparently the test didn't like that as an answer.  Thank you though
 
Sheriff
Posts: 16921
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us your program. It's much easier to give specific advice that way. Otherwise, we'll just be guessing at what you're actually doing wrong.
 
Erik Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It's sloppy but it does what it supposed to do but the site doesn't accept this as an answer.
 
Junilu Lacar
Sheriff
Posts: 16921
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not surprised. They gave you sample input so you have a better idea of what your program needs to do. The idea is take any 4 numbers and decrement each one. The test probably gave you totally different numbers so naturally your program failed the test.
 
Erik Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe i need to use the scanner object? at least it'll be input?  So besides the sample input is the code actually right? I know it compiles with no issues but is there a better way to do it?
 
Junilu Lacar
Sheriff
Posts: 16921
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you mean hyperskill.org? You wrote hyperkill.org which doesn't exist.
 
Junilu Lacar
Sheriff
Posts: 16921
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a Scanner and .nextInt() -- you could also check to see if args[] has 4 elements in it and iterate over it if it does. You never know how your program will be called. Most likely that Scanner will do the job though.
 
Erik Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I updated the code to use scanner and it worked.  Ugh, I hope I can start to understand this stuff it gets really hard.  Thank you for helping.
 
Junilu Lacar
Sheriff
Posts: 16921
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us the updated code so we might suggest ways to make it better. One of the best ways to learn is to see how other people solve the same problem and understand their approach.
 
Erik Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Junilu Lacar
Sheriff
Posts: 16921
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're guaranteed to get four numbers, then this is another way:

That will work but it's hard to read. You can refactor to make it easier to read:

Alternatively, you could use a for-loop:

If there's no guarantee of how many numbers will be in the input, then you'll want to go more conservative and use the for-loop a while-loop.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic