Results 1 to 4 of 4
  1. #1
    andys is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default how to do i make my program to do.....please help

    Ask the user for a word to find in the text. Then display all the text in lower case, except for the target word, which should be in upper case. Also display how many times the target word was found.


    Can someone please give me some direction on how to go about this.....thank you

  2. #2
    adhoc334 is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default

    various ways. i'll go over getting input, finding the number of matches, and then a way to do the uppercase thing.

    for one, there are multiple ways to ask for the input. GUI-wise, you'd need to make a textfield and a button, and every time the user presses the button it searches for the word you're looking for. As for command lines, I never use them for this, but I think either System.input or System.in (Google it) works for input.

    As for finding the string and counting how many times it occurs, you can either use String methods or regex.
    Regex is the way to do it these days, but it seems a little daunting at first. I recommend looking up a tutorial for it online if you need a deeper understanding, but this should work:
    Java Code:
        	 	String mass = "i looked looked looked but didn't find find cats cats catsc"; //replace this string with the text you're searching through
    		
    		String expression = "cats";  // replace this with the text you're looking for
    		int matches = 0;
    	 	CharSequence inputStr = mass;  
    	 	Pattern pattern = Pattern.compile(expression);  
    	 	try {
        	 	Matcher matcher = pattern.matcher(inputStr);  
        	 	
        	 	while(matcher.find()){  
    
        		 	matches++;
        	 	} 
        	 	System.out.println(matches);
        	 	
    	 	}catch (Exception e) {
    	 		System.out.println("no matches");
    	 	}
    As for the uppercase thing, use a combination of reaplceAll() and the toUpperCase() methods.
    be sure to actually update the string when using replaceAll, too. (ex: string = string.replaceAll... (vs) string.replaceAll. just doing string.replaceAll won't update your string, but can be used within System.out.println to perform the operation and print the results with the toUpperCase results included).

    good?
    Last edited by adhoc334; 11-25-2010 at 11:34 PM.

  3. #3
    andys is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by adhoc334 View Post
    various ways. i'll go over getting input, finding the number of matches, and then a way to do the uppercase thing.

    for one, there are multiple ways to ask for the input. GUI-wise, you'd need to make a textfield and a button, and every time the user presses the button it searches for the word you're looking for. As for command lines, I never use them for this, but I think either System.input or System.in (Google it) works for input.

    As for finding the string and counting how many times it occurs, you can either use String methods or regex.
    Regex is the way to do it these days, but it seems a little daunting at first. I recommend looking up a tutorial for it online if you need a deeper understanding, but this should work:
    Java Code:
        	 	String mass = "i looked looked looked but didn't find find cats cats catsc"; //replace this string with the text you're searching through
    		
    		String expression = "cats";  // replace this with the text you're looking for
    		int matches = 0;
    	 	CharSequence inputStr = mass;  
    	 	Pattern pattern = Pattern.compile(expression);  
    	 	try {
        	 	Matcher matcher = pattern.matcher(inputStr);  
        	 	
        	 	while(matcher.find()){  
    
        		 	matches++;
        	 	} 
        	 	System.out.println(matches);
        	 	
    	 	}catch (Exception e) {
    	 		System.out.println("no matches");
    	 	}
    As for the uppercase thing, use a combination of reaplceAll() and the toUpperCase() methods.
    be sure to actually update the string when using replaceAll, too. (ex: string = string.replaceAll... (vs) string.replaceAll. just doing string.replaceAll won't update your string, but can be used within System.out.println to perform the operation and print the results with the toUpperCase results included).

    good?

    hey thnx very much for ur help....but can u tell me how to find the string and count it.....using the string method plz......as we haven't covered regex yet........thank you soo much

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Hi andys,

    What have you done yourself to tackle this problem? It is much appreciated at this forum to show some of your own effort. And don't be ashamed of showing bad code. You'll definitely learn from the remarks you'll get.

    Hint for your problem: have a look at the indexOf methods of the String class.

    Succes,
    Erik
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. How to make autorun program
    By arshadalisoomro in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 08:09 PM
  2. Help with a program ro make a landscape picture
    By no s e e d s in forum New To Java
    Replies: 1
    Last Post: 12-16-2009, 12:03 AM
  3. Make a program notify
    By McChill in forum New To Java
    Replies: 7
    Last Post: 06-03-2009, 09:17 PM
  4. Trying to make a GUI program
    By GettinGwap in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 11:37 PM
  5. i need help to make this program
    By masaka in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 01:26 PM

Posting Permissions

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