NEED help with my Java Basic calculator

Java Console Basic Calculator

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4318 Views - Last Post: 18 January 2010 - 10:22 PM Rate Topic: -----

#1 ace_w1zard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-October 09

NEED help with my Java Basic calculator

Post icon  Posted 17 January 2010 - 09:19 PM

import java.util.Scanner;
class helloworld {
	 public static void main(String args[]){
double fnum,snum,answer,dv;
Scanner x = new Scanner(System.in);
System.out.println("Enter your 1st number you wish to calculate: ");
fnum = x.nextDouble();
System.out.println("Enter your 2nd number you wish to calculate: ");
snum = x.nextDouble();
System.out.println("If you wish to multiply your 2 number click 1");
System.out.println("If you wish to Add your 2 number click 2");
System.out.println("If you wish to divide your 2 number click 3");
System.out.println("If you wish to subtract your 2 number click 4");
System.out.println("If you wish to find the remainder of your 2 number click 5");
 dv = x.nextDouble();
 if (dv == 1){
	 answer = fnum * snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 2){
	 answer = fnum + snum;
	 
 }else{
	 System.out.println("You Entered a invalid Number!!!");
	 
 }
 if (dv == 3){
	 answer = fnum / snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 4){
	 answer = fnum - snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 5){
	 answer = fnum % snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 System.out.println(answer);
	 }

	 
 
	 } 


i need help, i am trying to program a console basic calculator in java, i want the user to be able to input a number the another, and then for the computer to out put the answer, BUT i also want them to choose what they want to be done with said number, so they choose 1 for times, 2 for add, 3 for /, 4 for -, and 5 for %. once they chose there number the system would out put teh answer, but IDK why it aint working! can some one help me, and tell me what is wrong with my code?

Is This A Good Question/Topic? 0
  • +

Replies To: NEED help with my Java Basic calculator

#2 ace_w1zard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-October 09

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 09:31 PM

come on guys i need your help! any body, is this code to hard?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 09:36 PM

This code is horrible. You need to work on improving your spacing and indentation conventions. Also, in the future, please describe your errors. In this instance, they were obvious enough for me to spot. However, for larger programs, it is a pain to weed through 50+ lines of code looking for the error. Thanks for helping us help you! :)

Now, onto your errors. There are two things wrong here. The first is that you have an else statement after each of your if statements (not really an error, but a design and logic flaw). Really, you just need one else statement at the end telling the user that he/she entered an invalid number. Also, you might want to make all of your if statements between the first conditional and the end else statement else-if statements. An else-if statement occurs below another else-if or a standard if statement, and is evaluated if the above conditional is false. So something like:
if(dv == 1){..code..}
else if(dv == 2){..code..}
..more conditionals..
else{..print invalid number..}



Your next problem is that you print answer without guaranteeing its initialization. You should initialize it to 0 at the beginning so it will print.
Was This Post Helpful? 0
  • +
  • -

#4 ace_w1zard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-October 09

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 09:44 PM

View Postmacosxnerd101, on 17 Jan, 2010 - 08:36 PM, said:

This code is horrible. You need to work on improving your spacing and indentation conventions. Also, in the future, please describe your errors. In this instance, they were obvious enough for me to spot. However, for larger programs, it is a pain to weed through 50+ lines of code looking for the error. Thanks for helping us help you! :)

Now, onto your errors. There are two things wrong here. The first is that you have an else statement after each of your if statements (not really an error, but a design and logic flaw). Really, you just need one else statement at the end telling the user that he/she entered an invalid number. Also, you might want to make all of your if statements between the first conditional and the end else statement else-if statements. An else-if statement occurs below another else-if or a standard if statement, and is evaluated if the above conditional is false. So something like:
if(dv == 1){..code..}
else if(dv == 2){..code..}
..more conditionals..
else{..print invalid number..}



Your next problem is that you print answer without guaranteeing its initialization. You should initialize it to 0 at the beginning so it will print.

OMG ty so much for the help, i am SUPER new to java, just started yesterday man, but ty for the help! and ty for the info on the if/else, also is there a if/then statment?
Was This Post Helpful? 0
  • +
  • -

#5 ace_w1zard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-October 09

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 09:57 PM

TY for the help man, here is my new and improved code! and my 1sy offcial fully working calculator i made in java! =)
 import java.util.Scanner;
class helloworld {
	 public static void main(String args[]){
double fnum,snum,answer,dv;
Scanner x = new Scanner(System.in);
System.out.println("Enter your 1st number you wish to calculate: ");
fnum = x.nextDouble();
System.out.println("Enter your 2nd number you wish to calculate: ");
snum = x.nextDouble();
System.out.println("If you wish to multiply your 2 number click 1");
System.out.println("If you wish to Add your 2 number click 2");
System.out.println("If you wish to divide your 2 number click 3");
System.out.println("If you wish to subtract your 2 number click 4");
System.out.println("If you wish to find the remainder of your 2 number click 5");
 dv = x.nextDouble();
if (dv == 1){
	answer = fnum * snum;
	System.out.println(answer);}
else if (dv == 2){
		answer = fnum + snum;
		System.out.println(answer);}
else if (dv == 3){
	answer = fnum / snum;
	System.out.println(answer);}
else if (dv == 4){
	answer = fnum - snum;
	System.out.println(answer);}
else if (dv == 5){
	answer = fnum % snum;
	System.out.println(answer);}
else {System.out.println("Invalid number! Try again!");}
 
	 }
}	
:) ty for help mon
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 10:01 PM

No problem. Just remember that it is ok to indent your code and to leave blank lines. This will make it easier for you to debug. Since you just started yesterday, I'll indent this one gratis if you'll continue to use proper indentation standards. :)

import java.util.Scanner;

class helloworld {
	 public static void main(String args[]){
			double fnum,snum,answer,dv;
			Scanner x = new Scanner(System.in);

			System.out.println("Enter your 1st number you wish to calculate: ");
			fnum = x.nextDouble();

			System.out.println("Enter your 2nd number you wish to calculate: ");
			snum = x.nextDouble();

			System.out.println("If you wish to multiply your 2 number click 1");
			System.out.println("If you wish to Add your 2 number click 2");
			System.out.println("If you wish to divide your 2 number click 3");
			System.out.println("If you wish to subtract your 2 number click 4");
			System.out.println("If you wish to find the remainder of your 2 number click 5");

		   dv = x.nextDouble();

		   if (dv == 1){
			 answer = fnum * snum;
			 System.out.println(answer);
		   }
		   else if (dv == 2){
				  answer = fnum + snum;
				  System.out.println(answer);
		   }
		   else if (dv == 3){
				 answer = fnum / snum;
				 System.out.println(answer);
		   }
		   else if (dv == 4){
			   answer = fnum - snum;
			  System.out.println(answer);
		  }
		 else if (dv == 5){
			 answer = fnum % snum;
			 System.out.println(answer);}
		else {System.out.println("Invalid number! Try again!");}

	 }
}	



There- doesn't that look a little nicer?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 10:02 PM

Not really improved
And your code indentation is horrible
And you cannot compare a double using == because 1.99999 is not equals to 2 neither 4 equals 3.999999
you should never check a double with ==
so for the operation to be performed read an int bot a double
Was This Post Helpful? 0
  • +
  • -

#8 ace_w1zard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-October 09

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 10:05 PM

View Postpbl, on 17 Jan, 2010 - 09:02 PM, said:

Not really improved
And your code indentation is horrible
And you cannot compare a double using == because 1.99999 is not equals to 2 neither 4 equals 3.999999
you should never check a double with ==
so for the operation to be performed read an int bot a double

what do ya mean by " my indention is horrible" and also can you explain the int/double thing again, cause i got a error for it while i was programming it and it kinda said what you said! Teach me...lol :P :D
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 10:26 PM

In terms of your indentation, compare the code you wrote to the code I indented. The aesthetic differences should be obvious.

In terms of the int/double point, you are comparing integer values (1, 2, 3, 4, 5, etc.), not decimal values (3.445, 5.7, etc.), so you should be reading in an int value (use the Scanner nextInt() method) to an int variable instead of working with a double to determine your function. The error you got was probably due to the fact that you were reading in an int, but typed a double. The operands you are performing the mathematical operation on, however, should be treated as real numbers, so you are correct in using the double type here.

This post has been edited by macosxnerd101: 17 January 2010 - 10:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 ace_w1zard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-October 09

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 10:48 PM

View Postmacosxnerd101, on 17 Jan, 2010 - 09:26 PM, said:

In terms of your indentation, compare the code you wrote to the code I indented. The aesthetic differences should be obvious.

In terms of the int/double point, you are comparing integer values (1, 2, 3, 4, 5, etc.), not decimal values (3.445, 5.7, etc.), so you should be reading in an int value (use the Scanner nextInt() method) to an int variable instead of working with a double to determine your function. The error you got was probably due to the fact that you were reading in an int, but typed a double. The operands you are performing the mathematical operation on, however, should be treated as real numbers, so you are correct in using the double type here.

but say if i wanted to make a calculator that can use decimals? what could i do?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 10:58 PM

Right now it can operate on decimals. So if you tell it to add 1.5+3.99, it can do that. However, there are 5 discrete mathematical functions- addition, subtraction, multiplication, division, and modulus. So therefore, you have 5 integer value options corresponding with the functions- 1, 2, 3, 4 and 5. Can you have a blend between addition and multiplication? No. So therefore, you wouldn't accept 1.5 as a valid input to determine what operation the calculator will perform.
Was This Post Helpful? 0
  • +
  • -

#12 0x00hex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 11-January 10

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 11:15 PM

Quote

What do you mean "my indention is horrible"?

I'll try to answer this question as clearly as possible, lets have a look at your code:
import java.util.Scanner;
class helloworld {
	 public static void main(String args[]){
double fnum,snum,answer,dv;
Scanner x = new Scanner(System.in);
System.out.println("Enter your 1st number you wish to calculate: ");
fnum = x.nextDouble();
System.out.println("Enter your 2nd number you wish to calculate: ");
snum = x.nextDouble();
System.out.println("If you wish to multiply your 2 number click 1");
System.out.println("If you wish to Add your 2 number click 2");
System.out.println("If you wish to divide your 2 number click 3");
System.out.println("If you wish to subtract your 2 number click 4");
System.out.println("If you wish to find the remainder of your 2 number click 5");
 dv = x.nextDouble();
 if (dv == 1){
	 answer = fnum * snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 2){
	 answer = fnum + snum;
	 
 }else{
	 System.out.println("You Entered a invalid Number!!!");
	 
 }
 if (dv == 3){
	 answer = fnum / snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 4){
	 answer = fnum - snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 5){
	 answer = fnum % snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 System.out.println(answer);
	 }

	 
 
	 }  


Although to you it may be easy to read, for some one who has never seen your code before it is a nightmare.
You need to use spaces and white space to make your code easy to read (also variable names that are unique and have meaning).

I will show you an example your code 'cleaned' up:
class helloworld {
	
	public static void main(String args[]){
		
		double firstNum,secondNum,answer;
		int operationselection;
		Scanner consoleInput = new Scanner(System.in);

		/* Also, use comments to comment bits of code! 
		 * Get numbers from the user
		 */
		
			System.out.print("Enter your 1st number you wish to calculate: ");
				firstNum = consoleInput.nextDouble();

			System.out.print("\nEnter your 2nd number you wish to calculate: ");
				secondNumber = consoleInput.nextDouble();
	   
	   //Get Operation to perform from user
	   
			System.out.print("If you wish to Multiply your 2 number click 1 \n" +
							 "If you wish to Add your 2 number click 2 \n" + 
							 "If you wish to Divide your 2 number click 3 \n" +
							 "If you wish to Subtract your 2 number click 4 \n" +
							 "If you wish to Find the remainder of your 2 number click 5");
				operationselection = consoleInput.nextInteger();
				
	  // Generate answer based on operation selected (Could use a switch staement instead of if else if.
	  
		   if (operationselection == 1)
			   answer = firstNumber * secondNumber;
		   
		   else if (operationselection == 2)
			   answer = firstNumber + secondNumber;

		   else if (operationselection == 3)
			   answer = firstNumber / secondNumber;
				 
		   else if (operationselection == 4)
			   answer = firstNumber - secondNumber;
			  
		   else if (operationselection == 5)
			   answer = firstNumber % secondNumber;
			 
		   else {
			   System.out.println("Invalid number! Try again!"); 
			   return 0; }
		   
	 // Print the answer
	 
		System.out.println(answer);

	 }
	 
}


all you need to do is indent your code and lay it out so it looks pretty and is readable, here is a link you might find useful as you further your understanding of the java language.

This post has been edited by 0x00hex: 17 January 2010 - 11:59 PM

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,794
  • Joined: 27-December 08

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 11:23 PM

@0x00hex: That first segment of code that you're criticizing is actually the code that I indented for the OP. The below code is the OP's unindented code. I completely agree with you about the variable names, but there isn't anything wrong with my code indentations. You might want to read before you start criticizing next time.

View Postace_w1zard, on 18 Jan, 2010 - 12:19 AM, said:

import java.util.Scanner;
class helloworld {
	 public static void main(String args[]){
double fnum,snum,answer,dv;
Scanner x = new Scanner(System.in);
System.out.println("Enter your 1st number you wish to calculate: ");
fnum = x.nextDouble();
System.out.println("Enter your 2nd number you wish to calculate: ");
snum = x.nextDouble();
System.out.println("If you wish to multiply your 2 number click 1");
System.out.println("If you wish to Add your 2 number click 2");
System.out.println("If you wish to divide your 2 number click 3");
System.out.println("If you wish to subtract your 2 number click 4");
System.out.println("If you wish to find the remainder of your 2 number click 5");
 dv = x.nextDouble();
 if (dv == 1){
	 answer = fnum * snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 2){
	 answer = fnum + snum;
	 
 }else{
	 System.out.println("You Entered a invalid Number!!!");
	 
 }
 if (dv == 3){
	 answer = fnum / snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 4){
	 answer = fnum - snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 5){
	 answer = fnum % snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 System.out.println(answer);
	 }

	 
 
	 } 

Was This Post Helpful? 0
  • +
  • -

#14 ace_w1zard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-October 09

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 11:53 PM

View Post0x00hex, on 17 Jan, 2010 - 10:15 PM, said:

Quote

What do you mean "my indention is horrible"?

I'll try to answer this question as clearly as possible, lets have a look at your code:
mport java.util.Scanner;

class helloworld {
	 public static void main(String args[]){
			double fnum,snum,answer,dv;
			Scanner x = new Scanner(System.in);

			System.out.println("Enter your 1st number you wish to calculate: ");
			fnum = x.nextDouble();

			System.out.println("Enter your 2nd number you wish to calculate: ");
			snum = x.nextDouble();

			System.out.println("If you wish to multiply your 2 number click 1");
			System.out.println("If you wish to Add your 2 number click 2");
			System.out.println("If you wish to divide your 2 number click 3");
			System.out.println("If you wish to subtract your 2 number click 4");
			System.out.println("If you wish to find the remainder of your 2 number click 5");

		   dv = x.nextDouble();

		   if (dv == 1){
			 answer = fnum * snum;
			 System.out.println(answer);
		   }
		   else if (dv == 2){
				  answer = fnum + snum;
				  System.out.println(answer);
		   }
		   else if (dv == 3){
				 answer = fnum / snum;
				 System.out.println(answer);
		   }
		   else if (dv == 4){
			   answer = fnum - snum;
			  System.out.println(answer);
		  }
		 else if (dv == 5){
			 answer = fnum % snum;
			 System.out.println(answer);}
		else {System.out.println("Invalid number! Try again!");}

	 }
}  


Although to you it may be easy to read, for some one who has never seen your code before it is a nightmare.
You need to use spaces and white space to make your code easy to read (also variable names that are unique and have meaning).

I will show you an example your code 'cleaned' up:
class helloworld {
	
	public static void main(String args[]){
		
		double firstNum,secondNum,answer;
		int operationselection;
		Scanner consoleInput = new Scanner(System.in);

		/* Also, use comments to comment bits of code! 
		 * Get numbers from the user
		 */
		
			System.out.print("Enter your 1st number you wish to calculate: ");
				firstNum = consoleInput.nextDouble();

			System.out.print("\nEnter your 2nd number you wish to calculate: ");
				secondNumber = consoleInput.nextDouble();
	   
	   //Get Operation to perform from user
	   
			System.out.print("If you wish to Multiply your 2 number click 1 \n" +
							 "If you wish to Add your 2 number click 2 \n" + 
							 "If you wish to Divide your 2 number click 3 \n" +
							 "If you wish to Subtract your 2 number click 4 \n" +
							 "If you wish to Find the remainder of your 2 number click 5");
				operationselection = consoleInput.nextInteger();
				
	  // Generate answer based on operation selected (Could use a switch staement instead of if else if.
	  
		   if (operationselection == 1)
			   answer = firstNumber * secondNumber;
		   
		   else if (operationselection == 2)
			   answer = firstNumber + secondNumber;

		   else if (operationselection == 3)
			   answer = firstNumber / secondNumber;
				 
		   else if (operationselection == 4)
			   answer = firstNumber - secondNumber;
			  
		   else if (operationselection == 5)
			   answer = firstNumber % secondNumber;
			 
		   else {
			   System.out.println("Invalid number! Try again!"); 
			   return 0; }
		   
	 // Print the answer
	 
		System.out.println(answer);

	 }
	 
}


all you need to do is indent your code and lay it out so it looks pretty and is readable, here is a link you might find useful as you further your understanding of the java language.


wow, ty for the info man, that really helped! =)


View Postmacosxnerd101, on 17 Jan, 2010 - 10:23 PM, said:

@0x00hex: That first segment of code that you're criticizing is actually the code that I indented for the OP. The below code is the OP's unindented code. I completely agree with you about the variable names, but there isn't anything wrong with my code indentations. You might want to read before you start criticizing next time.

View Postace_w1zard, on 18 Jan, 2010 - 12:19 AM, said:

import java.util.Scanner;
class helloworld {
	 public static void main(String args[]){
double fnum,snum,answer,dv;
Scanner x = new Scanner(System.in);
System.out.println("Enter your 1st number you wish to calculate: ");
fnum = x.nextDouble();
System.out.println("Enter your 2nd number you wish to calculate: ");
snum = x.nextDouble();
System.out.println("If you wish to multiply your 2 number click 1");
System.out.println("If you wish to Add your 2 number click 2");
System.out.println("If you wish to divide your 2 number click 3");
System.out.println("If you wish to subtract your 2 number click 4");
System.out.println("If you wish to find the remainder of your 2 number click 5");
 dv = x.nextDouble();
 if (dv == 1){
	 answer = fnum * snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 2){
	 answer = fnum + snum;
	 
 }else{
	 System.out.println("You Entered a invalid Number!!!");
	 
 }
 if (dv == 3){
	 answer = fnum / snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 4){
	 answer = fnum - snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 if (dv == 5){
	 answer = fnum % snum;
	 
 }else { System.out.println("You Entered a invalid Number!");
	 
 }
 System.out.println(answer);
	 }

	 
 
	 } 



lol are you drunk, how the hell was i criticizing you? :v: ...FAIL! lol :P
Was This Post Helpful? 0
  • +
  • -

#15 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: NEED help with my Java Basic calculator

Posted 17 January 2010 - 11:55 PM

View Postace_w1zard, on 18 Jan, 2010 - 01:53 AM, said:

lol are you drunk, how the hell was i criticizing you? :v: ...FAIL! lol :P


macosxnerd's post was directed at 0x00hex, not you. Hence the "@0x00hex"

This post has been edited by erik.price: 17 January 2010 - 11:56 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2