f



i need help figuring out what is wrong in my code

when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message 

Exception in thread "main" java.lang.NullPointerException
	at Ifexercise1a.main(Ifexercise1a.java:18)

and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.
0
Karim
12/12/2016 4:13:25 PM
comp.lang.java.programmer 52714 articles. 0 followers. Post Follow

20 Replies
64 Views

Similar Articles

[PageSpeed] 39

On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message 
> 
> Exception in thread "main" java.lang.NullPointerException
> 	at Ifexercise1a.main(Ifexercise1a.java:18)
> 
> and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.

the complete code is 

import java.util.Scanner;

public class Ifexercise1a {

	public static void main(String[] args) {
		
		Scanner myScanner = new Scanner(System.in);
		int age;
		double price;
		char reply;
		
		System.out.println("how old are you?");
		age = myScanner.nextInt();
		
		System.out.println("you have a coupon? (Y/N)");
		reply = myScanner.findInLine(".").charAt(0);
		
		if(age <= 12 || age > 65){
			price = 5.25;
		}else{
			price = 9.25;
		}
	    if (reply == 'y' || reply == 'Y'){
	    	price-=2.00;
	    }
		  System.out.println("you will pay $" + price);  
		}
	
	
}
0
Karim
12/12/2016 4:15:10 PM
On 12/12/2016 11:15 AM, Karim omar wrote:
> On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
>> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message
>>
>> Exception in thread "main" java.lang.NullPointerException
>> 	at Ifexercise1a.main(Ifexercise1a.java:18)
>>
>> and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.
>
> the complete code is
>
> import java.util.Scanner;
>
> public class Ifexercise1a {
>
> 	public static void main(String[] args) {
> 		
> 		Scanner myScanner = new Scanner(System.in);
> 		int age;
> 		double price;
> 		char reply;
> 		
> 		System.out.println("how old are you?");
> 		age = myScanner.nextInt();

     This consumes and converts the number (or throws an exception if
the user enters something non-numeric), and stops reading as soon as
it has swallowed the number.  It does not swallow anything after the
number -- in particular, it does not swallow the end-of-line marker.

> 		System.out.println("you have a coupon? (Y/N)");
> 		reply = myScanner.findInLine(".").charAt(0);

     This looks for a match in the current line.  If the number from
the earlier call was the last thing on the line, and the end-of-line
marker has not yet been consumed, then the remainder of the current
line is empty.  The findInLine() method will therefore fail to find
anything, and will return null.  And when you attempt null.charAt(0),
you get a NullPointerException.

-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
0
Eric
12/12/2016 4:27:18 PM
On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message 
> 
> Exception in thread "main" java.lang.NullPointerException
> 	at Ifexercise1a.main(Ifexercise1a.java:18)
> 
> and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.

thanks for your help , so i should use system.out.print instead of println?
0
Karim
12/12/2016 4:54:52 PM
On 12/12/2016 11:54 AM, Karim omar wrote:
> On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
>> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message
>>
>> Exception in thread "main" java.lang.NullPointerException
>> 	at Ifexercise1a.main(Ifexercise1a.java:18)
>>
>> and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.
>
> thanks for your help , so i should use system.out.print instead of println?

     (You posted this as a reply to your own message, so it is not clear
whom you are thanking.  It would have been clearer if you had posted a
reply to that person's message instead -- but on the off-chance that you
are thanking ME, I'll try a further response.)

     1. Your problem is with (choose one):

	[ ]  Input
	[ ]  Output

     2. System.out is an ___choose one___ stream

	[ ]  Input
	[ ]  Output

     3. System.out ___choose one___ involved with your problem.

	[ ]  is
	[ ]  is not

-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
0
Eric
12/12/2016 5:02:24 PM
On Monday, December 12, 2016 at 8:02:39 PM UTC+3, Eric Sosman wrote:
> On 12/12/2016 11:54 AM, Karim omar wrote:
> > On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
> >> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message
> >>
> >> Exception in thread "main" java.lang.NullPointerException
> >> 	at Ifexercise1a.main(Ifexercise1a.java:18)
> >>
> >> and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.
> >
> > thanks for your help , so i should use system.out.print instead of println?
> 
>      (You posted this as a reply to your own message, so it is not clear
> whom you are thanking.  It would have been clearer if you had posted a
> reply to that person's message instead -- but on the off-chance that you
> are thanking ME, I'll try a further response.)
> 
>      1. Your problem is with (choose one):
> 
> 	[ ]  Input
> 	[ ]  Output
> 
>      2. System.out is an ___choose one___ stream
> 
> 	[ ]  Input
> 	[ ]  Output
> 
>      3. System.out ___choose one___ involved with your problem.
> 
> 	[ ]  is
> 	[ ]  is not
> 
> -- 
> esosman@comcast-dot-net.invalid
> "Nobody ever went broke underestimating the intelligence of the
> American public." -- HLM (paraphrased)

well i was thanking you :)

is my problem with 3. is ?
0
Karim
12/12/2016 5:06:04 PM
On Monday, December 12, 2016 at 8:02:39 PM UTC+3, Eric Sosman wrote:
> On 12/12/2016 11:54 AM, Karim omar wrote:
> > On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
> >> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message
> >>
> >> Exception in thread "main" java.lang.NullPointerException
> >> 	at Ifexercise1a.main(Ifexercise1a.java:18)
> >>
> >> and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.
> >
> > thanks for your help , so i should use system.out.print instead of println?
> 
>      (You posted this as a reply to your own message, so it is not clear
> whom you are thanking.  It would have been clearer if you had posted a
> reply to that person's message instead -- but on the off-chance that you
> are thanking ME, I'll try a further response.)
> 
>      1. Your problem is with (choose one):
> 
> 	[ ]  Input
> 	[ ]  Output
> 
>      2. System.out is an ___choose one___ stream
> 
> 	[ ]  Input
> 	[ ]  Output
> 
>      3. System.out ___choose one___ involved with your problem.
> 
> 	[ ]  is
> 	[ ]  is not
> 
> -- 
> esosman@comcast-dot-net.invalid
> "Nobody ever went broke underestimating the intelligence of the
> American public." -- HLM (paraphrased)

and if you can take a look at my code and point out which line or part i did wrong i`d be grateful , i am new to java language and still studying it
0
Karim
12/12/2016 5:09:22 PM
On 12/12/2016 12:06 PM, Karim omar wrote:
> On Monday, December 12, 2016 at 8:02:39 PM UTC+3, Eric Sosman wrote:
>> On 12/12/2016 11:54 AM, Karim omar wrote:
>>> On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
>>>> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message
>>>>
>>>> Exception in thread "main" java.lang.NullPointerException
>>>> 	at Ifexercise1a.main(Ifexercise1a.java:18)
>>>>
>>>> and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.
>>>
>>> thanks for your help , so i should use system.out.print instead of println?
>>
>>      (You posted this as a reply to your own message, so it is not clear
>> whom you are thanking.  It would have been clearer if you had posted a
>> reply to that person's message instead -- but on the off-chance that you
>> are thanking ME, I'll try a further response.)
>>
>>      1. Your problem is with (choose one):
>>
>> 	[ ]  Input
>> 	[ ]  Output
>>
>>      2. System.out is an ___choose one___ stream
>>
>> 	[ ]  Input
>> 	[ ]  Output
>>
>>      3. System.out ___choose one___ involved with your problem.
>>
>> 	[ ]  is
>> 	[ ]  is not
>>
>> --
>> esosman@comcast-dot-net.invalid
>> "Nobody ever went broke underestimating the intelligence of the
>> American public." -- HLM (paraphrased)
>
> well i was thanking you :)
>
> is my problem with 3. is ?

      You are having trouble with a Scanner, right?  A Scanner that
reads from System.in, right?  And you expect to solve this by
doing something different with ... System.out?

     Here's a diagram (it's schematic, conceptual, not literal):

	<======================
	  Input goes THIS way
	Your problem is with THIS

	                          ======================>
	                           Output goes THAT way
	                        Your problem isn't with THAT


-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
0
Eric
12/12/2016 5:40:26 PM
On Monday, December 12, 2016 at 8:40:39 PM UTC+3, Eric Sosman wrote:
> On 12/12/2016 12:06 PM, Karim omar wrote:
> > On Monday, December 12, 2016 at 8:02:39 PM UTC+3, Eric Sosman wrote:
> >> On 12/12/2016 11:54 AM, Karim omar wrote:
> >>> On Monday, December 12, 2016 at 7:13:46 PM UTC+3, Karim omar wrote:
> >>>> when i use the (reply =3D myScanner.findInLine(".").charAt(0);) meth=
od i get the following error message
> >>>>
> >>>> Exception in thread "main" java.lang.NullPointerException
> >>>> 	at Ifexercise1a.main(Ifexercise1a.java:18)
> >>>>
> >>>> and i am sure i wrote it write cause i tried copying and paste it fr=
om another prog which is working fine, i am using eclipse and i don`t know =
if it`s a bug in eclipse or something wrong in the class itself.
> >>>
> >>> thanks for your help , so i should use system.out.print instead of pr=
intln?
> >>
> >>      (You posted this as a reply to your own message, so it is not cle=
ar
> >> whom you are thanking.  It would have been clearer if you had posted a
> >> reply to that person's message instead -- but on the off-chance that y=
ou
> >> are thanking ME, I'll try a further response.)
> >>
> >>      1. Your problem is with (choose one):
> >>
> >> 	[ ]  Input
> >> 	[ ]  Output
> >>
> >>      2. System.out is an ___choose one___ stream
> >>
> >> 	[ ]  Input
> >> 	[ ]  Output
> >>
> >>      3. System.out ___choose one___ involved with your problem.
> >>
> >> 	[ ]  is
> >> 	[ ]  is not
> >>
> >> --
> >> esosman@comcast-dot-net.invalid
> >> "Nobody ever went broke underestimating the intelligence of the
> >> American public." -- HLM (paraphrased)
> >
> > well i was thanking you :)
> >
> > is my problem with 3. is ?
>=20
>       You are having trouble with a Scanner, right?  A Scanner that
> reads from System.in, right?  And you expect to solve this by
> doing something different with ... System.out?
>=20
>      Here's a diagram (it's schematic, conceptual, not literal):
>=20
> 	<=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> 	  Input goes THIS way
> 	Your problem is with THIS
>=20
> 	                          =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D>
> 	                           Output goes THAT way
> 	                        Your problem isn't with THAT
>=20
>=20
> --=20
> esosman@comcast-dot-net.invalid
> "Nobody ever went broke underestimating the intelligence of the
> American public." -- HLM (paraphrased)

ok i got your point , my problem is with the input which saved in the varia=
ble 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 me=
an 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) {
	=09
		Scanner myScanner =3D new Scanner (System.in);
		Random myRandom =3D new Random ();
	=09
		int randomNum;
	=09
		System.out.println("What is your name?");
	=09
		String herName =3D myScanner.nextLine();
	=09
		System.out.println("hi " + herName +"," + " How are you doing today?");
	=09
		String   Answer1=3D myScanner.nextLine();
	=09
		System.out.println("Great, would you like to know your luck for today " +=
 herName +"?" + "(Y/N)");
	=09
		char Answer2 =3D myScanner.findInLine(".").charAt(0);
	=09
		if(Answer2 =3D=3D 'Y' || Answer2 =3D=3D 'y'){
			System.out.println("Umm ......");
		}
		else{
			System.out.println("Ok then ,See you tomorrow!");
		}
		randomNum =3D myRandom.nextInt(10) + 1;
	=09
		switch (randomNum){
		case 1:
			System.out.println("You will have a fancy day with everything on your mi=
nd happening");
			break;
		=09
		case 2:
			System.out.println("Well ,it`s not your day today,sleeping is your frien=
d today");
			break;
		=09
		case 3:
			System.out.println("Many people will contact you today for something you=
 have and they want");
		break;
	=09
		case 4:
			System.out.println("Your time to kill your enemies");
			break;
		=09
		case 5:
			System.out.println("Chill out kid, everything is coming in place , be pa=
tient");
			break;
		=09
		case 6:
			System.out.println("Take care of the surroundings!");
			break;
		=09
		case 7:
			System.out.println("you will have a lot of fun today");
			break;
		=09
		case 8:
			System.out.println("Your worry too much , but today all your worries wil=
l go away");
			break;
		=09
		case 9:
			System.out.println("Live your day , it might be your last day");
			break;
		=09
		case 10:
			System.out.println("people will laugh at you always , so do your thing a=
nd don`t look at them");
		}
	=09
	=09
	=09
	=09

	}

}
0
Karim
12/12/2016 5:52:46 PM
On Monday, December 12, 2016 at 12:52:59 PM UTC-5, Karim omar wrote:
> 		Scanner myScanner = new Scanner (System.in);
> 		String   Answer1= myScanner.nextLine();
> 		char Answer2 = myScanner.findInLine(".").charAt(0);

What was the last method of myScanner you called before .findInLine()?
0
Eric
12/12/2016 6:21:13 PM
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)
0
Eric
12/12/2016 6:36:19 PM
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 v=
ariable 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 workin=
g 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) {
> > 	=09
> > 		Scanner myScanner =3D new Scanner (System.in);
> > 		Random myRandom =3D new Random ();
> > 	=09
> > 		int randomNum;
> > 	=09
> > 		System.out.println("What is your name?");
> > 	=09
> > 		String herName =3D myScanner.nextLine();
> > 	=09
> > 		System.out.println("hi " + herName +"," + " How are you doing today?"=
);
> > 	=09
> > 		String   Answer1=3D myScanner.nextLine();
> > 	=09
> > 		System.out.println("Great, would you like to know your luck for today=
 " + herName +"?" + "(Y/N)");
> > 	=09
> > 		char Answer2 =3D myScanner.findInLine(".").charAt(0);
> > 	=09
> > 		if(Answer2 =3D=3D 'Y' || Answer2 =3D=3D 'y'){
> > 			System.out.println("Umm ......");
> > 		}
> > 		else{
> > 			System.out.println("Ok then ,See you tomorrow!");
> > 		}
> >[...]
>=20
>      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:
>=20
>      Your working program creates the Scanner, calls nextLine() twice,
> and then calls findInLine().
>=20
>      Your failing program creates the Scanner, calls nextInt() once,
> and then calls findInLine().
>=20
>      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?"
>=20
>      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()?"
>=20
>      Consider that question (my first response had some information
> that might help you), and see where it leads you.
>=20
> --=20
> 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 st=
ring , ok i solved the issue by using finInLine() method before nextInt() c=
all , but what if i still want the program to ask for age and get the int i=
nput then ask the yes or no question and get the char input ,how can i do i=
t?
0
Karim
12/12/2016 8:28:20 PM
On 12/12/2016 3:28 PM, Karim omar wrote:
> [...]
> 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?

     Learn to read the Javadoc for the classes you use.  One of the
good features of the language is that it has built-in mechanisms for
documentation, and strongly encourages that the programmer use them.
(One of the bad features is that the documentation is written by the
programmers and not by professional writers!  This means you'll need
to read the Javadoc with a lot of care, and be alert for small hints.)

     So, we turn to the Javadoc for Scanner and read about findInLine(),
and we learn right away that there are two different methods of that
name, one taking a String argument and the other a Pattern.  You're
using the String one, and the Javadoc says it "behaves in exactly the
same way" as the other one would with a Pattern derived from the String.
Okay, fine, we can guess what's going on: The String version compiles
its argument to a Pattern and calls the Pattern version.  (Or if it
doesn't do exactly that, it does something equivalent.)

     So, we turn to the Javadoc for the Pattern-accepting version of
findInLine(), and we read (emphasis mine)

	"[...] If the pattern is found *before the next line separator,*
	the scanner [matches and returns it]."

and

	"[...] If no such pattern is detected in the input *up to the
	next line separator,* then null is returned [...]"

In other words, findInLine() searches forward from the current position
in the input, but *only within the current line;* it will not advance
to the next line.  If you need to move to a subsequent line, you'll
need to call some other Scanner method to get there; findInLine() won't
do it unaided.

     Let's see: Does Scanner have a method that moves to the next line?
We peruse the Javadoc for a while and we find a method that

	"Advances this scanner past the current line and returns the
	input that was skipped. [...] The position is set to the
	beginning of the next line."

.... and Lo! it turns out to be a method you're already familiar with!
Perhaps if you used that method before findInLine(), things might go
more smoothly.  Care to try it?

-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
0
Eric
12/12/2016 9:28:32 PM
On Tuesday, December 13, 2016 at 12:28:49 AM UTC+3, Eric Sosman wrote:
> On 12/12/2016 3:28 PM, Karim omar wrote:
> > [...]
> > i totally got your point ,it`s because i am using nextInt() in the prev=
ious 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 i=
nt input then ask the yes or no question and get the char input ,how can i =
do it?
>=20
>      Learn to read the Javadoc for the classes you use.  One of the
> good features of the language is that it has built-in mechanisms for
> documentation, and strongly encourages that the programmer use them.
> (One of the bad features is that the documentation is written by the
> programmers and not by professional writers!  This means you'll need
> to read the Javadoc with a lot of care, and be alert for small hints.)
>=20
>      So, we turn to the Javadoc for Scanner and read about findInLine(),
> and we learn right away that there are two different methods of that
> name, one taking a String argument and the other a Pattern.  You're
> using the String one, and the Javadoc says it "behaves in exactly the
> same way" as the other one would with a Pattern derived from the String.
> Okay, fine, we can guess what's going on: The String version compiles
> its argument to a Pattern and calls the Pattern version.  (Or if it
> doesn't do exactly that, it does something equivalent.)
>=20
>      So, we turn to the Javadoc for the Pattern-accepting version of
> findInLine(), and we read (emphasis mine)
>=20
> 	"[...] If the pattern is found *before the next line separator,*
> 	the scanner [matches and returns it]."
>=20
> and
>=20
> 	"[...] If no such pattern is detected in the input *up to the
> 	next line separator,* then null is returned [...]"
>=20
> In other words, findInLine() searches forward from the current position
> in the input, but *only within the current line;* it will not advance
> to the next line.  If you need to move to a subsequent line, you'll
> need to call some other Scanner method to get there; findInLine() won't
> do it unaided.
>=20
>      Let's see: Does Scanner have a method that moves to the next line?
> We peruse the Javadoc for a while and we find a method that
>=20
> 	"Advances this scanner past the current line and returns the
> 	input that was skipped. [...] The position is set to the
> 	beginning of the next line."
>=20
> ... and Lo! it turns out to be a method you're already familiar with!
> Perhaps if you used that method before findInLine(), things might go
> more smoothly.  Care to try it?
>=20
> --=20
> esosman@comcast-dot-net.invalid
> "Nobody ever went broke underestimating the intelligence of the
> American public." -- HLM (paraphrased)

sure i`ll try it tomorrow morning , i am suffering now with the enum method=
 , i am reading (Beginning programming for dummies) and it uses wrong codes=
 ,that`s why when i use the exact coding i fail ... and now the enum , the =
writer didn`t call the enum method before assigning variables to it .

i appreciate your help a lot and allow me to ask more questions in the futu=
re
0
Karim
12/12/2016 9:39:37 PM
On Tuesday, December 13, 2016 at 12:28:49 AM UTC+3, Eric Sosman wrote:
> On 12/12/2016 3:28 PM, Karim omar wrote:
> > [...]
> > i totally got your point ,it`s because i am using nextInt() in the prev=
ious 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 i=
nt input then ask the yes or no question and get the char input ,how can i =
do it?
>=20
>      Learn to read the Javadoc for the classes you use.  One of the
> good features of the language is that it has built-in mechanisms for
> documentation, and strongly encourages that the programmer use them.
> (One of the bad features is that the documentation is written by the
> programmers and not by professional writers!  This means you'll need
> to read the Javadoc with a lot of care, and be alert for small hints.)
>=20
>      So, we turn to the Javadoc for Scanner and read about findInLine(),
> and we learn right away that there are two different methods of that
> name, one taking a String argument and the other a Pattern.  You're
> using the String one, and the Javadoc says it "behaves in exactly the
> same way" as the other one would with a Pattern derived from the String.
> Okay, fine, we can guess what's going on: The String version compiles
> its argument to a Pattern and calls the Pattern version.  (Or if it
> doesn't do exactly that, it does something equivalent.)
>=20
>      So, we turn to the Javadoc for the Pattern-accepting version of
> findInLine(), and we read (emphasis mine)
>=20
> 	"[...] If the pattern is found *before the next line separator,*
> 	the scanner [matches and returns it]."
>=20
> and
>=20
> 	"[...] If no such pattern is detected in the input *up to the
> 	next line separator,* then null is returned [...]"
>=20
> In other words, findInLine() searches forward from the current position
> in the input, but *only within the current line;* it will not advance
> to the next line.  If you need to move to a subsequent line, you'll
> need to call some other Scanner method to get there; findInLine() won't
> do it unaided.
>=20
>      Let's see: Does Scanner have a method that moves to the next line?
> We peruse the Javadoc for a while and we find a method that
>=20
> 	"Advances this scanner past the current line and returns the
> 	input that was skipped. [...] The position is set to the
> 	beginning of the next line."
>=20
> ... and Lo! it turns out to be a method you're already familiar with!
> Perhaps if you used that method before findInLine(), things might go
> more smoothly.  Care to try it?
>=20
> --=20
> esosman@comcast-dot-net.invalid
> "Nobody ever went broke underestimating the intelligence of the
> American public." -- HLM (paraphrased)

damn!! , it`s (Scanner myNewScanner =3D new Scanner(System.in);) that solve=
s it to reset the Scanner , couldn`t sleep before figuring it out , it appe=
rs that i am a little bit slow learner :)
0
Karim
12/12/2016 10:17:22 PM
On 12/12/2016 5:17 PM, Karim omar wrote:
> On Tuesday, December 13, 2016 at 12:28:49 AM UTC+3, Eric Sosman wrote:
>> [...]
>>      Let's see: Does Scanner have a method that moves to the next line?
>> We peruse the Javadoc for a while and we find a method that
>>
>> 	"Advances this scanner past the current line and returns the
>> 	input that was skipped. [...] The position is set to the
>> 	beginning of the next line."
>>
>> ... and Lo! it turns out to be a method you're already familiar with!
>> Perhaps if you used that method before findInLine(), things might go
>> more smoothly.  Care to try it?
>
> damn!! , it`s (Scanner myNewScanner = new Scanner(System.in);) that solves it to reset the Scanner , couldn`t sleep before figuring it out , it appers that i am a little bit slow learner :)

     That's like "My car is out of gas; time to get a new one."
Keep looking.

-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
0
Eric
12/12/2016 11:08:47 PM
Karim omar wrote:
> when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message 
> 
> Exception in thread "main" java.lang.NullPointerException
> 	at Ifexercise1a.main(Ifexercise1a.java:18)
> 
> and i am sure i wrote it write 

However, the evidence says otherwise.

> cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.

It's never a bug in Eclipse.

-- 
Lew
0
lewbloch
12/13/2016 8:38:02 PM
On Tuesday, December 13, 2016 at 3:38:11 PM UTC-5, lewb...@gmail.com wrote:
> > and i am sure i wrote it write 
> 
> However, the evidence says otherwise.
> 
Well he did say wrote it write, not right... kids today.

> > cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.
> 
> It's never a bug in Eclipse.
> 
I would be quite surprised, unless it's somehow a new version of Java on an old version of Eclipse?

I think we made this mystery clear by now, java.util.Scanner only processes one value at a time, the last value, and for findInLine() that value must be a String.
0
Eric
12/13/2016 8:49:12 PM
On Mon, 12 Dec 2016 08:13:25 -0800 (PST), Karim omar
<karimyousrymohamedomar@gmail.com> wrote:

>Exception in thread "main" java.lang.NullPointerException

see
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION
0
Roedy
12/14/2016 12:07:39 AM
On Tuesday, December 13, 2016 at 12:49:27 PM UTC-8, Eric Douglas wrote:
> On Tuesday, December 13, 2016 at 3:38:11 PM UTC-5, lewb...@gmail.com wrot=
e:
> > > and i am sure i wrote it write=20
> >=20
> > However, the evidence says otherwise.
> >=20
> Well he did say wrote it write, not right... kids today.
>=20
> > > cause i tried copying and paste it from another prog which is working=
 fine, i am using eclipse and i don`t know if it`s a bug in eclipse or some=
thing wrong in the class itself.
> >=20
> > It's never a bug in Eclipse.
> >=20
> I would be quite surprised, unless it's somehow a new version of Java on =
an old version of Eclipse?
>=20
> I think we made this mystery clear by now, java.util.Scanner only process=
es one value at a time, the last value, and for findInLine() that value mus=
t be a String.

One of the early lessons in my programming career is that it is pretty much=
 always your own fault. If you ever find yourself blaming the compiler or t=
he IDE, you are probably wrong. This is one of those useful principles that=
 falls into "teach a person to fish".

Likewise, "I wrote it correctly" is one of those things that prevents you f=
rom figuring out where you wrote it incorrectly. If you are chasing down a =
bug, then the evidence is that you did not write it correctly, and thinking=
 otherwise is, technically, stupid.

Another "teach a person to fish" principle.

If you resist these principles, you're in the wrong business.

--=20
Lew
0
lewbloch
12/15/2016 1:01:39 AM
On Wed, 14 Dec 2016 17:01:39 -0800, lewbloch wrote:

> One of the early lessons in my programming career is that it is pretty
> much always your own fault. If you ever find yourself blaming the
> compiler or the IDE, you are probably wrong. This is one of those useful
> principles that falls into "teach a person to fish".
>
Well said.

I've been involved with programming, both professionally and for my own 
purposes, since the late '60s. In all that time I think I've found and 
reported all of three major bugs in vendor or OS code. The only real 
showstopper was in a mainframe database (IDMSX), where the software 
clearly didn't do what the manual said it should. The vendor solution was 
to change that part of the manual.
 
> Likewise, "I wrote it correctly" is one of those things that prevents
> you from figuring out where you wrote it incorrectly. If you are chasing
> down a bug, then the evidence is that you did not write it correctly,
> and thinking otherwise is, technically, stupid.
> 
+1
 

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |
0
Martin
12/15/2016 10:47:03 AM
Reply: