• Post Reply Bookmark Topic Watch Topic
  • New Topic

dumb beginner problems  RSS feed

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so im just wanting to make sure im getting the basics down good. im a bit confused

1when using method .substring(x,x) i guess i cant use IgnoreCase() but why cant i use toLowerCase()

2i guess im just getting confused on how to combine these for manipulating strings.

3 for example could i do this: someString.equals(substring(0,someString.length() - 4);)

basically what i am trying to do is ask a user to enter a string and then see if that word contains a censored word like darn or what ever, (without using contains method)


 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use .equalsIgnoreCase at all?
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:Why use .equalsIgnoreCase at all?


well i mean if i sayed if userInput contains darn then do this... but the user entered DARN wouldnt it skip it and go to the else part of the if statement

plus im just wondering if i can lower case a substring method
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:
Brian Barrick wrote:Why use .equalsIgnoreCase at all?


well i mean if i sayed if userInput contains darn then do this... but the user entered DARN wouldnt it skip it and go to the else part of the if statement


With .toLowerCase() will userInput ever equal DARN?
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:
Sean Paulson wrote:
Brian Barrick wrote:Why use .equalsIgnoreCase at all?


well i mean if i sayed if userInput contains darn then do this... but the user entered DARN wouldnt it skip it and go to the else part of the if statement


With .toLowerCase() will userInput ever equal DARN?


oh i see, yeah i just put that in there because that was the easiest way i could find to do it. but im just wondering for learning purposes, just trying different ways to learn it well.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would do what you want. Substring has to be run on a string. I guess in reality you use your list of strings in there.

if(userInput.equalsIgnoreCase("durnit".substring(0,userInput.length()))) {
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:
Brian Barrick wrote:
Sean Paulson wrote:
Brian Barrick wrote:Why use .equalsIgnoreCase at all?


well i mean if i sayed if userInput contains darn then do this... but the user entered DARN wouldnt it skip it and go to the else part of the if statement


With .toLowerCase() will userInput ever equal DARN?


oh i see, yeah i just put that in there because that was the easiest way i could find to do it. but im just wondering for learning purposes, just trying different ways to learn it well.


I'm not a Java programmer yet but as far as I know that's a perfectly acceptable way to grab a string and work with it. For newbs like us anyway it beats creating a switch or if statement for every possible way a user could enter something.

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:This would do what you want. Substring has to be run on a string. I guess in reality you use your list of strings in there.

if(userInput.equalsIgnoreCase("durnit".substring(0,userInput.length()))) {


well that makes sense but im getting an error

enter a phrase: hello darn
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.String.substring(String.java:1950)
at testIf.main(testIf.java:13)
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it to work using if(userInput.indexOf("darn") != -1) but couldnt i use substring?
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:ok i got it to work using if(userInput.indexOf("darn") != -1) but couldnt i use substring?


Did you try userInputLength() - 1?

Or even (substring(1,userInput.length())?
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:
Sean Paulson wrote:ok i got it to work using if(userInput.indexOf("darn") != -1) but couldnt i use substring?


Did you try userInputLength() - 1?

Or even (substring(1,userInput.length())?


yes but how would i see if that contained darn? would i do "darn".equals(userInput.substring(0,userInput.length()) ?
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here this is from introduction to java programing i just found
indexOf(ch: char): int
+indexOf(ch: char, fromIndex: int): int +indexOf(s: String): int
+indexOf(s: String, fromIndex: int): int +lastIndexOf(ch: int): int
+lastIndexOf(ch: int, fromIndex: int): int +lastIndexOf(s: String): int
+lastIndexOf(s: String, fromIndex: int): in
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:
Brian Barrick wrote:
Sean Paulson wrote:ok i got it to work using if(userInput.indexOf("darn") != -1) but couldnt i use substring?


Did you try userInputLength() - 1?

Or even (substring(1,userInput.length())?


yes but how would i see if that contained darn? would i do "darn".equals(userInput.substring(0,userInput.length()) ?


 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:
Sean Paulson wrote:
Brian Barrick wrote:
Sean Paulson wrote:ok i got it to work using if(userInput.indexOf("darn") != -1) but couldnt i use substring?


Did you try userInputLength() - 1?

Or even (substring(1,userInput.length())?


yes but how would i see if that contained darn? would i do "darn".equals(userInput.substring(0,userInput.length()) ?




yeah i did that but it wont work i enter a whole sentence with the word darn.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yeah i did that but it wont work i enter a whole sentence with the word darn.


Ah, ok. I got you. That will work if the only thing entered is darn.

Back to the drawing board. Unless one of the head hanchos steps in and sets us straight. I think there is another method we can use for this. Let me look.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(userInput.contains("darn"))

Then you could use if or switch logic to further determine whether or not they entered darnit or darnydarndarn etc..
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:

yeah i did that but it wont work i enter a whole sentence with the word darn.


Ah, ok. I got you. That will work if the only thing entered is darn.

Back to the drawing board. Unless one of the head hanchos steps in and sets us straight. I think there is another method we can use for this. Let me look.


yeah you can use contains but the tutorial im using hasent shown that so im not wanting to use it i got it to work using



but it does seem like you would be able to use substring but you might have to put each character into an array
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:
Brian Barrick wrote:

yeah i did that but it wont work i enter a whole sentence with the word darn.


Ah, ok. I got you. That will work if the only thing entered is darn.

Back to the drawing board. Unless one of the head hanchos steps in and sets us straight. I think there is another method we can use for this. Let me look.


yeah you can use contains but the tutorial im using hasent shown that so im not wanting to use it i got it to work using



but it does seem like you would be able to use substring but you might have to put each character into an array


Well I may be mistaken here but substring is looking for the string in a specific location. So if you knew exactly where the word was going to be then you could use substring. However, you might be able to drop a loop in the substring method and force it to look at each location of the string.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is indexOf, but that is used by contains behind the scenes.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about this im doing another little program that gives the meaning of abbreviations such as if a user enters lol it says lol = laughing out loud. how would i make a simple spell checker to see if the user enters l, o, l in any order and then have the computer say did you mean lol = laughing out loud.

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote:
well that makes sense but im getting an error

enter a phrase: hello darn
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.String.substring(String.java:1950)
at testIf.main(testIf.java:13)


Strange. It ran fine for me. Only change was removed public from the class so wouldn't have to rename my test file.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to figure this out, but I cannot seem to wrap my feeble mind around the problem/solution that you are looking for. What you are looking for, as I understand it, is a substring of characters within a string. I'm all for writing obfuscating my code as much as the next guy, but .contains() is the right tool for the job in this instance.

Accoring to the Java Docs, substring returns a new string that is a substring of this string. Example:


Based on your requirements, as near as I can, you won't see your 'naughty word, at least not without a lot of extraneous code, walking through the entire string, one substring at a time. Take the phrase, 'This is a darn string.' How would you pick out darn without knowing ahead of time the start and ending positions?

Maybe I don't understand the issue; which is highly likely.

Regards,
Robert
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:I have been trying to figure this out, but I cannot seem to wrap my feeble mind around the problem/solution that you are looking for. What you are looking for, as I understand it, is a substring of characters within a string. I'm all for writing obfuscating my code as much as the next guy, but .contains() is the right tool for the job in this instance.

Accoring to the Java Docs, substring returns a new string that is a substring of this string. Example:


Based on your requirements, as near as I can, you won't see your 'naughty word, at least not without a lot of extraneous code, walking through the entire string, one substring at a time. Take the phrase, 'This is a darn string.' How would you pick out darn without knowing ahead of time the start and ending positions?

Maybe I don't understand the issue; which is highly likely.

Regards,
Robert


I've been trying...

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah in the book im reading it says you need to use indexOf() or contains()

and for my other problem someone told me you can use a indexOf in another indexOf such as userInput.indexOf("l" , userinput.indexOf("i" , +1)) to find the second occurence of a char in a string but i havent been able to get it to work
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:
Robert D. Smith wrote:I have been trying to figure this out, but I cannot seem to wrap my feeble mind around the problem/solution that you are looking for. What you are looking for, as I understand it, is a substring of characters within a string. I'm all for writing obfuscating my code as much as the next guy, but .contains() is the right tool for the job in this instance.

Accoring to the Java Docs, substring returns a new string that is a substring of this string. Example:


Based on your requirements, as near as I can, you won't see your 'naughty word, at least not without a lot of extraneous code, walking through the entire string, one substring at a time. Take the phrase, 'This is a darn string.' How would you pick out darn without knowing ahead of time the start and ending positions?

Maybe I don't understand the issue; which is highly likely.

Regards,
Robert


I've been trying...



is it working?
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is it working?


I can get the second half to print out whatever the user enters. Can't get the first half to censor it if it contains a specific word though...
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a more evil way to do this:


May seem like a lot of work, but case translation can be very efficient.

An even MORE evil way of doing case-insensitive scanning for forbidden words involves using Regular Expressions (regex'es). But that's best left until you're a little braver. We use that for the JavaRanch detector logic, incidentally. It can tell the difference between "darn you!" and "darning needle".

Stuff like this is why I fail those stupid job placement aptitude tests. They demand the "right" answer and I know lots of answers.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:


is it working?


I can get the second half to print out whatever the user enters. Can't get the first half to censor it if it contains a specific word though...


And it probably won't work that way, but I'm learning some interesting stuff about the substring method while I'm playing around.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got the loop working but I think because of the way Java returns substring I'm not sure how to equate the two. If you put a print statement in there you will see that it actually cycles through each word backwards and at one point equals the word "darn".

 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know you already figured that one out but once I get rolling on a problem I have to figure it out. Anyway, just change that to compare.equals() and it works. I should have caught that.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In regards to the other problem are you getting an error with what you have?
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah what i did is add another else if statement after each using nested indexOf() statements i posted about it here http://www.coderanch.com/t/652974/java/java/small-spell-checker-program#3018766 still working on it though.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats wrong with this says i cant use the or statement, or how could i just ignore or lower the case of indexOf?



 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Paulson wrote: whats wrong with this says i cant use the or statement, or how could i just ignore or lower the case of indexOf?





Try splitting them in two. if(statement) || (statement) { }
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!