Results 1 to 5 of 5
  1. #1
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Question Some while loop problem need help

    this is my code
    --------------------------------------------------------------------------
    class stringOut
    {
    static int cont = 1,k=0;
    static String spa;
    public static void main(String args[])
    {

    String str = "ABCDEFGFEDCBA";
    System.out.println(str);
    int len = str.length();
    while(cont <= 6){
    String sa = str.substring(0, len/2-k);
    String sb = str.substring(len/2+1+k ,len);
    spa = calc();
    str = sa.concat(spa.concat(sb));
    System.out.println(str);
    k++;
    cont++;
    }

    }
    static String calc()
    {
    if(cont == 1)
    return " ";
    else if(cont == 2)
    return " ";
    else if(cont == 3)
    return " ";
    else if(cont == 4)
    return " ";
    else if(cont == 5)
    return " ";
    else if(cont == 6)
    return " ";
    else return"";
    }
    }
    --------------------------------------------------------------------------
    what is the problem why does it not enter the while loop
    actually it is suppose to print the output as:-
    --------------------------------------------------------------------------
    ABCDEFGFEDCBA
    ABCDEF FEDCBA
    ABCDE EDCBA
    ABCD DCBA
    ABC CBA
    AB BA
    A A
    --------------------------------------------------------------------------
    what is the problem i m not able to understand
    pls help

  2. #2
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    actually in the code and output spacings is inappropriate

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Your prob is that you've changed the original string.

    Java Code:
    public class StringOut {	
    	public static void main(String [] args){
    		// setup var
    		String string = "ABCDEFGFEDCBA";
    		final int length = string.length();
    		int cont = 1;
    		int k=0;
    		
    		// print original string
    		System.out.println(string);
    		
    		while(cont <= 6){
    			String sa = string.substring(0, length/2-k);
    			String sb = string.substring(length/2+k+1 ,length);
    			System.out.println(sa + " " + sb);
    			
    			k++;
    			cont++;
    		}
    
    	}
    }

  5. #5
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

Similar Threads

  1. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 08:08 AM
  2. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 11:24 PM
  3. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 02:41 AM
  4. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 04:51 PM
  5. eternal loop problem
    By sandor in forum New To Java
    Replies: 3
    Last Post: 04-29-2007, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •