Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Java Help (programming)

  1. #1
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Ok, myself and a couple of guys from uni are working on a program which reads an input and outputs if the input is a palindrome or not. We're not allowed to use any stringBuffer methods, so this is where we're at with the code so far:

    import javabook.*;

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

    String s1="";
    char temp=0;
    char temp2=0;
    int n=0;
    boolean userHalt=false;

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

    if(s1.equalsIgnoreCase("stop!"))
    userHalt=true;

    for(int i =0; i<s1.length(); i++)
    {
    n=s1.length();
    temp=s1.charAt(0);
    temp2=s1.charAt(n-1);
    temp=temp++;
    temp2=temp2--;
    }

    if(temp==temp2)
    outputBox.printLine(s1 + " is a palindrome");
    else
    outputBox.printLine(s1 + " is not a palindrome");
    }
    }
    }

    The program works fine, until you mix cases. For example, with an input of "poop", it will return "poop is a palindrome". However, with an input of "Poop", it returns "Poop is not a palindrome". According to the Java API, we should be able to use toUpperCase, or toLowerCase to sort this, but I can&#39;t get it to work...

    Any help or advice would be greatly appreciated
    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.

  2. Software & Hardware   -   #2
    BANNED
    Join Date
    Jul 2003
    Location
    Guatemala
    Posts
    4,044
    Try importing java.io for that matter.... can&#39;t remember though

  3. Software & Hardware   -   #3
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Originally posted by DWk@1 February 2004 - 18:02
    Try importing java.io for that matter.... can&#39;t remember though
    The way we&#39;re taught we don&#39;t need to import io separetly. It&#39;s included in the javabook, I believe
    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.

  4. Software & Hardware   -   #4
    BANNED
    Join Date
    Jul 2003
    Location
    Guatemala
    Posts
    4,044
    Uhhhh well I don&#39;t know about javabook. However, the only lib that you don&#39;t have to import is java.lang, which is imported by default.

    So why not try anyway

  5. Software & Hardware   -   #5
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Got it sorted now, just created a second string

    Thanks anyway, DWK
    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   -   #6
    BANNED
    Join Date
    Jul 2003
    Location
    Guatemala
    Posts
    4,044
    Originally posted by 4th gen@1 February 2004 - 12:34
    Got it sorted now, just created a second string

    Thanks anyway, DWK
    An empty string?

    Anytime

  7. Software & Hardware   -   #7
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Originally posted by DWk+1 February 2004 - 19:42--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (DWk @ 1 February 2004 - 19:42)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-4th gen@1 February 2004 - 12:34
    Got it sorted now, just created a second string&nbsp;

    Thanks anyway, DWK
    An empty string?

    Anytime [/b][/quote]
    Here&#39;s my final code (unless i change the position of the input prompt):

    import javabook.*;

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

    String s1="";
    String s2="";
    char temp=0;
    char temp2=0;
    int n=0;
    boolean userHalt=false;

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

    if(s1.equalsIgnoreCase("stop&#33;"))
    userHalt=true;

    for(int i =0; i<s1.length(); i++)
    {
    n=s2.length();
    temp=s2.charAt(0);
    temp2=s2.charAt(n-1);
    temp=temp++;
    temp2=temp2--;
    }

    if(userHalt==true)
    break;

    if(temp==temp2)
    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.

  8. Software & Hardware   -   #8
    BANNED
    Join Date
    Jul 2003
    Location
    Guatemala
    Posts
    4,044
    Cool B)

  9. Software & Hardware   -   #9
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    The extra challenge for the program is to take the user input of a phrase (rather than a single word), ignoring punctuation and spaces. I think I have to use removeJunk but I can&#39;t seem to implement it correctly. Whenever I attempt to compile, I get:

    "--------------------Configuration: j2sdk1.4.2 <Default>--------------------
    C:&#092;Palinextra.java:24: cannot resolve symbol
    symbol : method RemoveJunk (java.lang.String)
    location: class Palinextra
    s3 = RemoveJunk(s2); // Removes punctuation from String s2
    ^
    1 error

    Process completed."

    Anyone know how to sort that error out?

    Here&#39;s my code:

    import javabook.*;

    public class Palinextra
    {
    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=""; // Intitial string
    String s2=""; // Second string
    String s3=""; // &#39;Clean&#39; String
    char tempval=0; // Temporary store for first character in analysis
    char tempval2=0; // Temp store for second character in analysis
    int n=0; // N number of letter in input word
    boolean userHalt=false; // Initialising condition for program halt to false

    while(userHalt==false) // While loop, works whilst user not halted program
    {
    s1 = inputBox.getString("Please type in a word: " ); // Reads user input
    s2 = s1.toUpperCase(); // Converts user input to uppercase for easier comparison
    s3 = removeJunk(s2); // Removes punctuation from String s2

    if(s1.equals("stop&#33;")) // If user types "stop&#33;" the program will halt
    userHalt=true;

    if(userHalt==true) // Terminate program when user inputs "stop&#33;"
    break;

    for(int i =0; i<s1.length(); i++) // For loop determining number of times the check should be run
    {
    n=s3.length(); // &#39;N&#39; takes value of the length of string s2
    tempval=s3.charAt(0); // tempval is the first character on the string
    tempval2=s3.charAt(n-1); // tempval2 is the last character on string
    tempval=tempval++; // Increments tempval by one (to allow comparison of next character)
    tempval2=tempval2--; // Decrements tempval2 by one
    }

    if(tempval==tempval2)
    outputBox.printLine(s1 + " is a palindrome"); // Outputs if the user input is a palindrome or not
    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.

  10. Software & Hardware   -   #10
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    New code (removed a few superfluous lines):

    import javabook.*;

    public class Palinextra
    {
    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=""; // Intitial string
    String s2=""; // Second string
    String s3=""; // &#39;Clean&#39; String
    char tempval=0; // Temporary store for first character in analysis
    char tempval2=0; // Temp store for second character in analysis
    int n=0; // N number of letter in input word
    boolean userHalt=false; // Initialising condition for program halt to false

    while(userHalt==false) // While loop, works whilst user not halted program
    {
    s1 = inputBox.getString("Please type in a word: " ); // Reads user input
    s2 = s1.toUpperCase(); // Converts user input to uppercase for easier comparison
    s3 = removeJunk(s2); // Removes punctuation from String s2

    if(s1.equals("stop&#33;")) // If user types "stop&#33;" the program will break
    break;

    for(int i =0; i<s1.length(); i++) // For loop determining number of times the check should be run
    {
    n=s3.length(); // &#39;N&#39; takes value of the length of string s2
    tempval=s3.charAt(0); // tempval is the first character on the string
    tempval2=s3.charAt(n-1); // tempval2 is the last character on string
    tempval=tempval++; // Increments tempval by one (to allow comparison of next character)
    tempval2=tempval2--; // Decrements tempval2 by one
    }

    if(tempval==tempval2)
    outputBox.printLine(s1 + " is a palindrome"); // Outputs if the user input is a palindrome or not
    else
    outputBox.printLine(s1 + " is not a palindrome");
    }
    }
    }

    Problem still persists though
    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 1 of 3 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
  •