Win a copy of Head First Android this week in the Android 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How do I print out every other letter?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

Can someone please help me to create a program in java that prints out every other letter? For example if I enter "Hello World", then the output should be "Hlo Wrd". I am very new at programming and would very much appreciate your help!
 
Marshal
Posts: 74359
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

We don't hand out complete solutions; please show us what you have so far. Or tell us how you think you would do it, with or without code.

if I enter "Hello World", then the output should be "Hlo Wrd".

Please explain how you would get that; you are not printing alternate letters nor alternate keystrokes for the whole output.
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the most important things you need to learn when programming is that the details matter infinitely more than you can believe.  You ask how to print every other letter.  Your example has a space in it. Does that space count as a letter, or do you always print spaces, or should you not have included it in your example output (or not been in your input)?  Are you supposed to start printing at the first letter, or should you skip the first letter?  What about other non-alphabetic characters, like '&' or '#'...

another extremely important thing to learn is how to break down a big problem into littler problems that are easier to solve - or that get you one step closer to your answer.  Can you write a program that simply prints "Hello World"?  If so. do that.  Then see if you can print out each individual letter.  Then see if you can do the next step.

Does your program need to ask a user for an input, will it read a string from a database, will it be hard-coded?  

These are a few tips to help get you started.  Try something, then post your code here when you get stuck and tell us WHERE you are stuck.
 
Ellen Reddix
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for answering!
Blank spaces are not included, only letters. The first letter is printed out but every other is deleted.

I know how to do a program that asks the user to write a sentence. I also know that I probably have to use charAt and %. But I do not know how to use it. I am also suspecting that I need to use an array or a loop but I don’t know how. Please help.
 
fred rosenberger
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget about Java.  

How would YOU do it, using pencil and paper?  Or even better, how would you tell a young child to do it?  It helps to literally write it out in Notepad, Word, or some text editor. I'd do it something like this:

ask someone to give you a string.
print every other letter
quit

Ok...so..do i know how to do each of those steps?  You state yes for the first, and no for the second. So, start breaking it down.

ask someone to give you a string.
print every other letter
  Find the first character
  decide if it should be printed
  until you are out of letters
      find the next letter
      decide if it should be printed
quit

Ok, can I do each of THOSE?  if not, break them down.  if so, start coding.  Even if you only know how to do some of those, you can code up that part, and then write dummy code...someting like a System.out.println("I need to decide if I should print the letter " + myCurrentLetter);

the important things are:
1)  TRY something
2) Have a good idea what you are trying to do
3) Try SMALL pieces at a time.
 
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First I would break this task down into smaller steps.

1. Write a program that prompts the user for a string.
2. Update program to accept and save string into a variable.
3. Update program to loop through the string displaying each character.
4. Find a way to skip every other character while looping through string.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic