Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 24

Thread: Java Help (programming)

  1. #11
    well to get rid of whitespaces and other ascii characters you can use the String class function "trim"

    Or:

    Here is the code for removeJunk method, got it from somewhere else.

    protected static String removeJunk(String string) {
    int i, len = string.length();
    StringBuffer dest = new StringBuffer(len);
    char c;

    for (i = (len - 1); i >= 0; i--) {
    c = string.charAt(i);
    if (Character.isLetterOrDigit&copy {
    dest.append©;
    }
    }

    return dest.toString();
    }

    ----
    In case you get impatient here is the full code of your program
    Pallindrome
    ___________________
    If you decide not to look at that code and still trying your code and get stuck let us know. We will sure help you out. Is toLowerCase() working? Also, as String is a class and when you define its object you have to use
    String myString = new String("thisString");
    However, java lets you define like String myString = "thisString" without errors. So its initializing an object before creating it.

    Let us know how it goes.
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

  2. Software & Hardware   -   #12
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Originally posted by I.am@2 February 2004 - 06:17
    well to get rid of whitespaces and other ascii characters you can use the String class function "trim"

    Or:

    Here is the code for removeJunk method, got it from somewhere else.

    protected static String removeJunk(String string) {
    int i, len = string.length();
    StringBuffer dest = new StringBuffer(len);
    char c;

    for (i = (len - 1); i >= 0; i--) {
    c = string.charAt(i);
    if (Character.isLetterOrDigit©) {
    dest.append©;
    }
    }

    return dest.toString();
    }

    ----
    In case you get impatient here is the full code of your program
    Pallindrome
    ___________________
    If you decide not to look at that code and still trying your code and get stuck let us know. We will sure help you out. Is toLowerCase() working? Also, as String is a class and when you define its object you have to use
    String myString = new String("thisString");
    However, java lets you define like String myString = "thisString" without errors. So its initializing an object before creating it.

    Let us know how it goes.
    One of the conditions is that we can&#39;t use any of the methods from stringBuffer (if we could it&#39;d be a whole lot easier). I think the "trim" function only gets rid of spaces at the very start and beginning of a string
    The toUpperCase is working out fine, the program recognises "Mum" as a palindrome now, so that&#39;s one of the bits I was working on earlier sorted
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  3. Software & Hardware   -   #13
    Use the removeJunk method I have up there and keep it as protected. Apparantly, from your program looking at that error the method is not recognized. You sure you dont have something like RemoveJunk instead of removeJunk.

    Try renaming, if doesnt work then use the method up there and let us know if it works.
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

  4. Software & Hardware   -   #14
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Originally posted by I.am@2 February 2004 - 18:50
    Use the removeJunk method I have up there and keep it as protected. Apparantly, from your program looking at that error the method is not recognized. You sure you dont have something like RemoveJunk instead of removeJunk.

    Try renaming, if doesnt work then use the method up there and let us know if it works.
    In the end I never had to use the removeJunk method, I gave up on it. I also changed my code extensively, I&#39;ll post the final code in a short while
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  5. Software & Hardware   -   #15
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Final code (got 3/3 for it), no removeJunk method, but the program does what&#39;s asked of it, so that&#39;s really all that matters

    import javabook.*;

    public class Palin
    {
    public static void main (String[] args)
    {
    MainWindow mainWindow = new MainWindow("Palindromes");
    InputBox inputBox = new InputBox(mainWindow);
    OutputBox outputBox = new OutputBox(mainWindow);
    outputBox.setVisible(true);

    String s1="";
    String s2="";
    char tempval=0;
    char tempval2=0;
    int n=0;
    boolean userHalt=false;

    while(userHalt==false)
    {
    s1 = inputBox.getString("Please type in a word: " );
    s2 = s1.toUpperCase();

    if(s1.equals("stop&#33;"))
    {
    userHalt=true;
    break;
    }

    n=s2.length();

    for(int i=0; i<n/2; i++)
    {
    n=s2.length();
    tempval=s2.charAt(0+i);
    tempval2=s2.charAt(n-1-i);
    }

    if(tempval==tempval2)
    outputBox.printLine(s1 + " is a palindrome");
    else
    outputBox.printLine(s1 + " is not a palindrome");
    }
    }
    }
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  6. Software & Hardware   -   #16
    Congrats&#33;

    But,
    Now try entering "Pallen" for the word to check whether it is a palindrome. Try entering "Niihiil" to see it again and you will know what I mean.
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

  7. Software & Hardware   -   #17
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Originally posted by I.am@3 February 2004 - 20:32
    Congrats&#33;

    But,
    Now try entering "Pallen" for the word to check whether it is a palindrome. Try entering "Niihiil" to see it again and you will know what I mean.




    What makes those words "special"? Any ideas for a solution?
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  8. Software & Hardware   -   #18
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Just noticed why they&#39;re "special".
    Give me a while to try and fix it, please
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  9. Software & Hardware   -   #19
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Lol, that was one of the simpler solutions I&#39;ve seen:



    I just added in a new "if" so that every time tempval and tempval2 were equal, 1 would be added to a new int called "correct". If, after all the letters were checked, correct was equal to n then it was a palindrome.

    Thanks for pointing out the mistake though
    I don&#39;t think I&#39;d have noticed if it weren&#39;t for you saying.
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  10. Software & Hardware   -   #20
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Whoops
    It&#39;s outputting "not a palindrome" for everything now. I&#39;ll need to try and fix it again
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

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