Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

while loop question

Options
  • 04-07-2011 2:17pm
    #1
    Registered Users Posts: 55 ✭✭


    failed a beginners java module, so Im doing some revision for my repeat in august, kinda stuck here if anyone can help please....


    write a method called reverseString that takes a string s, passed to it as a parameter, and reverses the string. You should use a while loop to display one character at a time.

    use the header: public void reverseString(String s)


    here's what I have, yes it's a looks like a dog's dinner, can someone fix it for me thanks
    public void reverseString(String s)
    {
            String answer = "" ;
            int i = 0 ;
            int j = 0 ;
            while(i <= s.length()){
                while(j >= 0){
                    answer = s.substring(i); 
                }i--  ;
                j++ ;
            }
    System.out.println(i) ;
    
        }
    


Comments

  • Registered Users Posts: 255 ✭✭boblong


    Something like this?
    
    public class Main {
    	
    	public static void main(String[] args) {
    		reverseString("Hello JavaBeans!");
    	}
    	
    	public static void reverseString(String s) {
    		int i = s.length()-1;
    		
    		while (i>-1) {
    			System.out.println(s.charAt(i));
    			i--;
    		}
    	}
    
    }
    

    Although the question wording sort of implies that a reversed string should be returned, the return type of 'void' in the prototype suggests otherwise...


  • Registered Users Posts: 55 ✭✭JavaBeans


    hey, thanks for reply, ive slightly modified what you wrote, and now it works,
    but we haven't covered charAT method you've called !! but it works !! ill learn it off thanks..Im sure ill be back with more questions as i redo the course work :D

    http://i55.tinypic.com/11tlsu8.jpg


  • Registered Users Posts: 255 ✭✭boblong


    Cool, glad it worked out.

    Note that the i>-1 terminating condition will also work, as:

    i=5 (ok)
    i=4 (ok)
    i=3 (ok)
    i=2 (ok)
    i=1 (ok)
    i=0 (ok)
    i=-1 (Not OK as i is not > -1)


  • Registered Users Posts: 539 ✭✭✭but43r


    JavaBeans wrote: »
    but we haven't covered charAT method you've called !!
    http://i55.tinypic.com/11tlsu8.jpg

    :confused::confused::confused: You will NEVER cover all methods available in JAVA API in college and I am sure you are not restricted to use particular methods when doing coursework. I would suggest you to have a look at JAVA API - http://download.oracle.com/javase/6/docs/api/


  • Registered Users Posts: 297 ✭✭stesh


    but43r wrote: »
    :confused::confused::confused: You will NEVER cover all methods available in JAVA API in college and I am sure you are not restricted to use particular methods when doing coursework. I would suggest you to have a look at JAVA API - http://download.oracle.com/javase/6/docs/api/

    JAVA API is best API.


  • Advertisement
  • Registered Users Posts: 8,070 ✭✭✭Placebo




  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Placebo wrote: »

    Above code is javascript, not java and does not demonstrate a while loop.


  • Registered Users Posts: 55 ✭✭JavaBeans


    but43r wrote: »
    :confused::confused::confused: You will NEVER cover all methods available in JAVA API in college and I am sure you are not restricted to use particular methods when doing coursework.
    probably not, but the exercises given to us we're specifcally to get us used to while loops, and solve problems with while loops, Im aware of the api cheers !

    like my lecturer would of wanted this as the answer..
    public void reverseString(String s)
    {
    String answer = "" ;
    int i = s.length() ;
    int j = 0 ;
    while(i >= 0){
    while(j < s.length()){
    answer = s.substring(i);
    }i-- ;
    j++ ;
    }
    System.out.println(i) ;
    
    }
    


Advertisement