Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with string output

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Help with string output

    I have to write a program that outputs all possible permutations for three input strings. My code works fine except for one problem: if I put a space in between words, it takes the second word as a second input string. How could I use the following code but allow for blank spaces?

    // Chapter 2 programming exercise 17)
    // Program to give all six permutations for 3 inout strings
     
    import java.util.*;
     
    public class Ch2_PrExercise17
    {
    	public static void main(String[] args)
    	{
    		Scanner console = new Scanner(System.in);
     
    		//variable declaration
    		String a, b, c, aa, bb, cc, dd, ee, ff;
     
    		//executable expressions
    		System.out.println("This program will allow you to input three phrases and it will give you all the permutations.");
    		System.out.println("Please press Enter after each entry.");
    		System.out.println();
     
    		System.out.println("Enter the first phrase: ");
     
    		a = console.next();
    		System.out.println();
     
    		System.out.println("Enter the second phrase: ");
     
    		b = console.next();
    		System.out.println();
     
    		System.out.println("Enter the final phrase: ");
     
    		c = console.next();
    		System.out.println();
     
    		//calculations
    		aa = a + " " + b + " " + c;
    		bb = a + " " + c + " " + b;
    		cc = b + " " + a + " " + c;
    		dd = b + " " + c + " " + a;
    		ee = c + " " + a + " " + b;
    		ff = c + " " + b + " " + a;
     
    		//output requirements
    		System.out.println(aa);
    		System.out.println(bb);
    		System.out.println(cc);
    		System.out.println(dd);
    		System.out.println(ee);
    		System.out.println(ff);		
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with string output

    I put a space in between words, it takes the second word as a second input string.
    Are you talking about what the Scanner class reads in from the user?
    The Scanner class has several methods to read in different types of data.
    Look at some of its next... methods to see if any of them will read in what you want to be read.

    To see how they all work, write a small test program and use some of them with different combinations of user input to see what they do. Read in a value and print it.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Help with string output

    This noob thanks you for the hint. I figured it out.........nextLine

Similar Threads

  1. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  2. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM
  5. Output String Handling in Java
    By merry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2010, 09:59 PM