Problem calling method from another class - java

My program only runs "correctly" if i add the three Scanners in main but if i delete them, i get "cannot resolve symbol fnum, snum and total" error. How can i call this method from the CalculatorClass, from main?
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner nameInput = new Scanner(System.in);
NameInputClass nameInputClassObject = new NameInputClass();
System.out.println("Hello! Please enter your name");
String name = nameInput.nextLine();
nameInputClassObject.nameInput(name);
Scanner input1 = new Scanner(System.in);
int fnum = input1.nextInt();
Scanner input2 = new Scanner(System.in);
int snum = input1.nextInt();
Scanner input3 = new Scanner(System.in);
int total = input1.nextInt();
CalculatorClass calculatorClassObject = new CalculatorClass();
calculatorClassObject.calcInput(snum,fnum,total);
public class NameInputClass {
public static void nameInput(String name){
System.out.println("Hello "+name);}}
import java.util.Scanner;
public class CalculatorClass {
public static void calcInput(int fnum, int snum, int total){
System.out.println("Give first number");
Scanner firstNumber = new Scanner(System.in);
fnum = firstNumber.nextInt();
System.out.println("Give second number");
Scanner secondNumber = new Scanner(System.in);
snum = firstNumber.nextInt();
total = fnum + snum;
System.out.println("your total = " +total);
}
}

You create an object of your Main. Main main = new Main();
But I suggest you should separate all your objects and functions properly. The proper way of creating classes and objects.
Then call it all in your main.

Related

How to get and print string in java

I can get and print the integer value in java but I am confuse how to get and print string. Can someone help
package hello;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int integer;
System.out.println("Please Enter Integer");
Scanner sc = new Scanner(System.in);
integer = sc.nextInt();
sc.close();
System.out.println("you entered : " +integer);
}
}
Program output
Please Enter Integer
5
you entered : 5
I am stuck in this program. I don't understand how to get string and print on screen
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
int name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextInt();
sc.close();
System.out.println("Your name"+name);
}
}
You need to change your type value name from int to String. And replace sc.nextInt() by sc.nextLine() or sc.next().
Example
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name " + name);
}
Use sc.nextLine() for reading string inputs
or
sc.next() (But this will read only a word before it encounters a space)
You can also use InputStreamReader for this purpose
eg.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in()));
String input = br.readLine();
name = sc.nextInt(); doesn't work for strings, only for integers, you should use sc.nextline instead.
And also you have to change int name to String name, due to other type of variable.
Your code should look like this:
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}
change int name to string name and use sc.nextLine()
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
String name;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();
sc.close();
System.out.println("Your name"+name);
}
}

How do i add a number to another class formula to get answer?

public class maathclass {
public void someNumbers() {
int z = 4;
System.out.println(z);
}
}
//Trying to take variable z to add with answer from maathclass.java
import java.util.Scanner;
public class input {
public static void main (String args[]) {
double fnum, snum, answer;
Scanner userInput= new Scanner(System.in);
maathclass mathObject = new maathclass();
mathObject.someNumbers();
System.out.println("Enter first number:");
fnum = userInput.nextDouble();
System.out.println("Enter second number:");
snum = userInput.nextDouble();
// i want to add the variable z to the answer
answer = fnum + snum;
System.out.println("Answer:" + answer);
}
}
Well you can do this
maathclass.java
public class maathclass {
private int z = 4
public void setZ(int z) {
this.z = z;
}
public int getZ(){
return z;
}
}
input.java
import java.util.Scanner;
public class input {
public static void main (String args[]) {
double fnum, snum, answer;
Scanner userInput= new Scanner(System.in);
maathclass mathObject = new maathclass();
mathObject.setZ(4);
System.out.println("Enter first number:");
fnum = userInput.nextDouble();
System.out.println("Enter second number:");
snum = userInput.nextDouble();
// i want to add the variable z to the answer
answer = fnum + snum + maathclass.getZ();
System.out.println("Answer:" + answer);
}
}
or else if you desire to set the value of z from maathclass then just initialize it with the value in maathclass constructor.
Hopes this helps.

I want to make my java program executable like .exe?

This is my java code...I want to make it double click run forever after compiling once..!
Please Guide me How can I make this .. ?
import java.util.Scanner;
public class Functions {
public static int addnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("\nMethod Call - Addition");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a+b);
}
public static int subnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Subtraction");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a-b);
}
public static int mulnumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Multiply");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a*b);
}
public static int quonumbers(){
Scanner input = new Scanner(System.in);
int a,b;
System.out.println("Method Call - Quotient");
System.out.print("\nEnter a = ");
a=input.nextInt();
System.out.print("Enter b = ");
b=input.nextInt();
return(a/b);
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n;
System.out.println("**Calcultor**\n\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Quotient");
System.out.print("\nEnter no : ");
n=input.nextInt();
if(n==1) {
System.out.println("\nResult = "+addnumbers());
}
if(n==2) {
System.out.println("\nResult = "+subnumbers());
}
if(n==3) {
System.out.println("\nResult = "+mulnumbers());
}
if(n==4) {
System.out.println("\nResult = "+quonumbers());
}
System.out.println("\nControl exit!");
}
}
Tell me any way to do this....
Launch4j
JSmooth
probably a lot more.
These are programs that wrap the Jar file and dependencies to create a native executable for Windows.

Trying to invoke a method called addAccount for a bank application but it keeps giving me .class is expected error

This has been giving me error when I try to invoke the addAccount method saying .class is expected on the line where I try to invoke it.
I am trying to do an assignment whereby I am supposed to create a method called addAccount that accepts two parameters; accountName and accountBalance and put the parameters into an accountArray
import java.util.*;
public class bank
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if(choice == 1)
{
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
public static void displayMenu()
{
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo,double accountBal)
{
}
}
You are missing a closing curly bracket before the definition of the displayMenu method. The following version is syntactically correct:
public class Bank {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if (choice == 1) {
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
}
public static void displayMenu() {
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo, double accountBal) {
}
}
Do not specify parameter type while calling a method.
Here is an error:
case 1 : addAccount(int accountNo,double accountBal);
It must be:
case 1 : addAccount(accountNo,accountBal); break;
you have declared it static so you need to add the class name before the call
bank.addAccount(int accountNo,double accountBal);
and instead of the types decleration you have to call it with parameters.
bank.addAccount(accountNo,accountBal);

Java .class is expected [duplicate]

This has been giving me error when I try to invoke the addAccount method saying .class is expected on the line where I try to invoke it.
I am trying to do an assignment whereby I am supposed to create a method called addAccount that accepts two parameters; accountName and accountBalance and put the parameters into an accountArray
import java.util.*;
public class bank
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if(choice == 1)
{
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
public static void displayMenu()
{
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo,double accountBal)
{
}
}
You are missing a closing curly bracket before the definition of the displayMenu method. The following version is syntactically correct:
public class Bank {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
int accountNo = 0;
double accountBal = 0;
int[] accountNoArray = new int[20];
int[] accountBalArray = new int[20];
displayMenu();
System.out.print("Please Enter Your Choice: ");
choice = sc.nextInt();
if (choice == 1) {
System.out.print("Please Enter NRIC number: ");
accountNo = sc.nextInt();
System.out.print("Please Enter Account Balance: ");
accountBal = sc.nextInt();
}
}
public static void displayMenu() {
System.out.println("Menu");
System.out.println("1. Add an account");
System.out.println("2.Search an account with the given account number");
System.out.println("3.Display accounts below the given balance");
System.out.println("4.Exit");
}
public static void addAccount(int accountNo, double accountBal) {
}
}
Do not specify parameter type while calling a method.
Here is an error:
case 1 : addAccount(int accountNo,double accountBal);
It must be:
case 1 : addAccount(accountNo,accountBal); break;
you have declared it static so you need to add the class name before the call
bank.addAccount(int accountNo,double accountBal);
and instead of the types decleration you have to call it with parameters.
bank.addAccount(accountNo,accountBal);

Categories