Results 1 to 20 of 29
- 05-29-2010, 07:47 PM #1
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
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
and it comes right but i wont to implement same procedures with words or strings .. please help ..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"); } }
regards
- 05-29-2010, 08:21 PM #2
Look at the API doc for the Scanner class. It has methods to read Strings.with words or strings
- 05-29-2010, 09:25 PM #3
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
i did before creating the thread
- 05-29-2010, 09:28 PM #4
What method(s) did you see that would read Strings vs ints?
Have you changed your program to use them?
- 05-29-2010, 09:41 PM #5
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
y
yes sir
am tired tring
this is what i've done
but there are errorsJava 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" ); } }
its not coming like my wishes
- 05-29-2010, 09:44 PM #6
If there are error messages, please copy and paste them here.there are errors
Please describe.its not coming like my wishes
- 05-29-2010, 09:54 PM #7
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
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
- 05-29-2010, 10:06 PM #8
What is type is the variable guess? Does it have a method: charAt() ?
Scanner.next() returns a String.
- 05-29-2010, 10:44 PM #9
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
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
- 05-29-2010, 10:56 PM #10
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.
- 05-29-2010, 11:08 PM #11
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
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
- 05-29-2010, 11:22 PM #12
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
i've done it
- 05-29-2010, 11:23 PM #13
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-30-2010, 12:06 AM #14
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
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
- 05-30-2010, 12:10 AM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 05-30-2010, 12:14 AM #16
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
i know :( but i dont know how to implement it
could you explain more please
- 05-30-2010, 12:26 AM #17
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 05-30-2010, 12:30 AM #18
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
- 05-30-2010, 12:43 AM #19
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.
- 05-30-2010, 12:45 AM #20
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
A new thing arrives...
By ewomack in forum IntroductionsReplies: 3Last Post: 10-09-2009, 09:16 PM -
[SOLVED] Simple Trig Thing
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 05-16-2009, 06:45 AM -
What did i do wrong on thing method?
By PureAwesomeness in forum New To JavaReplies: 9Last Post: 03-08-2009, 08:37 AM -
Need help with a simple Java thing involving array of objects
By Jeremy8 in forum New To JavaReplies: 5Last Post: 02-25-2009, 07:14 PM -
PLz i really need help on this final thing
By jason27131 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks