• Lucid Dreaming - Dream Views




    Results 1 to 6 of 6
    Like Tree3Likes
    • 2 Post By Mzzkc
    • 1 Post By Replicon

    Thread: Help with a simple java program

    1. #1
      Member Achievements:
      Created Dream Journal Tagger First Class Made lots of Friends on DV Referrer Bronze 5000 Hall Points Veteran Second Class
      Daredevilpwn's Avatar
      Join Date
      Oct 2011
      LD Count
      15
      Gender
      Location
      MD
      Posts
      493
      Likes
      378
      DJ Entries
      63

      Help with a simple java program

      Hey guys please help me out. This is homework by the way so please don't spoon feed me code but instead give me some advice on what to do. My goal is to make a program that lets the user change a word in a string. First it prompts the user to enter a string then prompts what word to look for then prompts another for what word you want to use to replace the word you looked for.
      This is what I have so far.

      i
      Code:
      mport java.util.Scanner;
      public class replacement {
      
      
      		int pos = 0;
      		
      		int lengther = 0;
      		
      		String replacer = new String(); // placeholder for the string that is replacing another string
      		
      		String replacee = new String();
      		
      		String line1 = new String();
      		
      		String line2 = new String();
      	
      		Scanner checker = new Scanner(System.in); // new object 
      		
      		System.out.print("Enter your sentence: ");
      		line1 = checker.nextLine();
      		
      		System.out.print("What word do you want to look for?: "); // asks what word you want to scan
      		replacee = checker.nextLine();
      		
      		System.out.print("What word do you want to use?: "); // asks what word do you want to use to replace the word you looked for
      		replacer = checker.nextLine();
      		
      
      		while (pos >=0){ // stops at neg 1
      			pos = line1.indexOf(replacee); // gets position of word the user is looking for
      			
      			if (pos >=0){ // start if
      				line2 = line1.substring(0,pos);
      				
      				line2 = line2 + replacer;
      				
      				
      				
      				
      				
      			}// end if
      			
      	
      			
      			
      			
      		}// end loop
      		
      		
      		
      		
      		
      		
      	
      		
      		
      		
      		
      	}// end class
      
      }// end main
      What the problem is that this will partly work. It will replace one instance of the word the user inputs but after that it cuts everything else off. Also I know I can easily do this with the replace method but that is not what the teacher wants. It MUST be a while loop because that is what we as a class are focusing on right now. Any help is appreciated

    2. #2
      Member Achievements:
      Created Dream Journal Tagger First Class Made lots of Friends on DV Referrer Bronze 5000 Hall Points Veteran Second Class
      Daredevilpwn's Avatar
      Join Date
      Oct 2011
      LD Count
      15
      Gender
      Location
      MD
      Posts
      493
      Likes
      378
      DJ Entries
      63
      For some reason I can't edit my post. Just wanted to say that I am not using the 'lengther' variable so ignore that.

    3. #3
      Upside down Achievements:
      Tagger First Class Made lots of Friends on DV Vivid Dream Journal 5000 Hall Points Veteran Second Class
      Taffy's Avatar
      Join Date
      May 2010
      LD Count
      ~40
      Gender
      Posts
      1,416
      Likes
      807
      DJ Entries
      163
      Oof, been A while since I did this. Isn't there an indexOf method that looks for certain words? You could probably use that to seek out the replacee word.

      I hope someone more knowledgeable comes to help out, sorry if I wasn't too much help. :c

    4. #4
      The i's are invisible. Achievements:
      Tagger First Class Made lots of Friends on DV Vivid Dream Journal Populated Wall 10000 Hall Points Veteran First Class Referrer Silver
      Mzzkc's Avatar
      Join Date
      Mar 2009
      LD Count
      l҉ots
      Location
      Present Day. Present Time.
      Posts
      2,367
      Likes
      1688
      DJ Entries
      179
      A straightforward solution:

      1. Split the string along the white space.
      2. Store the resulting word-strings in an array (or linked list, I dunno what they've taught you yet).
      3. Get the users choice.
      4. Iterate over the array/list, using simple string comparisons to search for the user's chosen word.
      5. Replace the chosen word (in the array/list) with a new one.
      6. Reassemble message.

      Easy enough.
      Supernova and Daredevilpwn like this.

    5. #5
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      Couple of questions: Are you specifically replacing single words, or are you replacing any substring? For example, if the sentence is:

      "My cat is black"

      Can the user replace "s bl" and replace it with "nhales cr"

      so it becomes "My cat inhales crack" ? Or does it SPECIFICALLY have to be one word?

      Did the teacher specify any input guarantees? (e.g. the input line will be just letters and single spaces separating them)

      What if it's a sentence that ends with a period, and you want to replace the last word? If you just split on the space, you won't be able to compare with the last word.

      Basically, you need to really carefully read over the problem description for these programming questions, and even state all your assumptions in the comments.


      Finally, quick mini-feedback: You don't need those "placeholders" like where you go "String line1 = new String();"

      It's fine to just do:

      String line1 = checker.nextLine();


      All strings in Java are immutable. It's actually bad style to declare those "placeholders" and serves no purpose. I hope your teacher didn't recommend it.

      Otherwise, if it's safe to just assume words and spaces, Mzzkc's approach is just fine. When you re-assemble the string, use a StringBuilder, rather than a bunch of "+" operations.
      Mzzkc likes this.

    6. #6
      Member Achievements:
      Created Dream Journal Tagger First Class Made lots of Friends on DV Referrer Bronze 5000 Hall Points Veteran Second Class
      Daredevilpwn's Avatar
      Join Date
      Oct 2011
      LD Count
      15
      Gender
      Location
      MD
      Posts
      493
      Likes
      378
      DJ Entries
      63
      Thanks for the input Replicon! My teacher wants the program to simply replace ONE word. So if the user inputs "Hello sir, are you ok sir?. and wants to replace the word "sir" with "man" then the output should be "Hello man, are you ok man?" The teacher just wants words and spaces replaced so Mzzkc's method is what I am going to use.

    Similar Threads

    1. Help with this java program
      By Daredevilpwn in forum Homework Help
      Replies: 8
      Last Post: 02-02-2013, 05:23 PM
    2. Stubborn about java
      By Daredevilpwn in forum Tech Talk
      Replies: 7
      Last Post: 09-22-2012, 04:43 AM
    3. My brainfuck (Java)
      By Sornaensis in forum Tech Talk
      Replies: 2
      Last Post: 02-04-2012, 09:00 PM
    4. Windows users: small & simple lucidity-assisting program
      By Oneiroknot in forum Attaining Lucidity
      Replies: 8
      Last Post: 07-10-2010, 05:22 AM
    5. Starting On Java
      By Lonestar in forum Tech Talk
      Replies: 6
      Last Post: 01-02-2007, 02:18 PM

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •