I am having a tough time understanding the mistake. The mistake is somewhere in the object making or using. VS code is showing me calIncom cannot be resolved to a typeJava(16777218)
import java.util.Scanner;
class incomtax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int salary;
System.out.println("Give the salary");
salary = sc.nextInt();
calIncom obj = new calIncom();
obj(salary);
}
public void calIncom(int a) {
if (a <= 50000) {
System.out.println("No incom tax");
}
else if (a <= 60000 && a > 50000) {
System.out.println("Your incom tax is: " + a / 10);
} else if (a <= 150000 && a > 60000) {
System.out.println("Your incom tax is: " + a / 20);
} else if (a >= 150000) {
System.out.println("Your incom tax is: " + a / 30);
}
}
}
That's not possible in Java. Methods are not classes.
You have to create an instance of the incomtax class
incomtax obj = new incomtax();
obj.calIncom(salary);
Related
I just tried to get an integer value from the user for a variable in another a method that created differently from main but it gives an error message like this:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:941)
at java.base/java.util.Scanner.next(Scanner.java:1598)
at java.base/java.util.Scanner.nextInt(Scanner.java:2263)
at java.base/java.util.Scanner.nextInt(Scanner.java:2217)
at StoreUsingArrays_20210808043.menu(StoreUsingArrays_20210808043.java:14)
at StoreUsingArrays_20210808043.storeRun(StoreUsingArrays_20210808043.java:57)
at StoreUsingArrays_20210808043.main(StoreUsingArrays_20210808043.java:90)
Code:
import java.util.Arrays;
import java.util.Scanner;
public class StoreUsingArrays_20210808043 {
public static int menu(String[] items,double[] prices,int answer) {
Scanner input = new Scanner(System.in);
for (int i = 1;i <= items.length;i++) {
System.out.println(i+" - for "+items[i-1]+" ("+prices[i-1]+")");
}
System.out.println("0 - to checkout");
System.out.print("Please enter what would you like : ");
answer = input.nextInt();
System.out.println("Your choice was : "+answer);
input.close();
return answer;
}
public static void returnedAmounts(double amount) {
double bill200,bill100,bill50,bill20,bill10,bill5,bill1,coin50,coin25,coin10,coin1;
bill200 = (amount - (amount%200)) / 200;
amount = amount%200;
bill100 = (amount - (amount%100)) / 100;
amount = amount%100;
bill50 = (amount - (amount%50)) / 50;
amount = amount%50;
bill20 = (amount - (amount%20)) / 20;
amount = amount%20;
bill10 = (amount - (amount%10)) / 10;
amount = amount%10;
bill5 = (amount -(amount%5)) / 5;
amount = amount%5;
bill1 = (amount - (amount%1)) / 1;
amount = amount%1;
coin50 = (amount - (amount%0.50)) / 0.50;
amount = amount%0.50;
coin25 = (amount - (amount%0.25)) / 0.25;
amount = amount%0.25;
coin10 = (amount - (amount%0.10)) / 0.10;
amount = amount%0.10;
coin1 = (amount - (amount%0.01)) / 0.01;
double[] returnedNumbers = {bill200,bill100,bill50,bill20,bill10,bill5,bill1,coin50,coin25,coin10,coin1};
double[] returnedValues = {200,100,50,20,10,5,1,0.50,0.25,0.10,0.01};
for (int i = 0;i < returnedNumbers.length;i++) {
if ((returnedNumbers[i] > 0) && (returnedValues[i] > 0)) {
System.out.println((int)returnedNumbers[i]+" - "+returnedValues[i]);
}
}
}
public static void storeRun(String[] item,int[] quantity,double[] price) {
Scanner input = new Scanner(System.in);
capitalizeArray(item);
int choice,req = 0;
while (true) {
choice = menu(item, price, 0);
if (choice == 0) break;
else if (choice > item.length && choice < 0) {
System.out.println("ERROR:Invalid choice");
break;
}
else {
System.out.println("How many "+item[choice-1]+" would you like? ");
if (input.hasNextInt()) req = input.nextInt();
System.out.println(req);
}
}
input.close();
}
public static String capitalizeString(String text) {
return text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase();
}
public static String[] capitalizeArray(String[] name) {
for (int i = 0;i < name.length;i++) {
name[i] = capitalizeString(name[i]);
}
return name;
}
public static void main(String[] args) {
String[] item = {"bRead","cOLA","ROLL","BaKe"};
double[] price = {4,2,6,5};
int[] quantity = {10,25,17,22};
//capitalizeArray(item);
//System.out.println(Arrays.toString(item));
//menu(item, price, 0);
storeRun(item, quantity, price);
//returnedAmounts(167.5);
}
}
I expected to get a value for the req variable from user and use it for another purposes but I tried a lot of things like that:
Initializing the variable at the begin.
Removing the input.close() line.
(etc.)
But all of them didn't work.
Replace all input.close() by input.reset()
In method menu, replace answer = input.nextInt(); by if (input.hasNextInt()) answer = input.nextInt();
and it should work
I want to make some simple program which will count monthly rate of product. There is two inputs: cost of the product - between 100-10000 and number of rates - between 6-48. I wanted to do it like in the code below:
import java.util.Scanner;
public class Calculator {
Scanner sc = new Scanner (System.in);
double productCost;
int numberOfRates;
double loanInterestRate;
double monthlyRate;
Double print () {
Calculator c = new Calculator();
System.out.println ("Enter the value of your product from 100 to 10 000 : ");
productCost=sc.nextDouble();
if (productCost < 100){
System.out.println ("You have to choose price between 100 to 10000. Try again: ");
c.print();
} else if (productCost >10000){
System.out.println ("You have to choose price between 100 to 10000. Try again: ");
c.print();
} else if (productCost >= 100 || productCost <=10000){
c.print1();
return = productCost;
// how to return productCost to be used in next method print1()?
}
else return null;
}
void print1(){
Calculator c = new Calculator();
System.out.println ("Now enter how many rates do you want to pay from 6 to 48: ");
numberOfRates=sc.nextInt();
if (numberOfRates<6){
System.out.println ("You can't choose this number of rates. Choose between 6-48: ");
c.print1();
} else if (numberOfRates>48){
System.out.println ("You can't choose this number of rates. Choose between 6-48: ");
c.print1();
} else if (numberOfRates>=6 || numberOfRates<=12) {
loanInterestRate=1.025;
monthlyRate = (productCost*loanInterestRate)/numberOfRates;
System.out.printf("Your monthly rate is: "+ "%.2f%n",monthlyRate);
} else if (numberOfRates>=13 || numberOfRates <=24 ) {
loanInterestRate=1.05;
monthlyRate = (productCost*loanInterestRate)/numberOfRates;
System.out.printf("Your monthly rate is: "+ "%.2f%n",monthlyRate);
} else if (numberOfRates >=25|| numberOfRates<=48){
loanInterestRate=1.1;
monthlyRate = (productCost*loanInterestRate)/numberOfRates;
System.out.printf("Your monthly rate is: "+ "%.2f%n",monthlyRate);
}
}
}
And the main method only calling the method from the other class.
public class MonthlyRate {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.print();
// TODO code application logic here
}
}
And what is the problem, I don't know how to return the "double productCost" from the method "print()". productCost is taking from the input and this is double but NetBeans showing me that it's not correct type. Can anybody help me understand where is the problem?
Simply do
return productCost;
return is a keyword, not a variable. It 'returns' the given value and exits the function, so that the entity calling the function can do this:
public static void main(String[] args) {
...
double cost = calc.print(); // note calc.print() PRODUCES a value, which we assign to `cost`
...
}
You can then do whatever you want with cost (or whatever you choose to name the variable), including passing it to another function.
Your program needs changes in a few places. I have done those changes and written below the updated program:
import java.util.Scanner;
class Calculator {
Scanner sc = new Scanner(System.in);
double productCost;
int numberOfRates;
double loanInterestRate;
double monthlyRate;
void print() {
Calculator c = new Calculator();
System.out.println("Enter the value of your product from 100 to 10 000 : ");
productCost = sc.nextDouble();
if (productCost < 100) {
System.out.println("You have to choose price between 100 to 10000. Try again: ");
c.print();
} else if (productCost > 10000) {
System.out.println("You have to choose price between 100 to 10000. Try again: ");
c.print();
} else if (productCost >= 100 || productCost <= 10000) {
print1(productCost);
}
}
void print1(double productCost) {
Calculator c = new Calculator();
System.out.println("Now enter how many rates do you want to pay from 6 to 48: ");
numberOfRates = sc.nextInt();
if (numberOfRates < 6) {
System.out.println("You can't choose this number of rates. Choose between 6-48: ");
c.print1(productCost);
} else if (numberOfRates > 48) {
System.out.println("You can't choose this number of rates. Choose between 6-48: ");
c.print1(productCost);
} else if (numberOfRates >= 6 || numberOfRates <= 12) {
loanInterestRate = 1.025;
monthlyRate = (productCost * loanInterestRate) / numberOfRates;
System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);
} else if (numberOfRates >= 13 || numberOfRates <= 24) {
loanInterestRate = 1.05;
monthlyRate = (productCost * loanInterestRate) / numberOfRates;
System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);
} else if (numberOfRates >= 25 || numberOfRates <= 48) {
loanInterestRate = 1.1;
monthlyRate = (productCost * loanInterestRate) / numberOfRates;
System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);
}
}
}
public class MonthlyRate {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.print();
// TODO code application logic here
}
}
It is easy to understand the changes after comparing your program with this updated program. Nevertheless, feel free to let me know if you need any further help on this.
I'm making a currency tracker to track the live value of bitcoin.
the class "bitcoinlive" runs Correctly if I run it in its own main method, but it won't work when I make an instance of the file. I need it to print the live value you of bitcoin.
I try to print out the variable "a53" but I don't know if I'm doing it right.
Here are the list of imports for the bitcoinlive class because it kept giving me an error message and wouldn't allow this to be apart of the code when posting this.
public static void main(String[] args) {
Dates d = new Dates();
String s = d.getDate();
System.out.println("Date is" + s);
W3 mywallet = new W3();
Bitcoinlive mybitcoinlive = new Bitcoinlive();
L3 myledger = new L3();
Scanner myscanner = new Scanner(System.in);
double buy = 0.0;
int choice = 0;
double bitcoin = 4000;
double USD = 20000;
while (choice != 5) {
System.out.println("Welcome! Enter a command. \n"
+ "Enter 1) Buy Bitcoin \n"
+ "Enter 2) Sell Bitcoin \n"
+ "Enter 3) Print Balance \n"
+ "Enter 4) Print History \n"
+ "ENTER 5) Exit Program\n");
choice = myscanner.nextInt();
if (choice == 1) {
System.out.println("How many? ");
buy = myscanner.nextDouble();
mywallet.add(buy);
bitcoin = bitcoin * buy;
USD = USD - bitcoin;
myledger.save(s);
System.out.println("you have bought:" + mywallet.numcoins);
System.out.println(USD);
System.out.println(mybitcoinlive.a53);
bitcoin = 4000;
} else if (choice == 2 && USD >= bitcoin) {
System.out.println("How many?");
buy = myscanner.nextDouble();
mywallet.subtract(buy);
System.out.println("you have sold:" + mywallet.numcoins);
USD = USD + bitcoin;
System.out.println(USD);
bitcoin = 4000;
myledger.save(s);
} else if (choice == 3) {
System.out.println("Balance:" + mywallet.numcoins);
} else if (choice == 4) {
System.out.println("Transaction history: ");
System.out.println("you have made" + myledger.getsize() + "transactions"
+ d.getDate());
} else if (choice == 5) {
// exit
break;
} else if (choice == 7) {
System.out.println(mybitcoinlive.price);
}
}
System.out.println("Bye");
}
this is my separate class
public class Bitcoinlive {
Double a53=0.0;
double price;
Double get() {
try {
String urlcoincapeth13 = "https://api.coinmarketcap.com/v1/ticker/bitcoin/";
Document docblocktradescoincapeth13 = Jsoup.parse(new URL(urlcoincapeth13).openStream(), "UTF-8", "", Parser.xmlParser());
String a13 = docblocktradescoincapeth13.toString();
int a23 = a13.indexOf("price_usd") + 13;
int a33 = a13.indexOf("price_btc") - 4;
String a43 = a13.substring(a23, a33);
a53 = Double.parseDouble(a43);
} catch (Exception e) {
System.out.println("Error accessing bitcoin values");
}
return a53;
}
}
Your class Bitcoinlive stores the price in a field called a53. You can update this field by calling get(). However, it looks like you're never calling get() - you're just calling the field:
System.out.println(mybitcoinlive.a53);
Try replacing that line with:
System.out.println(mybitcoinlive.get());
Or refresh it first:
mybitcoinlive.get();
System.out.println(mybitcoinlive.a53);
System.out.println("Do you want to add 1? if yes type in '0.01' ");
the problem with this is that it runs only once. I tried to add it inside a while loop (while(i > 100)) but it simply printed till 100 wihtout asking the user again
My code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int i = 0;
double yes = 0.01;
System.out.println("i = " + i);
System.out.println("Do you want to add 1? if yes type in '0.01' ");
Scanner keyboardInput = new Scanner(System.in);
double add = keyboardInput.nextDouble();
if (yes == add) {
i++;
System.out.println("i now is: " + i);
}
}
}
Write while(i < 100) not while(i > 100)
This works fine:
public static void main(String[] args) {
int i = 0;
double yes = 0.01;
while(i < 100) {
System.out.println("i = " + i);
System.out.println("Do you want to add 1? if yes type in '0.01' ");
Scanner keyboardInput = new Scanner(System.in);
double add = keyboardInput.nextDouble();
if (yes == add) {
i++;
System.out.println("i now is: " + i);
}
}
}
I need some help with this as I'm completely lost. I'm trying to call a method I created in another class to output what the grade is for the average entered by the user, but I'm not sure where I messed up. I don't get how to call my other class, I read that using an object as a variable doesn't work in Java, but I have no clue how else to get it to pull through to apply the method in the other class to give the correct grade.
Here's the class that contains the method....
public class Assignment {
private double score;
public double getScore(){
return score;
}
public void setScore(double newScore){
score = newScore;
}
public static void newScore(double a, double b, double c, double d, double f) {
double score1 = a;
double score2 = b;
double score3 = c;
double score4 = d;
double score5 = f;
if (score1 >= 90) {
System.out.println("Your grade is: " + score1);
System.out.println("Great job!");
} else if (score2 >= 80 && score2 < 90) {
System.out.println("Your grade is: " + score2);
System.out.println("Not bad!");
} else if (score3 >= 70 && score3 < 80) {
System.out.println("Your grade is: " + score3);
System.out.println("Need a little work");
} else if (score4 >= 60 && score4 < 70) {
System.out.println("Your grade is: " + score4);
System.out.println("Not looking so good.");
} else if (score5 >= 50 && score5 < 60) {
System.out.println("Your grade is: " + score5);
System.out.print("Study harder!");
} else {
System.out.println("Yikes!");
}
}
}
And here's what I put in the main method...
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Assignment newScore = new Assignment ();
System.out.println("Enter your test score: ");
newScore = input.nextDouble();
System.out.println("Your current grade is: " + newScore);
}
}
Don't quite understand your question here. You have a method that takes in multiple score, and it has a set of if-else statements to determine the message to print based off;
if first score is more than 90 do this,
and if first score's condition is not met, then check second score value.
and then if second condition not met, then check third score to see if it met certain score.. blah blah
If you are looking at having an assignment class to keep a set of scores and finally print out each score then do this;
Modify your Assignment class is to allow multiple scores to be kept at a time. To be able to hold multiple scores you will have to change the score variable to be at least an array or a list. That is, List<Double> scores = new ArrayList<Double>();
Then modify the setter method to be something like "addScore(double score)"
public void addScore(double score) {
scores.add(score)
}
In your "newScore" method, use a loop to iterate through the score to output the appropriate score message for each assignment.
for (Double score : scores) {
if (score > 90) {
System.out.println('blah');
}
}
Note the code above are just something I type off my mind, I didn't test it using any compilers so you may have to rework on some syntax errors.
Since the variable newScore is a Assignment object rather than a double, the code newScore = input.nextDouble(); can not be compiled. I suppose you are using the input number as your score, so that line should be
newScore.setScore(input.nextDouble());
Hence the next line should be
System.out.println("Your current grade is: " + newScore.getScore());
Kindly replace these two lines and check if it suites your requirements.
EDIT:
Assignment.java:
public class Assignment {
private double score;
public double getScore(){
return score;
}
public void setScore(double newScore){
score = newScore;
}
public void checkScore(double a, double b, double c, double d, double f) {
if (score >= a) {
System.out.println("Your grade is: " + score);
System.out.println("Great job!");
} else if (score >= b && score < a) {
System.out.println("Your grade is: " + score);
System.out.println("Not bad!");
} else if (score >= c && score < b) {
System.out.println("Your grade is: " + score);
System.out.println("Need a little work");
} else if (score >= d && score < c) {
System.out.println("Your grade is: " + score);
System.out.println("Not looking so good.");
} else if (score >= f && score < d) {
System.out.println("Your grade is: " + score);
System.out.print("Study harder!");
} else {
System.out.println("Yikes!");
}
}
}
Grades.java:
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Assignment newScore = new Assignment();
System.out.println("Enter your test score: ");
newScore.setScore(input.nextDouble());
newScore.checkScore(90, 80, 70, 60, 50);
System.out.println("Your current grade is: " + newScore.getScore());
}
}