Results 1 to 5 of 5
  1. #1
    cdoan17 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Help with output

    Hi,

    So I'm trying to figure out why I keep getting this output:

    a-b c-d-

    when I'm trying to get this output:

    a-b c-d

    It's driving me crazy. My print command is bounded within the loop function. In the end, x should equal zero before it hits the end of the loop. Please help!

    Java Code:
    class shuffle1 {
    	public static void main (String[] args) {
    		int x = 3;
    		while (x > 0) {
    			if (x > 2) {
    				System.out.print ("a");
    			}
    			
    			if (x == 2) {
    				System.out.print ("b c");
    			}
    			
    			if (x == 1) {
    				System.out.print ("d");
    					x = x - 1;
    			}
    			
    			x = x - 1;
    			
    			System.out.print ("-");
    			
    		}
    		
    		System.out.println();
    	}
    }
    Thanks!
    C

  2. #2
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Help with output

    Quote Originally Posted by cdoan17 View Post
    Hi,

    So I'm trying to figure out why I keep getting this output:

    a-b c-d-

    when I'm trying to get this output:

    a-b c-d

    It's driving me crazy. My print command is bounded within the loop function. In the end, x should equal zero before it hits the end of the loop. Please help!

    Java Code:
    class shuffle1 {
    	public static void main (String[] args) {
    		int x = 3;
    		while (x > 0) {
    			if (x > 2) {
    				System.out.print ("a");
    			}
    			
    			if (x == 2) {
    				System.out.print ("b c");
    			}
    			
    			if (x == 1) {
    				System.out.print ("d");
    					x = x - 1;
    			}
    			
    			x = x - 1;
    			
    			System.out.print ("-");
    			
    		}
    		
    		System.out.println();
    	}
    }
    Thanks!
    C


    Hi,

    you will change your code like below.... I think that will be works...

    class shuffle1 {
    public static void main (String[] args) {
    int x = 3;
    while (x > 0) {
    if (x > 2) {
    System.out.print ("a");
    }

    if (x == 2) {
    System.out.print ("b c");
    }

    if (x == 1) {
    System.out.print ("d");
    x = x - 1;
    }

    x = x - 1;
    if(x!=0){

    System.out.print ("-");
    }

    }

    System.out.println();
    }
    }

    thanks,

  3. #3
    cdoan17 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Help with output

    I changed it like you posted. I'm still getting the same output:

    a-b c-d-

    It's strange. That little dash at the back is really annoying.

  4. #4
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Help with output

    Hi,

    This is definitely works Already i was tested..
    So use this....

    public class shuffle1 {
    public static void main (String[] args) {
    int x = 3;
    while (x > 0) {
    if (x > 2) {
    System.out.print ("a");
    }

    if (x == 2) {
    System.out.print ("b c");
    }

    if (x == 1) {
    System.out.print ("d");

    }

    x = x - 1;
    if(x!=0){

    System.out.print ("-");
    }

    }

    System.out.println();
    }
    }
    Thanks,

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help with output

    I find it a vey convoluted way of printing such a simple line; the simplest way to print it is:

    Java Code:
    System.out.println("a-b c-d");
    but if you really, really want to use a loop here: three parts are printed: "a-" "b c" and "-d"; why not use it and do:

    Java Code:
    for (int i=0; i < 3; i++) {
       switch (i) {
          case 0: System.out.print("a-"); break;
          case 1: System.out.print("b c"); break;
          case 2: System.out.println("-d"); break;
       }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2012, 12:39 PM
  2. Can't See the output
    By Gousia in forum New To Java
    Replies: 12
    Last Post: 02-28-2011, 12:01 PM
  3. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  4. cannot see the output...
    By Mihail Kravsun in forum New To Java
    Replies: 3
    Last Post: 04-15-2009, 07:57 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 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
  •