13 Replies - 610 Views - Last Post: 14 May 2011 - 07:53 AM Rate Topic: -----

#1 red_piscean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 11

to check the product of two nos. is correct or not

Posted 14 May 2011 - 04:41 AM

user must enter the two numbers and its product and the program shud chek if its correct. it will prompt for the answer untill user hits 'quit' and then displays it.
class Product
{
    public static void main( String args[])throws IOException
     {

	 System.out.println(" This program checks the product of two numbers.");
	 System.out.println(" enter first number");

	 InputStreamReader I = new InputStreamReader(System.in);
	 BufferedReader br = new BufferedReader(I);

	 String s;
 	 s= br.readLine();
	 int a= Integer.parseInt(s);

	 System.out.println(" enter second number");
	 s= br.readLine();
 	 int b= Integer.parseInt(s);

	int res=a*b;

	System.out.println(" what is the product of " +a+ " and " +b+ "?");
	s= br.readLine();
	while(s!= "quit" || s!= "QUIT")
	{
	int p= Integer.parseInt(s);

	if(p==res)
	System.out.println(" Correct!");
	break;			

	else
	System.out.println(" NO! try again...");
	}
	
	System.out.println(" Product of " + a + " and " + b +" is " + res);
    }
}




the error is, i want my code to exit from the do loop after if is satisfied. and also, antering quit throws exceptions. cant understand.

Is This A Good Question/Topic? 0
  • +

Replies To: to check the product of two nos. is correct or not

#2 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 04:52 AM

Why don't you copy paste the full exception text so we can explain it to you, plus without it we just have to guess ;)
Was This Post Helpful? 0
  • +
  • -

#3 red_piscean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 11

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 04:55 AM

View PostRenagado, on 14 May 2011 - 04:52 AM, said:

Why don't you copy paste the full exception text so we can explain it to you, plus without it we just have to guess ;)



Exception in thread "main" java.lang.NumberFormatException: For input string: "q
uit"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Product.main(Product.java:27)


nowww?????
Was This Post Helpful? 0
  • +
  • -

#4 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 04:59 AM

You're trying to cast a string to a number... that won't work. Also note that in your while loop you never read the input again.

	s= br.readLine();
	while(s!= "quit" || s!= "QUIT")
	{
	int p= Integer.parseInt(s);

	if(p==res)//you forgot brackets here
        {
	System.out.println(" Correct!");
	break;			
        }
	else
	System.out.println(" NO! try again...");

        s= br.readLine();//read input again, if its quit it will break out of the loop
	}//while loop ends here
	
	System.out.println(" Product of " + a + " and " + b +" is " + res);
    }
}


Note that this is still a bit dangerous, if you type something else then a integer or quit, you'll get the exception again. I'll leave it up to you to fix that if needed.

This post has been edited by Renagado: 14 May 2011 - 05:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 red_piscean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 11

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 05:12 AM

it worked...i mean the logic. i replaced while with do while but m still getting the same exception when i enter quit! :( :'( :( :/
Was This Post Helpful? 0
  • +
  • -

#6 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 05:14 AM

Ok, post your new code then.
Was This Post Helpful? 0
  • +
  • -

#7 red_piscean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 11

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 05:15 AM


class Product
{
    public static void main( String args[])throws IOException
     {
	 String s;
	 System.out.println(" This program checks the product of two numbers.");
	
	 InputStreamReader I = new InputStreamReader(System.in);
	 BufferedReader br = new BufferedReader(I);

	 System.out.println(" enter first number");
 	 s= br.readLine();
	 int a= Integer.parseInt(s);

	 System.out.println(" enter second number");
	 s= br.readLine();
 	 int b= Integer.parseInt(s);

	int res=a*b;

	System.out.println(" what is the product of " +a+ " and " +b+ "?");
	s= br.readLine();
	
do{
	int p= Integer.parseInt(s);

	if(p==res)
	{System.out.println(" Correct!");
	break;}
	else
	System.out.println(" NO! try again...");
	s=br.readLine();
   }
	while(s!="quit");
	System.out.println(" product: "+res);
	
	 }
}


Was This Post Helpful? 0
  • +
  • -

#8 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 05:18 AM

A do loop will always run at least once. So if you enter quit it will try to convert that to a number again. That won't work.
Was This Post Helpful? 0
  • +
  • -

#9 red_piscean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 11

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 05:22 AM

the point is, how do i remove the number format exception??
evn if i replace it with a while loop, entering any string throws the exception.
Was This Post Helpful? 0
  • +
  • -

#10 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 05:31 AM

Am sorry, C# confusion got me again, you can't compare strings in Java using == or !=, you're only checking for object equality. This should work:
	s= br.readLine();
	while(!s.equals("quit") )
	{
	int p= Integer.parseInt(s);

	if(p==res)
        {
	System.out.println(" Correct!");
	break;
        }

	else
	System.out.println(" NO! try again...");
        s= br.readLine();
	}

This post has been edited by Renagado: 14 May 2011 - 05:37 AM

Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 05:44 AM

You can't compare STrings like that

s!="quit"

== and != compare if 2 Objects are the same not if they are equals
you will have to use the equals() method of the String class

(!s.equals("quit"))
Was This Post Helpful? 1
  • +
  • -

#12 red_piscean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 11

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 06:35 AM

O YES!!!!
THAT WORKS IN C LANGUAGE!


YIPPPEEEEEEEEEEEEEEEE!!!
THANKS!
IT WORKED!!!! :) :clap:
Was This Post Helpful? 0
  • +
  • -

#13 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 07:04 AM

Glad to see you're happy about it hehe ;)
Anyway, you realise now what caused the error? I mean the string comparing, not the left out brackets etc.
Was This Post Helpful? 0
  • +
  • -

#14 red_piscean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 11

Re: to check the product of two nos. is correct or not

Posted 14 May 2011 - 07:53 AM

yes! i realised it. c/c++ coding rules always interfere! :P
thanks a lot!! :) :angel:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1