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 6 of 6

Thread: Simple java beginners question (for school)

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple java beginners question (for school)

    Dear Java Programmers,

    Excuse me if my English is not very good. I'm just learning java on school and now I have an assignments for school. I really cant figure them out, could someone help me please?

    First question:
    I need to write a function/method that can rotatate characters in a string.

    Example:
    'JAVA' will return 'AVAJ'
    'JAVA' with n = '2' will return 'VAJA'
    'JAVA' with n = '3' will return 'AVAJ'

    Please don't be to hard on me, I just started with learning Java.

    Thanks alot!

    Jeesie


  2. #2
    Forum Admin
    Join Date
    Oct 2012
    Posts
    92
    My Mood
    Amused
    Thanks
    9
    Thanked 18 Times in 13 Posts

    Default Re: Simple java beginners question (for school)

    Welcome Jessie.

    Have you tried coding the solution yet? Where did you fail? What's not working?

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple java beginners question (for school)

    Quote Originally Posted by Admin View Post
    Welcome Jessie.

    Have you tried coding the solution yet? Where did you fail? What's not working?
    Dear Admin,

    I have been brainstorming and the best way (I think) is to 'cut' the string into loose characters. And then I need to move the characters in the String to the left/right. But i don't know how to do that.
    for (int i = 0 ; i < string.length() ; i++) 
    {*
    char ch = s.charAt(i);*
    //rotatation here
    }

    Is my thinking right?

    Thanks in advance

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple java beginners question (for school)

    I've done it! Here is the solution (using substring):
    public class Main
    {
        public static void main(String[] args)
        {
    	//INVOER
    	String s = "HELLO";
    	int shift = 1;
     
    	//UITKOMST
    	String solution = s.substring(shift)+s.substring(0,shift);
    	System.out.println(solution);
        }
    }

    But I can't insert '-1' in int shift, that will give me an error. How can I also shift the letters to the right? Now they are going to the left. This script above will output: ELLOH

    Thanks!

  5. #5
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Simple java beginners question (for school)

    So your desired output would be OLLEH?

    If don't know if this helps, but you can also take letters separate out of it.
    That might be another way of solving it. (There are multiple ways to Rome right? :p)
    Although your way also can work.

    Maybe the functions below might help with your question?
    But you need to implement them yourself into your code if you like using them.

    int len = s.length();
    System.out.println(len);
     
    char letter = s.charAt(len -1);
    System.out.println(letter);

    You can use a while loop to grab every letter in the complete string separate.

    Are you Dutch btw?

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple java beginners question (for school)

    You can try for this
    public String rotateStringCW(String str, int no){
    int N=str.length();
    String str1= str.substring(N-no,N)+str.substring(0,N-no);
    return str1;
    }

Similar Threads

  1. Beginners Java Help !
    By Cluelius in forum Loops & Control Statements
    Replies: 4
    Last Post: October 22nd, 2012, 01:24 PM
  2. School Java Project Help (XML and links)
    By MC2170 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 3rd, 2012, 08:44 AM
  3. High School Java Programming Class
    By SDennis52 in forum Collections and Generics
    Replies: 1
    Last Post: May 7th, 2012, 12:38 AM
  4. School java project, completely stuck
    By John1818 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 18th, 2010, 04:10 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM