Replace2 - String answer = "";...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class Replace2 //messed up - not working correctly, don't feel like fixing { public static void main(String[] args) { System.out.print("Enter a sentence: "); String sentence = IO.readString(); System.out.print("Replace what: "); String oldword = IO.readString(); System.out.print("With what: "); String newword = IO.readString(); if (oldword.equals(newword)) { System.out.println(sentence); return; } sentence = sentence + " ";
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: String answer = ""; int spaceindex = sentence.indexOf(" "); i while (spaceindex != -1) { String firstword = sentence.substring(0, spaceindex); if (firstword.equals(oldword)) { answer += answer + newword + " "; } else { answer += answer + firstword + " "; } sentence = sentence.substring(spaceindex + 1); spaceindex = sentence.indexOf(" "); } answer = answer.trim(); IO.outputStringAnswer(answer); } }...
View Full Document

This note was uploaded on 11/03/2009 for the course CS 01-111 taught by Professor Pradiphari during the Fall '09 term at Rutgers.

Ask a homework question - tutors are online