Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By JosAH

Thread: Need Help with JAVA String

  1. #1
    thienvina is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Need Help with JAVA String

    Im trying to make it so that I can't to out put as the example! is only:
    S: - - - - ; A: - A - A ; V: - - V -; J : J - - -
    Help Please ME!

    Thank you very much!
    - - - -
    S
    - - - -
    A
    - A - A
    V
    - A V A
    D
    - A V A
    J
    J A V A
    Bingo! You won.

    This is my program:

    import java.util.*;
    class GuessWordJava {
    public static void main ( String[] args ){
    Scanner scanner = new Scanner(System.in);
    //scanner.useDelimiter(System.getProperty("line.sepa rator"));
    System.out.print("\n Enter secret entence : ");
    String sentence = scanner.next();

    int length = sentence.length();

    while( true) {
    char letter;
    System.out.print("\n Enter your guess : ");
    char ch = scanner.next().charAt(0);
    if ( ch == ' ') {break;}
    String str="";
    for (int i = 0; i < length; i++){
    letter = sentence.charAt(i);
    if ( letter == ch ){
    str=str+letter;
    } else {
    str= str+ "-";
    }
    }
    System.out.print(str);
    }
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Need Help with JAVA String

    In the poll thread I closed (and have now removed) I asked you to go through some links before starting a new thread. Clearly, you didn't bother.

    Here they are again:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Need Help with JAVA String

    Consider using a char array to hold the guessed letters. Iterate over the secret word and for each guessed letter copy it into the array at the same position.

  4. #4
    thienvina is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need Help with JAVA String

    Please send to me - code for solution ! Thank for your help!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need Help with JAVA String

    Quote Originally Posted by thienvina View Post
    Please send to me - code for solution ! Thank for your help!
    Are you nuts? Sending code isn't helping; you probably wouldn't understand it anyway and you'd probably try to run it without looking at it or understanding the code; it might attempt to destroy the world as we know it. Do you take the blame?

    Jos
    DarrylBurke and Zigster like this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    thienvina is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need Help with JAVA String

    Jos - I'm beginner and I can't finish it for long time! so I'd like to complete code for my program!
    - Your reply are not value! Every mad - fool men can say that!
    Are you a teacher or educator or...... ??? plz see yourself in mirror!

    Thien

  7. #7
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Need Help with JAVA String

    He was kidding you. I'm sure he would be happy to write the code for you at his current rate. Well, maybe not after you called him a fool. Do they have mirrors where you live?

  8. #8
    thienvina is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need Help with JAVA String

    I finish my program - This is my complete programe!
    Java Code:
    import java.util.*;
    class  GuessWordJava {
    	public static void main ( String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.print("\n Enter sentence : ");	
    		String str = scanner.next();
    		StringBuffer str1 = new StringBuffer();
    		System.out.print("\n Enter char : ");	
    		char ch = scanner.next().charAt(0);
    		char letter;
    		for( int i = 0; i < str.length(); i++)	{
    			letter = str.charAt(i);
    			if ( letter ==ch){
    				str1.append(ch);
    			} else {
    				str1.append('-');
    		}
    		}
    		System.out.println(str1);
    		while(true) {
    		System.out.print("\n Enter char : ");	
    		char ch1 = scanner.next().charAt(0);
    		for( int j = 0; j < str.length(); j++)	{
    			letter = str.charAt(j);
    			if ( letter == ch1){
    				str1.setCharAt(j,ch1);
    				}
    		}
    		System.out.println(str1);
    		}
    		}
    }
    Last edited by JosAH; 05-25-2012 at 08:29 AM. Reason: added [code] ... [/code] tags

Similar Threads

  1. Replies: 12
    Last Post: 09-23-2011, 01:25 PM
  2. Replies: 3
    Last Post: 05-02-2011, 07:51 AM
  3. Replies: 4
    Last Post: 01-01-2011, 10:52 AM
  4. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 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
  •