What is the reason behind this kind of error? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm very new to programming and I've been teaching myself for almost a month now, can someone please explain to methe reason behind error in my code? It errors in the "total(moneyConv(moneySum * moneyRate));" line,
saying actual and formal argument differs in length. I've check all my parameters and it seemed fine to me. thanks a lot!
public class NewClass {
static Scanner sc = new Scanner(System.in);
public static float moneySum;
public static float moneyRate;
public static void findSum(float sum) {
moneySum = sum;
}
public static void findRate(float rate) {
moneyRate = rate;
}
public static float moneyConv(float sum, float rate) {
return sum * rate;
}
public static void total(float total) {
System.out.println(total + "Here is the total of your transaction.");
}
public static void main(String[] args) {
System.out.print("Input amount of money : ");
findSum(sc.nextFloat());
System.out.print("Input exchange rate : ");
findRate(sc.nextFloat());
total(moneyConv(moneySum * moneyRate));
}

the moneyConv() method parameters have 2 arguments: a float datatype sum and another float datatype rate. When you look at the method call:
total(moneyConv(moneySum * moneyRate));
you are actually trying to call the method moneyConv(float sum, float rate) but with one argument instead with a float datatype, as a result of the multiplication of moneySum and moneyRate. This is not valid since moneyConv accepts 2 arguments.
So, the fix would be total(moneyConv(moneySum, moneyRate));
First the nested method moneyConv(moneySum,moneyRate) will be executed and after the method total will be executed with the result of the moneyConv method.

Method parameters must be separated by commas:
total(moneyConv(moneySum, moneyRate));
moneySum * moneyRate is first evaluated and becomes a single value which is passed to moneyConv which actually requires two arguments.
It is equivalent to:
float temporary = moneySum * moneyRate;
total(moneyConv(temporary))

Related

Frustrating Syntax Issues with passing values into methods [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I needed to rewrite a java program I had already completed to now include a class I would be able to test with using JUnit4. Unfortunately I can't even get to that point because I'm receiving an error. It's a really simple program where I ask the user for 3 numbers, pass those values into a function that does some calculations and should return a print statement with the value back. I made it work without the function and I'm having trouble with the syntax and fully understanding what I can and can't do with methods.
Here it is:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
System.out.println("Given ax^2 + bx^2 + c = 0");
System.out.println("Please enter 'a', 'b', and 'c' to determine if there are any roots: ");
float numA = kybd.nextFloat();
float numB = kybd.nextFloat();
float numC = kybd.nextFloat();
quadraticAnswer(numA, numB, numC);
}
public static void float quadraticAnswer (float numA, float numB, float numC){
float discriminant = ((numB*numB)-(4*numA*numC));
if (discriminant < 0){
System.out.println("The Equation has no roots!");
}
else if (discriminant ==0) {
float root = (-numB + Math.sqrt(discriminant))/(2*numA);
System.out.println("The Equation has one root: "+ root);
}
else {
float root1 = (-numB + Math.sqrt(discriminant))/(2*numA);
float root2 = (-numB - Math.sqrt(discriminant))/(2*numA);
System.out.println("The Equation has two roots: " + root1 + " and " + root2 + ".");
}
}
}
Change the invalid syntax of
public static void float quadraticAnswer
to
public static void quadraticAnswer
as you are not returning anything.
If you use an IDE like Eclipse it will quickly highlight such errors

Using functions in Java Programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have doubt in using functions in java. I have wrote code for sum of natural numbers using a recursive function but I don't understand the error I am getting. I know it's a silly question though I'm a beginner and I need a brief explanation.
Here is the code
import java.util.Scanner;
public class natural {
public static int main(String args[]){
int a, s = 0,y;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number:");
int x = in.nextInt();
public static int SN(y)
{
if(x==1)
{
return 1;
}
else{
int N = SN(x-1) + x;
return N;
System.out.println("THE SUM IS :"+x);
}
}
Several problems:
You cannot declare a method within a method. Your SN method must be declared outside of the main method.
The parameter y in your SN method must have a type. Based on usage, it is probably supposed to be an int, so the method signature should look like SN(int y).
Despite the method parameter being called y, you appear to be using x everywhere. You should change x to y in the SN method, since that is the label of the data being passed to the method.
As others have pointed out, statements after the return line are unreachable, and as Matt Coubrough said, your IDE is likely warning you about this. Place it before the return line.
Well, one problem here is that you have an unreachable statement. Your System.out.println("THE SUM IS...") is never reached.

how to remove smallest value digit from an integer and return the rest of it without using string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
how to remove smallest value digit from an integer and return the rest of it without using string.Like if we have number 4412,then reomive the 1 and return 442.
I did the coding for extracting the smallest number but dont know how to combine rest of them.
public class RemoveSmallestDigit {
static int testcase1 = 4487;
static int testcase2 = 1111;
public static void main(String args[]){
RemoveSmallestDigit testInstance = new RemoveSmallestDigit();
int result = testInstance.removeSmallestDigit(testcase1);
System.out.println(result);
}
//write your code here
public int removeSmallestDigit(int num){
int small=9;
int digit=0;
while(num!=0){
digit=num%10;
num=num/10;
if(digit<=small){
small=digit;
}
}
System.out.println(small);
return small;
}
}
I suggest that you break this down into steps. For example, you might do something like
Find the digits of a number and store as an array
Find the lowest digit and remove it from the array
Convert the new array back to a number
Of course, you can come up with your own steps if you find something that makes more sense to you. The main idea is to break a problem down into smaller problems. If you have trouble with any of these smaller problems, please come back with more questions.
public int removeSmallerdigit( int a)
{
int smalldigit=0;
int ReverseNumber=0;
int finalNumber=0;
int digit=0;
while(a>0)
{
digit=a%10;
a=a/10;
if(smalldigit>digit)
{
smalldigit=digit;
}
ReverseNumber=ReverseNumber*10+digit;
}
while(ReverseNumber>0)
{
digit=ReverseNumber%10;
ReverseNumber=ReverseNumber/10;
if(smalldigit!=digit)
{
finalNumber=finalNumber*10+digit;
}
}
return finalNumber;
}

Java: Syntax error on token "}"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Attempting to write some code to convert feet to meters and meters to feet and achieve the table shown below. This is my first java program and I have no idea why I'm getting syntax errors involving "}".
Feet Meters| Meters Feet
1.0 0.305 | 20.0 65.574
2.0 0.61 | 25.0 81.967
…
9.0 2.745 | 60.0 196.721
10.0 3.05 | 65.0 213.115
Here's what I have...
public class Hmwk {
public static void main(String[] args){
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
for (double i = 1.0; i <11; i++){
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));}
}}
Any and all help is much appreciated.
Here's a small tip, if you don't like indenting code, then coding might not be for you.
This may sound harsh, but I sincerely believe that before one writes his/her first program, one needs to know what code structure is all about. Unfortunately, schools and courses tend to hide or ignore this fact completely. I know, I've seen this back at secondary school - telling your teacher to properly format code isn't nice believe me.
public class Hmwk {
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
public static void main(String[] args){
for (double i = 1.0; i <11; i++){
System.out.printf(i + footToMeter(i) + "|" + (i*5+15) + meterToFoot(i*5+15));
}
}
}
In Java, you cannot have methods inside methods.
Indenting code means you can find your problems easily.
Brackets are meant to stay on separate lines, except the opening brackets (depends on taste)
You have a lot of braces in incorrect positions. Most notably, your footToMeter and meterToFoot are declared inside of your main method, which is incorrect. Here is your code with correct brace placement:
public class Hmwk
{
public static void main(String[] args)
{
for (double i = 1.0; i <11; i++)
{
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));
}
}
public static double footToMeter(double foot)
{
return 0.305 * foot;
}
public static double meterToFoot(double meter)
{
return 3.279 * meter;
}
}
As a general rule, methods cannot be nested in Java. This means that no method can be declared inside of another.
No closing brace in the void main. Put '}' before 'public static double footToMeter' and delete closing bracket after closing bracket from for

nothing will print in the compiler. [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here's the question I was asked:
Write a complete Java program called CalcTotalPrice. The program must include five methods:
getSaleTotal, getSalePrice, getSaleWeight, calcTax, and calcShipping.
getSaleTotal takes no input parameters and returns a double, which is the sale total, and which it computes by calling the other four methods.
getSalePrice returns a double, which it gets from the user at the command line.
getSaleWeight returns a double, which it gets from the user at the command line.
calcTax takes a double as a parameters (the sale price) and returns the tax amount as a double (use 6% as a fixed tax rate).
calcShipping takes a double as a parameter (the sale weight) and returns the shipping amount as a double (calculate shipping as $10 if weight is less than 10 and $20 if weight is 10 or greater).
getSaleTotal should print the sale price amount, tax amount, shipping amount, and sale total amount to the command line.
nothing will print in the compiler. Please help me.
Here's my code:
import java.util.Scanner;
/**
*
* #author Kramer1
*/
public class CalcTotalPrice {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
public static double getSaleTotal(){
Scanner in = new Scanner(System.in);
double price = getSalePrice(in);
System.out.println(price);
double tax = calcTax(.06);
System.out.println(tax);
double shipping = calcShipping(in.nextDouble());
System.out.println(shipping);
double saleTotal = ((price)*tax)+price+shipping;
System.out.println(saleTotal);
return saleTotal;
}
public static double getSalePrice(Scanner in){
double salePrice = in.nextDouble();
return salePrice;
}
public static double getSaleWeight(Scanner in){
double saleWeight = in.nextDouble();
return saleWeight;
}
public static double calcTax(double salePrice){
double salesTax = .06;
return salesTax;
}
public static double calcShipping(double saleWeight){
double amountShipping = 0;
if (saleWeight < 10){
amountShipping = 10.;
}else if(saleWeight > 10){
amountShipping = 20.;
}
return amountShipping;
}
}
You arent doing anything in your main()
To see the output, you will have to create the Scanner in main and then call appropriate methods.
You need to do some code refactoring. First, move your Scanner to the main method. Then pass it around as an argument to other methods to read data from or read data in main and pass the values directly. I suggest the latter
You also need to declare the variables you use outside the methods and into the class so that their values persist till the end of the program and you will have access to them in various methods. Do declare them static.
You have a main method that is empty - it is not doing anything or calling any code.
Try instantiating your class and calling some methods in it.
it also looks like it is expecting some input from the user. So also try instantiating a Scanner class in your main which can then be passed to some methods. Remember to also call in.nextLine(); to flush the input before calling the next in.nextDouble();
try
Static Double salesPrice = null;
public static void main(String[] args) {
CalcTotalPrice ctp = new CalcTotalPrice ();
Scanner sc = new Scanner(System.in);
salesPrice = ctp.getSalesPrice (in);
in.nextLine();
//etc
}

Categories