Jump to content

Adding integer variables in Java

DrMacintosh
Go to solution Solved by 79wjd,
2 minutes ago, DrMacintosh said:

I'm literally new at this. Could you show me how the addition operator is set up? 

System.out.println("The sum of 1 and 2 is " + (1+2));

 

Java's string builder will treat integers before a string like integers (and do any math on them if necessary). Any integers after the string will be treated as strings (unless you wrap them in parenthesis so that the addition is processed first). 

 

E.g. 

 

System.out.println("1+2"+7+7) --> "1+277"

System.out.println(7+7+"1+2") --> "1412"

System.out.println("1+2"+(7+7)) -> "1+214"

Hello, so I'm working on some homework and I've been able to complete every task so far, however I am asked to find the sum of two user inputted variables. 

 

This is my code so far:

import java.util.Scanner; 

public class OutputWithVars {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int userNum;
      int userNum2;

      System.out.println("Enter integer:");
      userNum = scnr.nextInt();
      System.out.println("You entered: "+userNum);
      System.out.println(userNum+" squared is "+userNum*userNum);
      System.out.println("And "+userNum+" cubed is "+userNum*userNum*userNum+"!!");
      System.out.println("Enter another integer:");
      userNum2 = scnr.nextInt();
      System.out.println(userNum+" + "+userNum2+" is "\\help me here);
      System.out.println(userNum+" * "+userNum2+" is "+userNum*userNum2);
   }
}

The program outputs correctly:

Quote

Enter integer:

You entered: 4

4 squared is 16

And 4 cubed is 64!!

Enter another integer:

4 + 5 is (this is where the userNum and userNum2 need to add together)

4 * 5 is 20

but I have no idea how to add userNum and userNum2 together. 

 

Thanks for the help!

Laptop: 2019 16" MacBook Pro i7, 512GB, 5300M 4GB, 16GB DDR4 | Phone: iPhone 13 Pro Max 128GB | Wearables: Apple Watch SE | Car: 2007 Ford Taurus SE | CPU: R7 5700X | Mobo: ASRock B450M Pro4 | RAM: 32GB 3200 | GPU: ASRock RX 5700 8GB | Case: Apple PowerMac G5 | OS: Win 11 | Storage: 1TB Crucial P3 NVME SSD, 1TB PNY CS900, & 4TB WD Blue HDD | PSU: Be Quiet! Pure Power 11 600W | Display: LG 27GL83A-B 1440p @ 144Hz, Dell S2719DGF 1440p @144Hz | Cooling: Wraith Prism | Keyboard: G610 Orion Cherry MX Brown | Mouse: G305 | Audio: Audio Technica ATH-M50X & Blue Snowball | Server: 2018 Core i3 Mac mini, 128GB SSD, Intel UHD 630, 16GB DDR4 | Storage: OWC Mercury Elite Pro Quad (6TB WD Blue HDD, 12TB Seagate Barracuda, 1TB Crucial SSD, 2TB Seagate Barracuda HDD)
Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, DrMacintosh said:

Hello, so I'm working on some homework and I've been able to complete every task so far, however I am asked to find the sum of two user inputted variables. 

 

This is my code so far:


import java.util.Scanner; 

public class OutputWithVars {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int userNum;
      int userNum2;

      System.out.println("Enter integer:");
      userNum = scnr.nextInt();
      System.out.println("You entered: "+userNum);
      System.out.println(userNum+" squared is "+userNum*userNum);
      System.out.println("And "+userNum+" cubed is "+userNum*userNum*userNum+"!!");
      System.out.println("Enter another integer:");
      userNum2 = scnr.nextInt();
      System.out.println(userNum+" + "+userNum2+" is "\\help me here);
      System.out.println(userNum+" * "+userNum2+" is "+userNum*userNum2);
   }
}

The program outputs correctly:

but I have no idea how to add userNum and userNum2 together. 

 

Thanks for the help!

Use the standard addition operator and wrap the addition in parenthesis.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Add parentheses around the addition.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, 79wjd said:

Use the standard addition operator and wrap the addition in parenthesis.

I'm literally new at this. Could you show me how the addition operator is set up? 

Laptop: 2019 16" MacBook Pro i7, 512GB, 5300M 4GB, 16GB DDR4 | Phone: iPhone 13 Pro Max 128GB | Wearables: Apple Watch SE | Car: 2007 Ford Taurus SE | CPU: R7 5700X | Mobo: ASRock B450M Pro4 | RAM: 32GB 3200 | GPU: ASRock RX 5700 8GB | Case: Apple PowerMac G5 | OS: Win 11 | Storage: 1TB Crucial P3 NVME SSD, 1TB PNY CS900, & 4TB WD Blue HDD | PSU: Be Quiet! Pure Power 11 600W | Display: LG 27GL83A-B 1440p @ 144Hz, Dell S2719DGF 1440p @144Hz | Cooling: Wraith Prism | Keyboard: G610 Orion Cherry MX Brown | Mouse: G305 | Audio: Audio Technica ATH-M50X & Blue Snowball | Server: 2018 Core i3 Mac mini, 128GB SSD, Intel UHD 630, 16GB DDR4 | Storage: OWC Mercury Elite Pro Quad (6TB WD Blue HDD, 12TB Seagate Barracuda, 1TB Crucial SSD, 2TB Seagate Barracuda HDD)
Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, DrMacintosh said:

I'm literally new at this. Could you show me how the addition operator is set up? 

System.out.println("The sum of 1 and 2 is " + (1+2));

 

Java's string builder will treat integers before a string like integers (and do any math on them if necessary). Any integers after the string will be treated as strings (unless you wrap them in parenthesis so that the addition is processed first). 

 

E.g. 

 

System.out.println("1+2"+7+7) --> "1+277"

System.out.println(7+7+"1+2") --> "1412"

System.out.println("1+2"+(7+7)) -> "1+214"

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, 79wjd said:

System.out.println("The sum of 1 and 2 is " + (1+2));

Genius, it works flawlessly. 

import java.util.Scanner; 

public class OutputWithVars {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int userNum;
      int userNum2;

      System.out.println("Enter integer:");
      userNum = scnr.nextInt();
      System.out.println("You entered: "+userNum);
      System.out.println(userNum+" squared is "+userNum*userNum);
      System.out.println("And "+userNum+" cubed is "+userNum*userNum*userNum+"!!");
      System.out.println("Enter another integer:");
      userNum2 = scnr.nextInt();
      System.out.println(userNum+" + "+userNum2+" is " +(userNum+userNum2));
      System.out.println(userNum+" * "+userNum2+" is "+userNum*userNum2);
   }
}

 

Laptop: 2019 16" MacBook Pro i7, 512GB, 5300M 4GB, 16GB DDR4 | Phone: iPhone 13 Pro Max 128GB | Wearables: Apple Watch SE | Car: 2007 Ford Taurus SE | CPU: R7 5700X | Mobo: ASRock B450M Pro4 | RAM: 32GB 3200 | GPU: ASRock RX 5700 8GB | Case: Apple PowerMac G5 | OS: Win 11 | Storage: 1TB Crucial P3 NVME SSD, 1TB PNY CS900, & 4TB WD Blue HDD | PSU: Be Quiet! Pure Power 11 600W | Display: LG 27GL83A-B 1440p @ 144Hz, Dell S2719DGF 1440p @144Hz | Cooling: Wraith Prism | Keyboard: G610 Orion Cherry MX Brown | Mouse: G305 | Audio: Audio Technica ATH-M50X & Blue Snowball | Server: 2018 Core i3 Mac mini, 128GB SSD, Intel UHD 630, 16GB DDR4 | Storage: OWC Mercury Elite Pro Quad (6TB WD Blue HDD, 12TB Seagate Barracuda, 1TB Crucial SSD, 2TB Seagate Barracuda HDD)
Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, Spirit`yL said:

Neat bro

Thanks 

Laptop: 2019 16" MacBook Pro i7, 512GB, 5300M 4GB, 16GB DDR4 | Phone: iPhone 13 Pro Max 128GB | Wearables: Apple Watch SE | Car: 2007 Ford Taurus SE | CPU: R7 5700X | Mobo: ASRock B450M Pro4 | RAM: 32GB 3200 | GPU: ASRock RX 5700 8GB | Case: Apple PowerMac G5 | OS: Win 11 | Storage: 1TB Crucial P3 NVME SSD, 1TB PNY CS900, & 4TB WD Blue HDD | PSU: Be Quiet! Pure Power 11 600W | Display: LG 27GL83A-B 1440p @ 144Hz, Dell S2719DGF 1440p @144Hz | Cooling: Wraith Prism | Keyboard: G610 Orion Cherry MX Brown | Mouse: G305 | Audio: Audio Technica ATH-M50X & Blue Snowball | Server: 2018 Core i3 Mac mini, 128GB SSD, Intel UHD 630, 16GB DDR4 | Storage: OWC Mercury Elite Pro Quad (6TB WD Blue HDD, 12TB Seagate Barracuda, 1TB Crucial SSD, 2TB Seagate Barracuda HDD)
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×