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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

while loop question

  • 04-07-2011 1:17pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 8,070 ✭✭✭Placebo




  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    Placebo wrote: »

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


  • Registered Users, Registered Users 2 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