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.
Related
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.
Here the input for string is " hi how are you" but I am getting output as "hi" alone. Do let me know where is the error. I have tried out with nextLine() also.
import java.util.Scanner;
public class Stringintalter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter the num");
int a = sc.nextInt();
System.out.println("Enter the Double");
double b = sc.nextDouble();
System.out.println("Enter the string");
String c = sc.next(); //tried out with nextLine() also.
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
This is basic error only. I browsed a lot still I am not able to sort out the error.
everytime you do
int a = sc.nextInt();
double b = sc.nextDouble();
your scanner is not reading the newLine token, so you should do something like:
System.out.println("enter the num");
int a = sc.nextInt();
sc.next();
System.out.println("Enter the Double");
double b = sc.nextDouble();
sc.next();
System.out.println("Enter the string");
String c = sc.next(); //tried out with nextLine() also.
This code worked out for me.
import java.util.Scanner;
public class Stringintalter {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the num");
int a = sc.nextInt();
System.out.println("Enter the Double");
double b = sc.nextDouble();
System.out.println("Enter the string");
sc.nextLine();
String c = sc.nextLine();
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
Maybe this code will help you.
Scanner u = new Scanner(System.in);
Scanner sc=new Scanner(System.in);
System.out.println("enter the num");
int a = sc.nextInt();
System.out.println("Enter the Double");
double b = sc.nextDouble();
System.out.println(b);
System.out.println(a);
String c;
c = u.nextLine();
System.out.println(c);
How do I change this while(true) into a do while so when the user enters a number they will be given back details but if they enter * the system will close.
while (true){
System.out.print("Enter number: ");
int option = keyboard.nextInt();
out.writeInt(option);
char option;
do {
System.out.print("Enter number: ");
option = keyboard.nextChar();
out.writeChar(option);
} while (option != '*')
You may want to use nextLine() or next() to receive the input and parse them accordingly:
do{
System.out.print("Enter number: ");
String str = keyboard.nextLine();
int option = 0;
if(str.matches"[0-9]+"){
option = Integer.parseInt(str);
System.out.println(option);
}
else if(str.equals("*"))
System.exit(0); //or use break; if you want to exit the loop
}while(whatever); //whatever == true
If you change it to allow the user to input a String and then convert to int if possible, you can catch any errors and break out if the user enters a '*' character:
import java.util.*;
class MainInput{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int option;
String input;
do{
System.out.print("Enter number: ");
input = keyboard.nextLine();
try{
option = Integer.parseInt(input);
System.out.println("You entered the value: " + option);
}
catch(Exception ex) {
if (!input.equals("*")){
System.err.println("Invalid input, please enter numbers only.");
}
}
}while(!input.equals("*"));
}
}
If you use an exit value like (-1), you can continue to process input with nextInt() and becomes even easier. You can do this with a simple do/while:
import java.util.*;
class MainInput{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int option = 0;
do{
System.out.print("Enter number: ");
option = keyboard.nextInt();
System.out.println("You entered the value: " + option);
}while(option != -1);
}
}
do{
System.out.print("Enter number: ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
}while(!str.equals("*"))
edit: if you want to play with numbers as integer or double. Just let me know and I'll add casted version as your needs.
int number;
do{
System.out.print("Enter number: ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
number = Integer.parseInt(str);
}while(!str.equals("*"))
now you have a integer number.
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);
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);