PDA

View Full Version : Java Help (programming)



4th gen
02-01-2004, 06:19 PM
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 (http://java.sun.com/j2se/1.3/docs/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 :)

DWk
02-01-2004, 07:02 PM
Try importing java.io for that matter.... can&#39;t remember though

4th gen
02-01-2004, 07:06 PM
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

DWk
02-01-2004, 07:22 PM
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 :)

4th gen
02-01-2004, 07:34 PM
Got it sorted now, just created a second string :frusty:

Thanks anyway, DWK

DWk
02-01-2004, 08:42 PM
Originally posted by 4th gen@1 February 2004 - 12:34
Got it sorted now, just created a second string :frusty:

Thanks anyway, DWK
An empty string?

Anytime :rolleyes:

4th gen
02-01-2004, 08:50 PM
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; :frusty:

Thanks anyway, DWK
An empty string?

Anytime :rolleyes: [/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");
}
}
}

DWk
02-01-2004, 08:56 PM
Cool B)

4th gen
02-02-2004, 04:58 AM
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? :unsure:

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");
}
}
}

4th gen
02-02-2004, 05:04 AM
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 :(

I.am
02-02-2004, 07:17 AM
well to get rid of whitespaces and other ascii characters you can use the String class function "trim (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html)"

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&copy;;
}
}

return dest.toString();
}

----
In case you get impatient here is the full code of your program
Pallindrome (http://java.sun.com/docs/books/tutorial/java/data/stringsummary.html) :D
___________________
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.

4th gen
02-02-2004, 07:28 AM
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 (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html)"

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 (http://java.sun.com/docs/books/tutorial/java/data/stringsummary.html) :D
___________________
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

I.am
02-02-2004, 07:50 PM
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.

4th gen
02-03-2004, 06:24 PM
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

4th gen
02-03-2004, 08:27 PM
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");
}
}
}

I.am
02-03-2004, 09:32 PM
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. :)

4th gen
02-03-2004, 09:50 PM
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. :)
http://www.arap78.dsl.pipex.com/output.JPG

:(

What makes those words "special"? Any ideas for a solution?

4th gen
02-03-2004, 09:52 PM
Just noticed why they&#39;re "special".
Give me a while to try and fix it, please :)

4th gen
02-03-2004, 10:10 PM
Lol, that was one of the simpler solutions I&#39;ve seen:

http://www.arap78.dsl.pipex.com/newoutput.JPG

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.

4th gen
02-03-2004, 10:28 PM
Whoops :(
It&#39;s outputting "not a palindrome" for everything now. I&#39;ll need to try and fix it again :(

I.am
02-04-2004, 06:35 AM
I&#39;ll take a look at it. Can you post your new code?

I.am
02-04-2004, 07:05 AM
Here is your modified code. I modified it so it does what its suppose to. By now you must have understood the cases I mentioned earlier. I did this really quick and it should work but if you find a case that doesnt let me know so I will look at it more carefully. :) Although, I am sure this will work.





/*
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Palindrome v1.0
#############Palindrome Revised###########
*/




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);

outputBox.printLine("---------Type stop&#33; to quit program-------------");

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("exit"))
{
userHalt=true;
break;
}

n=s2.length();
boolean pal= true;
for(int i=0; i<n/2; i++)
{
//n=s2.length();&nbsp; /-> No Need for this (CHANGE MADE HERE)
tempval=s2.charAt(i);
tempval2=s2.charAt(n-i-1);

//CHANGES MADE HERE
if(tempval&#33;=tempval2)
&nbsp; {
&nbsp; pal=false;
&nbsp; break;
&nbsp; }
}

//CHANGES MADE HERE
if(pal==true){
&nbsp; outputBox.printLine(s1 + " is a palindrome");
}
else
outputBox.printLine(s1 + " is not a palindrome");
}
}
}

4th gen
02-04-2004, 08:27 PM
Thanks a lot :)
That works perfectly
One little thing though is that you should change the text displayed in the outputbox to "type exit" to stop the program.

I.am
02-04-2004, 08:40 PM
You are welcome. :)

Oh yes, I used "exit" to quit instead of stop&#33;, but didnt change it in the display :lol: