-
Trouble with concatenation output
This is a program I am workng on for class. We are to take input from the user and have it output it into a concatenated string. The program takes the input and outputs it when the loop is terminated but the problem is that the variable sentence that holds the input. Holds them without spaces between each input. Is there a way to do this. I think I can use an arraylist but we have not gone over these yet. We've had if, if/else, while, do/while and for, so I was trying to do it with just what we had been studing but I just can't get it to work. Thank you in advance for any help
Code:
import java.util.Scanner;
public class concatenationWorddw
{
public static void main ( String [] args )
{
// declare variables
String sentence = "";
String word = " ";
String end;
Scanner scan = new Scanner(System.in);
//menu for user
System.out.println( " Enter the words ");
System.out.println( " To make your sentence ");
System.out.println( " Enter end to stop >\n");
do
{
sentence += word ; // adds word to sentence
//prompts user for a word
System.out.print("Enter a word > ");
word = scan.next();
}while ( ! word.equals( "end" ) ); //condition to exit loop
//outputs the sentence
System.out.println("Your new sentence is" + sentence );
}
}
-
you can add a space to the string .
i changed some other parts of code.
StringBuffer is good if you want to change your string later.
nextLine() is more proper, so you can enter words ( seperated spaces ) without more than 1 "enter a word" prompt
Code:
import java.util.Scanner;
public class name
{
public static void main ( String [] args )
{
// declare variables
StringBuffer sentence = new StringBuffer();// changed !!!
String word = "";// changed !!!
String end;
Scanner scan = new Scanner(System.in);
//menu for user
System.out.println( " Enter the words ");
System.out.println( " To make your sentence ");
System.out.println( " Enter end to stop >\n");
do
{
sentence.append(word + " " ) ; // changed !!!// adds word to sentence
//prompts user for a word
System.out.print("Enter a word > ");
word = scan.nextLine();// changed !!!
}while ( ! word.equals( "end" ) ); //condition to exit loop
//outputs the sentence
System.out.println("Your new sentence is" + sentence );
}
}
-
mr1yh1, thank you. I knew there had to be a way to do this with out a ArrayList, and I thought that the StringBuffer would work but I was not sure how or where to use since we have not had it in class yet. So thank you for showing me how to use this in a program.So with the append in this it causes the string to be able changed with each user, is that correct?
-
Notice that the StringBuffer is not a String. Think of the StringBuffer as a collection of characters. You can tell it to append or delete characters. At some point, you can tell the StringBuffer to create a String from the collection of characters by writing:
Code:
String createdStr = sentence.toString();
This was not required in the program ( as seen in the line below) because concatenation of the string ("Your new sentence is") with an object (the StringBuffer) will automatically call the toString() method of that object.
Code:
//outputs the sentence
System.out.println("Your new sentence is" + sentence );
So the above line is equivalent to:
Code:
//outputs the sentence
System.out.println("Your new sentence is" + sentence.toString() );
For more help, www.NeedProgrammingHelp.com
-
Cool, thanks MPH this is good to know. I have learned so much in the last week just by reading other peoples problems and how they got them to work. I love Java but there are so many ways that you can do things that it just takes time to get it. mr1yh1 and MPH thank you so much for taking the time to really explain how things work I appreciate your time. This is a great forum no one makes you feel like you are trying to get someone to do your work like some other forums (not mentioning any names) So when I become more knowledgeable I hope that I can help someone like you have for me
Similar Threads
-
By davidsc in forum VB Classic
Replies: 4
Last Post: 11-03-2005, 08:21 AM
-
Replies: 3
Last Post: 08-28-2005, 10:01 PM
-
Replies: 0
Last Post: 02-18-2003, 04:23 PM
-
By Xin Li in forum Database
Replies: 0
Last Post: 01-04-2001, 03:49 PM
-
By Deborah D Brown in forum Database
Replies: 0
Last Post: 09-21-2000, 01:54 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks