Someone help me with this

0 Kumaran M · April 1, 2015
Hi, I just started learning Bucky's Java tutorials and I was making a small calculator kinda program.


Everything seems to be working except the subtraction part. I am not sure y.


I was thinking if someone could help me out with this.


I have pasted the code below and I unbolded it.


import java.util.Scanner;

public class Main {

public static void main(String[] args) {
 
byte fnum, snum, symbol ;

Scanner value = new Scanner (System.in);

System.out.println("Please enter the first number:");
fnum = value.nextByte();

System.out.println("Please enter the second number:");
snum = value.nextByte();

System.out.println("\nPlease select what you want to do with the numbers you have provided.");

System.out.println("\nPress '1' for Addition");
System.out.println("Press '2' for Subraction");
System.out.println("Press '3' for Multiplication");
System.out.println("Press '4' for Division");
System.out.println("Press '5' for Modulus");
symbol = value.nextByte();

if (symbol == 1){

System.out.println("Total = " + fnum + snum);
}

    if (symbol == 3){

System.out.println("Total = " + fnum * snum);
}
    
    if (symbol == 4){

System.out.println("Total = " + fnum / snum);
}
    
    if (symbol == 5){

System.out.println("Total = " + fnum % snum);
}
    
    if (symbol == 2){

  System.out.println("Total = " + fnum - snum);
  }
      
    
}

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Kumaran M · April 6, 2015
I finally understood the concept.


Thank you  steven kitchener, Ige Mayokun and Sumanpal Singh.
0 Kumaran M · April 6, 2015
Okay here is another thing which I needed some clarification.

public class Main {
public static void main(String[] args) {

int firstarray [] [] = {{1,2,3,4,5,6,7,8,9,10},{9,8,7,6,5,4,3,2,1,}};
int secondarray [] []= {{1,2,3,4,5,6,7,8,9,10},{32,56,87,55,55,66,44,55,6545,45454}, {9,8,7,6,5,4,3,2,1,}};

System.out.println();
display(firstarray);
System.out.println();
display(secondarray);

}

public static void display (int x[][]) {
for(int row=0; row<x.length; row++){
  for (int column = 0; column<x
0 Sumanpal Singh · April 2, 2015

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

byte fnum, snum, symbol ;

Scanner value = new Scanner (System.in);

System.out.println("Please enter the first number:");
fnum = value.nextByte();

System.out.println("Please enter the second number:");
snum = value.nextByte();

System.out.println("\nPlease select what you want to do with the numbers you have provided.");

System.out.println("\nPress '1' for Addition");
System.out.println("Press '2' for Subraction");
System.out.println("Press '3' for Multiplication");
System.out.println("Press '4' for Division");
System.out.println("Press '5' for Modulus");
symbol = value.nextByte();

if (symbol == 1){

System.out.println("Total = " + fnum + snum);
}

if (symbol == 3){

System.out.println("Total = " + fnum * snum);
}

if (symbol == 4){

System.out.println("Total = " + fnum / snum);
}

if (symbol == 5){

System.out.println("Total = " + fnum % snum);
}

if (symbol == 2){

System.out.println("Total = " + (fnum - snum));
}


}

}

Try This
works, but don't know why
0 Ige Mayokun · April 2, 2015
Just try and put all your operation inside a bracket..(fnum - snum), (fnum + snum) etc
0 Kumaran M · April 2, 2015
Looks like the brackets helped fix the problem, but why is it only an issue with the minus?
0 steven kitchener · April 2, 2015
Hi Kumaran,

The reason your calculations arent working is because of the way that you are working with Strings. 

When you use the line:

System.out.println("Total = " + fnum + snum)      which when values are inputted for fnum and snum will look like this:

System.out.println("Total = " + 5 + 4)                     you expect an outputted String to look like this "Total = 9"

What you will get however is "Total = 54". This is because System.out.println() prints out strings and it uses the "+" symbol to concatenate (join together) two strings. So in reality what System.out.println("Total = " + 5 + 4)  looks like to Java is 
System.out.prinln("Total = "+"5+"4"), which instead of adding them together will just append them to the end of the string.

The correct way to use math inside of the println statement is to use brackets. Java will see the brackets first and will do the math inside them before appending the result to the end of the string. 

So what you want to write is             System.out.println("Total = " + (fnum + snum));           This will give you the desired result that you want. You should always use brackets when doing math inside of this statement.

Hope this helps,

Steven
  • 1

Java / Android Development

106,965 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator