Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 416,725 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,907 people online right now.Registration is fast and FREE... Join Now!



Need help putting spaces in a java string Please help. Rate Topic: -----

#1 isoman2kx  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 08


Dream Kudos: 0

Share |

Need help putting spaces in a java string

Post icon  Posted 29 October 2008 - 05:51 PM

/* A test
*/

import java.util.Scanner;

public class WordInput
{
	public static void main( String [ ] args)
	{
	
	Scanner scan = new Scanner ( System.in );
	
	System.out.print ( "Enter a word >" );
	String word = scan.nextLine( );
	char character1 = word.charAt (1);
	char character2 = word.charAt (2);
	char character3 = word.charAt (3);
	char character4 = word.charAt (4);
	char character5 = word.charAt (5);
	char character6 = word.charAt (6);
	char character7 = word.charAt (7);
	char character8 = word.charAt (8);
	
System.out.println ( "Your word is (with spaces) " + word + " and " +character1 + " " + character2 + " " + character3);

}


}




The problem asks for the user to end a word (as a string) and then for your program to take that word and display it and then display the word with spaces between every character


like say you entered " longword" .... I need the program to format the string and say "l o n g w o r d "

I'm so frustrated. I've looked all over the damn internet for 2 hours for this problem :(

-Travis

oh and ignore the charat code above, i was just bs'ing to make myself feel better.
Was This Post Helpful? 0
  • +
  • -


#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • Icon

Reputation: 59
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08


Dream Kudos: 25

Re: Need help putting spaces in a java string

Posted 29 October 2008 - 05:53 PM

View Postisoman2kx, on 29 Oct, 2008 - 06:50 PM, said:

/* A test
*/

import java.util.Scanner;

public class WordInput
{
	public static void main( String [ ] args)
	{
	
	Scanner scan = new Scanner ( System.in );
	
	System.out.print ( "Enter a word >" );
	String word = scan.nextLine( );
	char character1 = word.charAt (1);
	char character2 = word.charAt (2);
	char character3 = word.charAt (3);
	char character4 = word.charAt (4);
	char character5 = word.charAt (5);
	char character6 = word.charAt (6);
	char character7 = word.charAt (7);
	char character8 = word.charAt (8);
	
System.out.println ( "Your word is (with spaces) " + word + " and " +character1 + " " + character2 + " " + character3);

}


}




The problem asks for the user to end a word (as a string) and then for your program to take that word and display it and then display the word with spaces between every character


like say you entered " longword" .... I need the program to format the string and say "l o n g w o r d "

I'm so frustrated. I've looked all over the damn internet for 2 hours for this problem :(

-Travis




You're close. If you want to do it like you are then try...

System.out.print(Character1 + " " + Character2 + " " + Character3 + " " + Character4 + " " + Character5 + " " and so on for how many letters you want)




This will only work for words that are 8 letters or less. if you want it for words that are more I suggest using a for loop to do that. cheers
Was This Post Helpful? 0
  • +
  • -

#3 isoman2kx  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 08


Dream Kudos: 0

Re: Need help putting spaces in a java string

Posted 29 October 2008 - 06:00 PM

You're close. If you want to do it like you are then try...

System.out.print(Character1 + " " + Character2 + " " + Character3 + " " + Character4 + " " + Character5 + " " and so on for how many letters you want)




This will only work for words that are 8 letters or less. if you want it for words that are more I suggest using a for loop to do that. cheers
[/quote]



could you please please please point me in the right direction ?

I need it for any length of a string actually. I was just putting in the charAt's to see if it was possible at all.

what I actually need is for whatever length of the word that the user enters and that the scanner class scans for my string input, i need to format that length of a word to include spaces between each of it's characters...
Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • Icon

Reputation: 59
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08


Dream Kudos: 25

Re: Need help putting spaces in a java string

Posted 29 October 2008 - 06:12 PM

do you happen to know how to use arrays?
Was This Post Helpful? 0
  • +
  • -

#5 isoman2kx  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 08


Dream Kudos: 0

Re: Need help putting spaces in a java string

Posted 29 October 2008 - 06:47 PM

we haven't used arrays yet in our class. it's a relatively beginner java level class.

we have used loops though, if that helps?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 2130
  • View blog
  • Posts: 13,803
  • Joined: 06-March 08


Dream Kudos: 550

Re: Need help putting spaces in a java string

Posted 29 October 2008 - 06:49 PM


import java.util.Scanner;

public class WordInput
{
	public static void main( String [ ] args)
	{
	
		Scanner scan = new Scanner ( System.in );
		System.out.print ( "Enter a word >" );
		String word = scan.nextLine( );
		char[] digit = word.toCharArray();	// convert String into an array of char
		for(int i = 0; i < digit.length; i++)
		   System.out.print(" " + digit[i]);
		System.out.println();
	  }
}


Was This Post Helpful? 1
  • +
  • -

#7 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • Icon

Reputation: 59
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08


Dream Kudos: 25

Re: Need help putting spaces in a java string

Posted 29 October 2008 - 06:52 PM

View Postisoman2kx, on 29 Oct, 2008 - 07:47 PM, said:

we haven't used arrays yet in our class. it's a relatively beginner java level class.

we have used loops though, if that helps?



Ya loops are good for certain things however you can't do everything with them. Because you don't know how many variables you are going to need(due to the fact you don't know how long the world will be), then you will not be able to make enough variables to hold all the characters in a potentially long string.

Think of it like this... if the user enters....

"dfkfjdaslkfjdldfkljkadlshfkjsadhflkhadsjfklhaskjdfhlksdhflkahsdjfklhdajslfhjdaslhfjdasklhfjklasdhfjklhsdfjklhdskajf"

as his string then you will need to manually create a variable for each letter. You can do this if you want however it will be extremely time consuming and the next time it's run if the user enters a string that is larger then you will need even more. So without arrays there in my knowledge is no way to do it like the way you are requiring.
Was This Post Helpful? 1
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users