Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default please i need help with a simple thing

    hellow there..

    i want to Write a Java program that contains a main() method with code that does the following:
    Has final data storage for a character with the value 'r'. (This is the "secret" letter.)
    Allows the the user to enter a word (their guess), and accepts the user's entry; when the user enters "stop" the program ends.
    When a word other than "stop" is entered, the user sees a message telling them whether or not their guess contained the secret letter and is asked for their next guess.
    Sample output:
    enter your word, stop to end.
    hello
    Your guess does not contain the secret letter
    enter next word, stop to end.
    frog
    Your guess contains the secret letter
    enter next word, stop to end.
    stop


    i've done same procedures but with integers like this
    Java Code:
    import java.util.Scanner;
    
    public class Secret
    {
    	public static void main(String[] args)
    	{
    		Scanner sc;
    		int hidden;
    		int guess;
    		
    		hidden = 5;
    		
    		sc = new Scanner(System.in);
    		do
    		{
    			System.out.println("enter the correct number");
    
    			guess = sc.nextInt();
    			
    			
    			
    		}
    		while (guess != hidden);
    		
    		System.out.println("Well done");		
    	}	
    }
    and it comes right but i wont to implement same procedures with words or strings .. please help ..

    regards

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    with words or strings
    Look at the API doc for the Scanner class. It has methods to read Strings.

  3. #3
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    i did before creating the thread

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    What method(s) did you see that would read Strings vs ints?

    Have you changed your program to use them?

  5. #5
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default y

    yes sir
    am tired tring

    this is what i've done

    Java Code:
    import java.util.Scanner; 
      
    public class secsec
    { 
        public static void main(String[] args) 
        { 
        	Scanner sc;
        	String ab="Rr";
    		char guess; 
    		String letter="";
    		
            sc = new Scanner(System.in);
    		do 
            { 
    
               System.out.print("enter your word "); 
               guess = sc.next(); 
               letter = guess.charAt(0);
            } 
            while (letter!=ab); 
    		
            System.out.println("Well done" ); 
        } 
    }
    but there are errors
    its not coming like my wishes

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    there are errors
    If there are error messages, please copy and paste them here.
    its not coming like my wishes
    Please describe.

  7. #7
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    these are my errors
    --------------------Configuration: <Default>--------------------
    C:\Documents and Settings\user\Desktop\Zalahdi\Z~O~H~D~I Collection\My Documents\secsec.java:17: incompatible types
    found : java.lang.String
    required: char
    guess = sc.next();
    ^
    C:\Documents and Settings\user\Desktop\Zalahdi\Z~O~H~D~I Collection\My Documents\secsec.java:18: char cannot be dereferenced
    letter = guess.charAt(0);
    ^
    2 errors

    Process completed.


    .. i want the output to ask me to entar a word .. if that word contains "r" letter .. then we find the secret and the program ends.. if not.. try again .. and thats it

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    What is type is the variable guess? Does it have a method: charAt() ?

    Scanner.next() returns a String.

  9. #9
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    actually i dont know ..
    as i told you i want the output to ask me to entar a word .. if that word contains "r" letter .. then we find the secret and the program ends.. if not.. try again .. and thats it

    i dont know if it requires charAt

    am a begginer

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Do you know what a data type is? For example int and char are data types. String is a class that defines a type.
    Where in your program do you define the variable guess? Usually it is on the first line in the program that references the variable.

    You need to read the Java API doc for the String class and see what it says for the chartAt() method.

    You can NOT write a program by making things up. You must follow the rules according to the language definitions and the class definitions.

  11. #11
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    actually am not good in english and we learn java in french so am confused

    culd you please write the programm for me i must hand it within 2 hours and it wont take 5 minutes from you pleese

  12. #12
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    i've done it

  13. #13
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by zohdi View Post
    i've done it
    Congratulations! It's much better if you write it anyway. :)

  14. #14
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    public static void main(String[] args)  
            {  
                    Scanner sc;  
                    char hidden = 'R';  
                    String guess;  
                    sc = new Scanner(System.in);  
                    do  
                    {  
                            System.out.println("enter the word");  
                            guess = sc.nextLine().toUpperCase();                            
                    }  
                    while (guess.indexOf(hidden) == -1);  
                      
                    System.out.println("Well done");                  
            }   
    }


    But i want the output to be like this
    Sample output:
    enter your word, stop to end.
    hello
    Your guess does not contain the secret letter
    enter next word, stop to end.
    frog
    Your guess contains the secret letter
    enter next word, stop to end.
    stop

    could you help me pleasee

  15. #15
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You've done most of the work already. The thing you need to change is your exit condition in the do while loop. You don't want to exit when you guess the letter, but when the String stop is entered. All you need to do now is slightly modify your loop and the assignment is done.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  16. #16
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    i know :( but i dont know how to implement it

    could you explain more please

  17. #17
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I don't know what else to explain. The requirements were well stated, and if you have written the code above by yourself, you should know exactely what to do. And generally, begging for code/homework is frowned upon.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  18. #18
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    I don't know what else to explain. The requirements were well stated, and if you have written the code above by yourself, you should know exactely what to do. And generally, begging for code/homework is frowned upon.
    :confused:

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Do one thing at a time.
    How do you detect if the user entered the word "stop".
    Look at the String class for a method.

  20. #20
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    i cant understand what you're meaning guys .. am tring for 5 hours to solve a silly excercise .. thats why am saying i need more explaination

Page 1 of 2 12 LastLast

Similar Threads

  1. A new thing arrives...
    By ewomack in forum Introductions
    Replies: 3
    Last Post: 10-09-2009, 09:16 PM
  2. [SOLVED] Simple Trig Thing
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 05-16-2009, 06:45 AM
  3. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  4. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  5. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 AM

Posting Permissions

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