Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
Member Login

Site Notices
Posted: 10/28/2013 3:01:43 PM EST
Got a project due and I have written the program out by hand and it don't seem to be compiling to what I get and I can't seem to find my screw up. It is working to find out if a word or sentence is a palindrome.

    public String itPalindrome(String element){

strElement = element;
if(strElement.length() <= 0){
return strElement;

}


while((strElement.length() - 1) >= 0){
for(int i = 0; i < strElement.length(); i++){
subElement = strElement.substring((strElement.length() - 1) - i);
revElement = revElement + subElement;
// revElement = strElement.substring((strElement.length() - 1) - i);
// subElement = subElement + revElement;

}
return revElement;


}


couple of things:
1. I am using james as a word to test that is not a palindrome
2. I have tried different variations to no avail

I should get with subElement the first go around s from james, then I need the e, the m, etc... but I don't get that.
Link Posted: 10/28/2013 3:15:38 PM EST
Java is a strongly-typed language (you have to declare the type of the variables you use)., You did not declare the type of 'strElement' (should be 'string strElement=element'). The same goes for 'subElement' and 'revElement'. If you don't declare those, the program will never compile.

And I assume that the "Main" method is somewhere else. Otherwise, that's a huge no-go.
Link Posted: 10/28/2013 3:24:48 PM EST
Two bugs, I'll give you hints......

First on this line...
while ((strElement.length() - 1) >= 0)

hint... strElement never changes.

Second, step through the code in the debugger and watch how subElement and revElement change. Then look up the Javadoc on substring, it is not doing what you think it is doing.

Link Posted: 10/28/2013 3:28:00 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By 1911ar-15:
Java is a strongly-typed language (you have to declare the type of the variables you use)., You did not declare the type of 'strElement' (should be 'string strElement=element'). The same goes for 'subElement' and 'revElement'. If you don't declare those, the program will never compile.

And I assume that the "Main" method is somewhere else. Otherwise, that's a huge no-go.
View Quote

I declared them higher, I just put in what I was having a problem with.

Yes the main is else where. our instructor want's everything in different classes.
Link Posted: 10/28/2013 3:28:09 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By 1911ar-15:
Java is a strongly-typed language (you have to declare the type of the variables you use)., You did not declare the type of 'strElement' (should be 'sString strElement=element'). The same goes for 'subElement' and 'revElement'. If you don't declare those, the program will never compile.

And I assume that the "Mmain" method is somewhere else. Otherwise, that's a huge no-go.
View Quote

fify
Link Posted: 10/28/2013 3:35:54 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By jeffmx5:
Two bugs, I'll give you hints......

First on this line...
[div style='margin-left: 40px;']while ((strElement.length() - 1) >= 0)

[div style='margin-left: 40px;']
hint... strElement never changes.

Second, step through the code in the debugger and watch how subElement and revElement change. Then look up the Javadoc on substring, it is not doing what you think it is doing.

View Quote


We haven't been taught to use the debugger yet

I am trying for the subString to give me the letter off of the end. When I run just the subElement = strElement.subString(strElement.length() - 1) it will give me s. Every thing changes once I put it inside the for loop.
Link Posted: 10/28/2013 3:48:36 PM EST
[Last Edit: 10/28/2013 3:49:05 PM EST by DevilDog22]
Did some reading around more and found a solution, however I am stuck with a double output, correct output but it displays twice

    public String itPalindrome(String element){

strElement = element;
if(strElement.length() <= 0){
return strElement;

}
// subElement = strElement.substring(strElement.length() - 1);

while((strElement.length() - 1) >= 0){
length = element.length() - 1;
for(int i = length; i >= 0; i--){
revElement = revElement + strElement.charAt(i);
// subElement = revElement;
}
return revElement;


}
return null;


}
Link Posted: 10/28/2013 3:49:00 PM EST
[Last Edit: 10/28/2013 3:53:57 PM EST by jeffmx5]
The substring method gives you everything to the right of the index you provide. So, for "James", on the first time thru the loop you get
"James".length() = 5 - 1 - 0 = 4
strElement.substring(4) = "s"
The second time thru the loop you get:
"James".length() = 5 - 1 - 1 = 3
strElement.substring(3) = "es"

The third time thru the loop you get:
"James".length() = 5 - 1 - 2 = 2
strElement.substring(3) = "mes"
and so on...

Use strElement.charAt() instead.


Nevermind, I see you figured that out as I was typing... it works for me just fine, not sure where you are seeing the double output.

Ditch the second if and change "if ((strElement.length() - 1) >= 0)" to just "else". If the first condition fails, the length must be > 0.

if (strElement.length() <= 0) {
return strElement;
}

else {
Link Posted: 10/28/2013 3:56:44 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By jeffmx5:
The substring method gives you everything to the right of the index you provide. So, for "James", on the first time thru the loop you get
[div style='margin-left: 40px;']"James".length() = 5 - 1 - 0 = 4
strElement.substring(4) = "s"

The second time thru the loop you get:
[div style='margin-left: 40px;']"James".length() = 5 - 1 - 1 = 3
strElement.substring(3) = "es"


The third time thru the loop you get:
[div style='margin-left: 40px;']"James".length() = 5 - 1 - 2 = 2
strElement.substring(3) = "mes"

and so on...



Use strElement.charAt() instead.


Nevermind, I see you figured that out as I was typing... it works for me just fine, not sure where you are seeing the double output.

Ditch the second if and change "if ((strElement.length() - 1) >= 0)" to just "else". If the first condition fails, the length must be > 0.

if (strElement.length() <= 0) {
return strElement;
}

else {
View Quote


I took out the while statement. So I only have

if(strElement <= 0)
return strElement


then I got the for loop.

when I type in james for the example my output shows

semajsemaj and I'm not seeing where that would happen
Link Posted: 10/28/2013 3:59:50 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By DevilDog22:


I took out the while statement. So I only have

if(strElement <= 0)
return strElement


then I got the for loop.

when I type in james for the example my output shows

semajsemaj and I'm not seeing where that would happen
View Quote View All Quotes
View All Quotes
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By DevilDog22:
Originally Posted By jeffmx5:
The substring method gives you everything to the right of the index you provide. So, for "James", on the first time thru the loop you get
[div style='margin-left: 40px;']"James".length() = 5 - 1 - 0 = 4
strElement.substring(4) = "s"

The second time thru the loop you get:
[div style='margin-left: 40px;']"James".length() = 5 - 1 - 1 = 3
strElement.substring(3) = "es"


The third time thru the loop you get:
[div style='margin-left: 40px;']"James".length() = 5 - 1 - 2 = 2
strElement.substring(3) = "mes"

and so on...



Use strElement.charAt() instead.


Nevermind, I see you figured that out as I was typing... it works for me just fine, not sure where you are seeing the double output.

Ditch the second if and change "if ((strElement.length() - 1) >= 0)" to just "else". If the first condition fails, the length must be > 0.

if (strElement.length() <= 0) {
return strElement;
}

else {


I took out the while statement. So I only have

if(strElement <= 0)
return strElement


then I got the for loop.

when I type in james for the example my output shows

semajsemaj and I'm not seeing where that would happen


Ha i'm an idiot.... it was in my main where the double display was
Link Posted: 10/28/2013 4:03:00 PM EST
Java sucks, and I have nothing useful to offer, because I'm a thread stomping dick.

Write it again, in C.
Link Posted: 10/28/2013 4:07:52 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By Subnet:
Java sucks, and I have nothing useful to offer, because I'm a thread stomping dick.

Write it again, in C.
View Quote


Jesus no, C# maybe.

In the future, post as much code as possible. It's hard to tell what's wrong when you don't include declared variables. Or, uh, don't be using globals. Yes, members count.
Link Posted: 10/28/2013 4:08:37 PM EST
Glad you found it OP.

Link Posted: 10/28/2013 4:09:45 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By AmorphousBlob:


Jesus no, C# maybe.

In the future, post as much code as possible. It's hard to tell what's wrong when you don't include declared variables. Or, uh, don't be using globals. Yes, members count.
View Quote View All Quotes
View All Quotes
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By AmorphousBlob:
Originally Posted By Subnet:
Java sucks, and I have nothing useful to offer, because I'm a thread stomping dick.

Write it again, in C.


Jesus no, C# maybe.

In the future, post as much code as possible. It's hard to tell what's wrong when you don't include declared variables. Or, uh, don't be using globals. Yes, members count.
I'm okay with C#.

I refuse to help him with Java, on principle.

I really have no decent reason for any of this, I'm just being a dick. Plus, he's getting good suggestions, already.
Link Posted: 10/28/2013 4:17:16 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By Subnet:
I'm okay with C#.

I refuse to help him with Java, on principle.

I really have no decent reason for any of this, I'm just being a dick. Plus, he's getting good suggestions, already.
View Quote View All Quotes
View All Quotes
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By Subnet:
Originally Posted By AmorphousBlob:
Originally Posted By Subnet:
Java sucks, and I have nothing useful to offer, because I'm a thread stomping dick.

Write it again, in C.


Jesus no, C# maybe.

In the future, post as much code as possible. It's hard to tell what's wrong when you don't include declared variables. Or, uh, don't be using globals. Yes, members count.
I'm okay with C#.

I refuse to help him with Java, on principle.

I really have no decent reason for any of this, I'm just being a dick. Plus, he's getting good suggestions, already.




Thanks everyone for the help!
Link Posted: 10/28/2013 4:21:39 PM EST
C# is for losers.
Link Posted: 10/28/2013 4:23:42 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By JaxShooter:
C# is for losers.
View Quote


I see the Ruby programmers have staggered onto the scene.
Link Posted: 10/28/2013 4:25:20 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By AmorphousBlob:


I see the Ruby programmers have staggered onto the scene.
View Quote View All Quotes
View All Quotes
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By AmorphousBlob:
Originally Posted By JaxShooter:
C# is for losers.


I see the Ruby programmers have staggered onto the scene.

Link Posted: 10/28/2013 4:25:24 PM EST
Ughhhh. . . FUCK. . . . JAVA!!!!!
Link Posted: 10/28/2013 4:52:11 PM EST
Java can burn in hell.
Link Posted: 10/28/2013 6:03:39 PM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By Enigma102083:
Java can burn in hell.
View Quote


Yea I am coming to that conclusion as well!
Link Posted: 10/28/2013 10:10:12 PM EST
I thought this thread was for coffee....I'm leaving...

Link Posted: 10/28/2013 10:32:33 PM EST
Link Posted: 10/29/2013 2:48:40 AM EST
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By AmorphousBlob:


I see the Ruby programmers have staggered onto the scene.
View Quote View All Quotes
View All Quotes
Discussion ForumsJump to Quoted PostQuote History
Originally Posted By AmorphousBlob:
Originally Posted By JaxShooter:
C# is for losers.


I see the Ruby programmers have staggered onto the scene.

Why? Never touched Ruby. Ok, not entirely true, I did download it once.

Pure Java baby.

7 billion transactions last year. Suck it.
Top Top