String class question

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 473 Views - Last Post: 28 July 2011 - 09:12 AM Rate Topic: -----

#1 Rhotor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-July 11

String class question

Posted 28 July 2011 - 06:26 AM

Hello Dream.In.Code users,

I have the following assigment to do:

Write a Java program that continuosly reads in a single word from the keyboard. For each word read, if that word contains "or" at the end of the word, your program changes the "or" to "our". Your program should run forever until the user enters the word "quit". When "quit" is entered, your program completes.
NOTE: The String class has a method called "endWith (String suffix)". You CANNOT use this method!

Now, i have a general idea on how to tackle this problem, but i just do not understand how to code it. What i have so far:

do{      
        System.out.println("Hello, and Welcome to the Proper English Program!");
        BufferedReader input= new BufferedReader( new InputStreamReader (System.in));
        String word;
        word= input.readLine();
        
        //If word ends with "or", change to "our"
        //else, just display the result.
        //loop until user types "quit"
               
        
        
        
        }while(// word is not "Quit")



Because we are not allowed to use the .endsWith method, I do not know how to check if the string ends in a particular way.

In my opinion, the .replace(char letter, char another_letter) method could work, but if the word was for example : work, it would change it to: wourk, which is not what i want.

Could i use the .contains method to check it the string contains it, and if so, change the "or", to "our"?


thanks alot for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: String class question

#2 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: String class question

Posted 28 July 2011 - 06:34 AM

I would suggest regular expressions or use indexOf('o',string.length()-2)); If it returns -1 there is not an o at the second to last character in the string, otherwise there is. Remember though you would have to do another to test if 'O' was there since 'o' and 'O' are different. If there is an 'o' there check to see if the last letter is an r/R.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: String class question

Posted 28 July 2011 - 06:34 AM

If you want to check if "or" is at the end of the String, you can use the substring() method. Then when replacing "or" with "our", I would take the substring() containing everything except the last two characters, then append "our" to it.
Was This Post Helpful? 0
  • +
  • -

#4 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: String class question

Posted 28 July 2011 - 06:39 AM

View Postmacosxnerd101, on 28 July 2011 - 08:34 AM, said:

If you want to check if "or" is at the end of the String, you can use the substring() method. Then when replacing "or" with "our", I would take the substring() containing everything except the last two characters, then append "our" to it.


Would that work though on a word without or in it?
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: String class question

Posted 28 July 2011 - 06:44 AM

First I may suggest you use scanner for your case instead of BufferedReader. Then your question need just a simple if like
 if(word.substring(word.length() -2 , word.length()).equals("or"))
   //take a substring before or and concatenate it with our 

This post has been edited by smohd: 28 July 2011 - 06:44 AM

Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7283
  • View blog
  • Posts: 12,079
  • Joined: 19-March 11

Re: String class question

Posted 28 July 2011 - 07:33 AM

For your input, readline will obviously get you everything on the line. If you know that there will be one word per line, this is fine. Otherwise, you may have to figure out how to break up a String.

If I were going to write this without regex, I suppose I might make use of a few methods from the String class - trim(), indexOf(), length(), substring() would probably do the work. All of these are documented in the API for the String class, so go ahead and look at that and see what you can make of them. Mac's suggestion is a good one for returning the modified String. I'd use indexOf() and length() to find occurrences of "or" and check to see that they're at the end of a word.
I mention trim() because it's often useful to ensure that you're not dealing with leading or trailing whitespace.

EDIT: regex is a good way to solve this problem as well, and it's probably what I'd use, but regex is really pretty close to learning a second language. If you don't know regex now, it'll be very frustrating to try to learn it while you're also trying to learn plain old Java.
Best way to learn regex, imo, is through perl. Java's regex has some complications having to do with escaped characters in strings, as well as the OO implementation with Patterns and Matchers. It'll actually be easier and more useful to acquire perl and learn to deal with regular expressions there, where Strings are simpler and regex is actually part of the language, and then transfer that knowledge to Java.

This post has been edited by jon.kiparsky: 28 July 2011 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#7 Rhotor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-July 11

Re: String class question

Posted 28 July 2011 - 08:24 AM

Thanks to all for your replies.Unfourtunatly, i have no clue about how im going to code this. Would I first grab the length of the word, then use the . substring method to see if the second last letter is "o" and the last is "r"?

Would the code look something like this?

word.length()=lengthOfWord
System.out.println(word.substring((lengthOfWord-2),(lengthOfWord-1)))

//if it contains o and r, concatenate with "our" ( have no clue how to code this)





thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: String class question

Posted 28 July 2011 - 08:25 AM

View Postsmohd, on 28 July 2011 - 09:44 AM, said:

First I may suggest you use scanner for your case instead of BufferedReader. Then your question need just a simple if like
 if(word.substring(word.length() -2 , word.length()).equals("or"))
   //take a substring before or and concatenate it with our 

smohd already demonstrated how to check for this.
Was This Post Helpful? 0
  • +
  • -

#9 Rhotor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-July 11

Re: String class question

Posted 28 July 2011 - 08:28 AM

yes, i read that, but the problem is that no-where in my notes is the scanner,and so i believe that my teacher does not want us to use scanner. I am studying this for school in september. These are the assigments which will be given out during the course of the year.
Was This Post Helpful? 0
  • +
  • -

#10 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: String class question

Posted 28 July 2011 - 08:28 AM

Since it seems that your problem requires ONE word at a time you can do something like this

Scanner in = new Scanner(System.in);
String input = in.nextLine();
//input holds whatever the used typed in


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: String class question

Posted 28 July 2011 - 08:31 AM

Scanner, BufferedReader, whatever you choose to use to get input. I was referring to the substring() demonstration.
Was This Post Helpful? 0
  • +
  • -

#12 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: String class question

Posted 28 July 2011 - 08:36 AM

Do not forget, if your use buffered reader you are going to need a try catch block then.

String word = "";
		do{      
			System.out.println("Hello, and Welcome to the Proper English Program!");
			BufferedReader input= new BufferedReader( new InputStreamReader (System.in));
			try{
				word = input.readLine();
				System.out.println(word);
			}
			catch(Exception e)
			{
				System.out.println("Error");
			}




Or something similar to fit your needs
Was This Post Helpful? 0
  • +
  • -

#13 Rhotor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-July 11

Re: String class question

Posted 28 July 2011 - 08:56 AM

Thanks alot for your help:)

Im almost done the assigment. The only problem i have is that in my do loop, i want it to end when it says "quit", would it be achieved by using the following?

while(word.substring(word.length()-4,word.length()-3,word.length() - 2, word.length()).equals("quit"));


because as i understand it, .Length is the end of the word, -2 is the second last letter, -3 should be the third last letter. Am i correct?

Again, thanks alot for your help

This post has been edited by Rhotor: 28 July 2011 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: String class question

Posted 28 July 2011 - 08:58 AM

Why not just use the String equals() method on word directly? Why use substring() here?
Was This Post Helpful? 1
  • +
  • -

#15 Rhotor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-July 11

Re: String class question

Posted 28 July 2011 - 09:11 AM

Thank you all for helping me :)The practice assigment really helped me understand the String class.
Again, thanks alot.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2