Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36101
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2016-12-12 12:28 -0800 |
| References | (2 earlier) <o2ml4h$j28$1@dont-email.me> <64fe5d3e-7f71-4d26-ad6e-c47136806f08@googlegroups.com> <o2mnbo$rqp$1@dont-email.me> <a7c79903-e047-4c1c-9d60-9260f41c3bed@googlegroups.com> <o2mqki$9c4$1@dont-email.me> |
| Message-ID | <ceb2b427-1479-40ad-9796-ba7d646dfbc4@googlegroups.com> (permalink) |
| Subject | Re: i need help figuring out what is wrong in my code |
| From | Karim omar <karimyousrymohamedomar@gmail.com> |
On Monday, December 12, 2016 at 9:36:29 PM UTC+3, Eric Sosman wrote:
> On 12/12/2016 12:52 PM, Karim omar wrote:
> > [...]
> > ok i got your point , my problem is with the input which saved in the variable reply in my code and executed by the findInLine method but still i don`t know how to solve it , i tried everything i know but still not working mean while the following basic oracle program is working just fine while i am using the same technique
> >
> > import java.util.Scanner;
> > import java.util.Random;
> >
> > public class IfScannerRandomProg {
> >
> > public static void main(String[] args) {
> >
> > Scanner myScanner = new Scanner (System.in);
> > Random myRandom = new Random ();
> >
> > int randomNum;
> >
> > System.out.println("What is your name?");
> >
> > String herName = myScanner.nextLine();
> >
> > System.out.println("hi " + herName +"," + " How are you doing today?");
> >
> > String Answer1= myScanner.nextLine();
> >
> > System.out.println("Great, would you like to know your luck for today " + herName +"?" + "(Y/N)");
> >
> > char Answer2 = myScanner.findInLine(".").charAt(0);
> >
> > if(Answer2 == 'Y' || Answer2 == 'y'){
> > System.out.println("Umm ......");
> > }
> > else{
> > System.out.println("Ok then ,See you tomorrow!");
> > }
> >[...]
>
> Since you're learning, here's an important technique: When you
> have a piece of code X that works and a similar piece Y that doesn't,
> pay attention to the two pieces X and Y differ. They will usually
> differ in many ways, so the trick is to focus on the parts of X and Y
> that seem to be connected with the problem, while ignoring the rest.
> In your case, one piece of code uses a random number generator while
> the other does not -- but since your trouble seems to be with input
> and not with random numbers, that's something to ignore. Since the
> trouble is with input and since both programs get their input through
> a Scanner, study what they do differently with that Scanner:
>
> Your working program creates the Scanner, calls nextLine() twice,
> and then calls findInLine().
>
> Your failing program creates the Scanner, calls nextInt() once,
> and then calls findInLine().
>
> What's the difference? Two nextLine() calls versus one nextInt()
> call. We may well guess that the two versus one isn't the issue (you
> could test that by modifying the working program to skip one of the
> two), so the focus turns to: "How do nextLine() and nextInt() differ?"
>
> Like the X and Y programs, these two methods differ in many ways:
> One returns a String, one returns an int, the circumstances under which
> they'll throw exceptions are different, and so on. But the failure
> doesn't occur on those calls, but on the findInLine() call that comes
> after nextInt() (while findInLine() after nextLine() succeeds). This
> suggests that nextLine() and nextInt() leave the Scanner in different
> states: In one state the findInLine() finds something, while in the
> other it does not. So the question now becomes: "What is the Scanner's
> state after nextLine(), and how does it differ from the state after
> nextInt()?"
>
> Consider that question (my first response had some information
> that might help you), and see where it leads you.
>
> --
> esosman@comcast-dot-net.invalid
> "Nobody ever went broke underestimating the intelligence of the
> American public." -- HLM (paraphrased)
i totally got your point ,it`s because i am using nextInt() in the previous method instead of nextLine() and finInLine() is looking for a char in a string , ok i solved the issue by using finInLine() method before nextInt() call , but what if i still want the program to ask for age and get the int input then ask the yes or no question and get the char input ,how can i do it?
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 08:13 -0800
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 08:15 -0800
Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 11:27 -0500
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 08:54 -0800
Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 12:02 -0500
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 09:06 -0800
Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 12:40 -0500
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 09:52 -0800
Re: i need help figuring out what is wrong in my code Eric Douglas <e.d.programmer@gmail.com> - 2016-12-12 10:21 -0800
Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 13:36 -0500
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 12:28 -0800
Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 16:28 -0500
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 13:39 -0800
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 14:17 -0800
Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 18:08 -0500
Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 09:09 -0800
Re: i need help figuring out what is wrong in my code lewbloch@gmail.com - 2016-12-13 12:38 -0800
Re: i need help figuring out what is wrong in my code Eric Douglas <e.d.programmer@gmail.com> - 2016-12-13 12:49 -0800
Re: i need help figuring out what is wrong in my code lewbloch@gmail.com - 2016-12-14 17:01 -0800
Re: i need help figuring out what is wrong in my code Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-15 10:47 +0000
Re: i need help figuring out what is wrong in my code Roedy Green <inquiry@mindprod.com> - 2016-12-13 16:07 -0800
csiph-web