5 Replies - 381 Views - Last Post: 02 February 2010 - 06:17 PM Rate Topic: -----

#1 Austinjs0102  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-February 10

Word to Letters seperated by spaces

Posted 02 February 2010 - 02:35 PM

I am trying to have the user input a word then the out put will be each letter with a space between.

Example

Hello
output is H e l l o

my code so far is lacking the output.. Will i use a for loop for this and do i need to make a boolean?

   public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    
    System.out.print("Please enter a word> ");
    String Word = scan.next();


                  
                for(int i = 0; i=Word.charAt(i); i++)
                   System.out.println(" " +i);



This post has been edited by Austinjs0102: 02 February 2010 - 02:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Word to Letters seperated by spaces

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Word to Letters seperated by spaces

Posted 02 February 2010 - 02:39 PM

You're on the right track, but not quite.

A for loop works like this:
for(variables; condition; increment){ }

So, since you want to loop through a String, you'd want to use this:
for(int i = 0; i < Word.length(); i++)
//print the character at index i, and a space



edit: or, KYA could just give you the answer. That works too :P

This post has been edited by erik.price: 02 February 2010 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: Word to Letters seperated by spaces

Posted 02 February 2010 - 02:40 PM

for(int i = 0; i < Word.length(); i++) //for loops need a conditional 
     System.out.println(" " + Word.charAt(i)); 


This post has been edited by KYA: 02 February 2010 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Word to Letters seperated by spaces

Posted 02 February 2010 - 02:43 PM

Your for loop is all wrong. Edit: just noticed this has been addressed. However, you may want to use [ilSystem.out.print()[/il], not System.out.println() That makes it not skip to the next line.

for(int i = 0; i < Word.length(); i++) 
     System.out.print(" " + Word.charAt(i));


This post has been edited by Dogstopper: 02 February 2010 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 Austinjs0102  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-February 10

Re: Word to Letters seperated by spaces

Posted 02 February 2010 - 02:47 PM

Well thanks, i actually read yours first Erik and then figured it out on my own but its nice to know i got it right and people were nice to share their input. Thanks a bunch!!

I appreciate all the help here too you guys are giving, now i see why my loop was incorrect, i was taking the number but i needed to specify that i wanted the characters at that specific spot.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,294
  • Joined: 27-December 08

Re: Word to Letters seperated by spaces

Posted 02 February 2010 - 06:17 PM

Also, just FYI, when you assign a char to an int, it will be auto-converted to a number based on its position in the ASCII table. So for example, 'A' as an int is 65.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1