Results 1 to 11 of 11
  1. #1
    Blaedel is offline Member
    Join Date
    Oct 2009
    Location
    Denmark
    Posts
    10
    Rep Power
    0

    Default What am I doing wrong in this basic problem

    Why won't this short program give the word backwards?



    package stringtestpalindroma;

    public class StringTestPalindroma {


    public static void main(String[] args) {

    String s2, s3, s5 = null, s6 = null;

    s2 = "russerdressur";

    int senl = s2.length();


    for (int i=0; i<=senl; i++)

    {
    senl = senl -1;
    s3 = s2.substring(senl, (senl +1));
    s6 = s5 + s3;

    s2 = s3.substring(0, senl);

    }

    System.out.println(s6);


    }

    }


    It's part of a bigger problem, but this particular part really teases me.

    Thanks a bunch

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    I can't see what you are trying to do with that snippet but didn't you want to use the loop index i inside that loop?

  3. #3
    Blaedel is offline Member
    Join Date
    Oct 2009
    Location
    Denmark
    Posts
    10
    Rep Power
    0

    Default

    hmm... Well basically I wanted the loop to keep going until the word or sentence had no more letters.

    So first identifying where in the word the substring should be taken.

    After that creating a word letter by letter in the new variable s6

    and lastly cutting one letter off the s2.

    Im pretty convinced there's a much smarter way of doing this, but I don't know how. AND I can only use very basic programming...

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

    Default

    some pseudocode for the logic

    Java Code:
    LET string = "abcba"
    LET forwardIndex = 0
    LET backwardIndex = string's max length - 1
    
    LOOP(condition)
      c1 = string.charAt(forwardIndex)
      c2 = string.charAt(backwardIndex)
      check c1 == c2 ?
      update indexes
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Blaedel is offline Member
    Join Date
    Oct 2009
    Location
    Denmark
    Posts
    10
    Rep Power
    0

    Default

    Hmm Thanks a bunch... I'll work in that for a while though I must admit I'm not that familiar whith the methods used.

    When you write update indexes.. What do you mean. Or is it a code it self??? Sorry I've just been programming for app. three-four weeks now.

    But again thanks a lot

  6. #6
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Let me try to explain. The biggest problem when you start programming is that you cannot know everything at the beginning:

    angryboy use what is called pseudo code, which is used to have a comprehensive view before you write the real code. So 'update indexes' is just a term for 'here you go to the next round in the loop'. Let say you have the loop

    Java Code:
    for (int i=0; i<=senl; i++)
    {
        // Here is what you do in the loop
    
    }  // This is the end of the loop and you 'update indexes' that is go for the next i
    Hope this has made a clarification for you.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  7. #7
    Blaedel is offline Member
    Join Date
    Oct 2009
    Location
    Denmark
    Posts
    10
    Rep Power
    0

    Default

    Hey Guys

    Thanks for the explanation Pellebye, that was actually nice to get clarified.

    However it still causes me trouble. I have a feeling that it is something very besic that I misunderstand.

    I tried using that charAt method but that didn't please the IDE so much, so back to the substring manouvre I went :-)

    So this is the code now. And it works better (it can be read by NetBeans) however still not doing what I would like it to. Why doesn't it say that my word is a palindrome

    [code]

    package stringtestpalindroma;

    public class StringTestPalindroma {

    public static void main(String[] args) {

    String s2, s5, s6;

    s2 = "russerdressur";

    int senl = s2.length();
    int forwardIndex = 1;
    int backwardIndex = senl-1;

    for (int i=1; i<=senl; i++)

    {
    s5 = s2.substring(forwardIndex, forwardIndex + 1);
    s6 = s2.substring(backwardIndex, backwardIndex + 1);

    if (s5.equals(s6))
    {
    backwardIndex = backwardIndex - 1;
    forwardIndex = forwardIndex + 1;
    }

    else System.out.println("this is not a palindrome")
    }
    System.out.println("This IS a palindrome");
    }

    [/end code]

    What my result is is:

    run:
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    this is not a palindrome
    This IS a palindrome
    BUILD SUCCESSFUL (total time: 0 seconds)

    Thanks again
    Last edited by Blaedel; 10-11-2009 at 07:27 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello. Could you edit your code and use code tags? I'm finding it very difficult to read what you've posted. Thanks.

    edit: also, you may wish to walk through your code pretending that it's running (but use a smaller word, such as "aba" or "otto"). In doing this, you'll see your errors.
    Last edited by Fubarable; 10-11-2009 at 07:13 PM.

  9. #9
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Its a problem that you use [/end code] where it should be [/code]. See
    Java Forums - BB Code List
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  10. #10
    Blaedel is offline Member
    Join Date
    Oct 2009
    Location
    Denmark
    Posts
    10
    Rep Power
    0

    Default

    oh...

    Yes it was quite simple thanks a lot mate.

    I remember the code tags in the future

  11. #11
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    If you inset some more system.out.print as I have did here

    Java Code:
    public class StringTestPalindroma {
    
    	public static void main(String[] args) {
    	
    		String s2, s5, s6;
    		
    		s2 = "russerdressur";
    		
    		int senl = s2.length();
    		int forwardIndex = 1;
    		int backwardIndex = senl-1;
    	
    		for (int i=1; i<=senl; i++){
    			s5 = s2.substring(forwardIndex, forwardIndex + 1); 
    			s6 = s2.substring(backwardIndex, backwardIndex + 1);
    			[b]System.out.print("i = " + i);
    			System.out.print(" s5 = " + s5);
    			System.out.println(" s6 = " + s6);[/b]
    			
    			if (s5.equals(s6)){
    				backwardIndex = backwardIndex - 1;
    				forwardIndex = forwardIndex + 1;
    			}else{ 
    				System.out.println("this is not a palindrome");
    			}
    		}
    		System.out.println("This IS a palindrome");
    	}
    }
    I think you will get a clue abort what is wrong ... good luck
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

Similar Threads

  1. Basic Help
    By robjames in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:58 AM
  2. Need some really basic help
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 01-24-2009, 06:00 AM
  3. [SOLVED] very basic java problem
    By sales1 in forum New To Java
    Replies: 13
    Last Post: 08-20-2008, 08:33 PM
  4. Wats the problem here ? anythig wrong with IDE
    By Shyam Singh in forum New To Java
    Replies: 6
    Last Post: 07-17-2008, 04:17 PM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 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
  •