Athena28
Athena28

Reputation:

Code for Simple Arithmetic

I am new to programming, first year college of BSIT and we were tasked to code a SimpleArithmetic where it will ask your name first and after that you will be asked to enter the first and the second integer.

After giving what is asked it must show "Hello (the name that was entered)" then what follows next is the sum, difference, product and the mod of the two integers and lastly it will show "Thank You!".

I tried a lot of codes but I will not run, so can someone help me? I would appreciate it really because I really want to learn how would that happen.

This was my code

public class SimpleArithmetic{

    public static void main(String[] args){

    //1: Declare name as symbol
    //2: num 1, num 2, sum, difference, product, mod to 0;

    System.out.print("Enter your name: ");
    name = in.next();    // <---- HERE
    System.out.printf("\nEnter first integer: ");
    System.out.printf("\nEnter second integer: ");
    System.out.printf("\nnum 1 + num 2");
    System.out.printf("\nnum 1 - num 2");
    System.out.printf("\nnum 1 * num 2");
    System.out.printf("\nnum 1 % num 2");
    System.out.print("Hello \n + name");
    System.out.println("num 1" + "+" + "is" + "sum");
    System.out.println("num 1" + "-" + "is" + "difference");
    System.out.println("num 1" + "*" + "is" + "product");
    System.out.println("num 1" + "%" + "is" + "mod");
    System.out.print("Thank You!");
    }

}

The bold one was the error when I tried to compile the java file

Upvotes: 0

Views: 133

Answers (5)

Anand saga
Anand saga

Reputation: 1503

try {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = in.nextLine(); // <---- HERE
        System.out.printf("\nEnter first integer: ");
        int nnum1=Integer.parseInt(in.nextLine());
        System.out.printf("\nEnter second integer: ");
        int nnum2=Integer.parseInt(in.nextLine());
        System.out.println("Hello \n" + name);
        System.out.println("num 1" + "+" + "is " + (nnum1 + nnum2));
        System.out.println("num 1" + "-" + "is " + (nnum1 - nnum2));
        System.out.println("num 1" + "*" + "is " + (nnum1 * nnum2));
        System.out.println("num 1" + "%" + "is " + (nnum1 % nnum2));
        System.out.print("Thank You!");
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        System.out.println("Please enter valid number");
        e.printStackTrace();
    }

Upvotes: 0

Rajesh
Rajesh

Reputation: 2155

Use Scanner and try catch block:

There is no declaration of variables which is listed in assignment comments.

You should have done something similar to this:

import java.util.Scanner;

public class SimpleArithmetic {

    int num1 = 0, num2 = 0, sum = 0, difference = 0, product = 0, mod = 0;
    String name = null;
    Scanner in = null;

    public static void main(String[] args) {
        SimpleArithmetic sa = new SimpleArithmetic();

        try {
            sa.doSimpleArithmetic();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void doSimpleArithmetic() throws Exception {
        in = new Scanner(System.in);
        System.out.print("Enter your name: ");
        name = in.nextLine();

        System.out.printf("\nEnter first integer: ");
        num1 = Integer.parseInt(in.nextLine());
        System.out.printf("\nEnter second integer: ");
        num2 = Integer.parseInt(in.nextLine());

        sum = num1 + num2;
        difference = num1 - num2;
        product = num1 * num2;
        mod = num1 / num2;

        System.out.println("\n" + "Hello " + name + "\n");

        System.out.println(num1 + " + " + num2 + " is :" + sum);
        System.out.println(num1 + " - " + num2 + " is :" + difference);
        System.out.println(num1 + " * " + num2 + " is :" + product);
        System.out.println(num1 + " % " + num2 + " is :" + mod);

        System.out.println("\n" + "Thank You!");
        in.close();
    }
}

Upvotes: 0

Jishnu Prathap
Jishnu Prathap

Reputation: 2033

Remove all the double qoutes on variables.


change System.out.print("Hello \n + name"); to System.out.print("Hello \n + name);

System.out.println("num 1" + "+" + "is" + "sum"); to System.out.println("num 1" + "+" + "is" + sum);

System.out.println("num 1" + "-" + "is" + "difference"); to  `System.out.println("num 1" + "-" + "is" + difference);`

System.out.println("num 1" + "*" + "is" + "product"); to System.out.println("num 1" + "*" + "is" + product);

System.out.println("num 1" + "%" + "is" + "mod"); to `System.out.println("num 1" + "%" + "is" + mod);`

Upvotes: 0

Pavan Kumar K
Pavan Kumar K

Reputation: 1376

The following will work for you...

package com.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SimpleArithmetic{

public static void main(String[] args){
    try{
        //1: Declare name as symbol
        //2: num 1, num 2, sum, difference, product, mod to 0;

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

        System.out.print("Enter your name: ");
        String name = in.readLine();   // <---- HERE
        System.out.printf("\nEnter first integer: ");
        String str1 = in.readLine();
        int number1 = Integer.parseInt(str1);
        System.out.printf("\nEnter second integer: ");
        String str2 = in.readLine();
        int number2 = Integer.parseInt(str2);
        System.out.print("Hello \n "+ name);
        System.out.println("num 1" + "+" + "is" + (number1 + number2));
        System.out.println("num 1" + "-" + "is" + (number1 - number2));
        System.out.println("num 1" + "*" + "is" + (number1 * number2));
        System.out.println("num 1" + "%" + "is" + (number1 % number2));
        System.out.print("Thank You!");
    }catch(Exception e)
    {
        e.printStackTrace();
    }
}

}

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

Use the class Scanner as below to read input:

Scanner scanner = new Scanner(System.in);  // Init scanner
String name = scanner.nextLine();          // Reads a full line
int a = scanner.nextInt();                 // Reads one integer
int b = scanner.nextInt();                 // Reads another integer

Check documentation here if you like to know more about the class Scanner.

Basically Scanner is a useful class to read input from a stream (System.in) in that case. From javadoc

A simple text scanner which can parse primitive types and strings using regular expressions.

Upvotes: 1

Related Questions