Trouble with concatenation output


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Trouble with concatenation output

  1. #1
    Join Date
    Nov 2005
    Location
    ohio
    Posts
    5

    Question 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   );
    
    
    
        }
    }

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    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   );
        }
    }

  3. #3
    Join Date
    Nov 2005
    Location
    ohio
    Posts
    5
    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?

  4. #4
    Join Date
    Aug 2005
    Posts
    17
    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

  5. #5
    Join Date
    Nov 2005
    Location
    ohio
    Posts
    5
    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

  1. Capture DOS output after CreateProcess
    By davidsc in forum VB Classic
    Replies: 4
    Last Post: 11-03-2005, 08:21 AM
  2. Java Encryptor - strange output
    By MrPica in forum Java
    Replies: 3
    Last Post: 08-28-2005, 10:01 PM
  3. Having trouble getting started
    By Debi in forum Java
    Replies: 0
    Last Post: 02-18-2003, 04:23 PM
  4. Replies: 0
    Last Post: 01-04-2001, 03:49 PM
  5. output in query analyzer in sql 7
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center