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 6 years ago.
Improve this question
Trying to call a method within a method.
public static void main(String[] args) {
int sq,cu=0;
//user input 1
sq=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be squared"));
//user input 2
cu=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be cubed"));
//results
JOptionPane.showMessageDialog(null, sqd(sq));
JOptionPane.showMessageDialog(null, cbd(cu));
}
public static String sqd(int sq){
int sqd=sq*sq;
//sq computation
return sq+" squared is "+sqd;
}
public static String cbd(int cu,int sqd){
int cbd;
cbd=sqd*cu;
//cu computation
return cu+" cubed is "+cbd;
} }
calling sqd value in cbd, but
JOptionPane.showMessageDialog(null, cbd(cu));
prevents me from doing so, it always gives me an error when I run it.
your cbd(int cu, int sqd) method needs 2 input parameters , you are calling it with only one parameter cbd(cu)
two choices :
1 - rewrite your cbd method with 1 parameter then you can call it using cbd(cu);
public static String cbd(int cu){
return cu + " cubed is " + (cu*cu*cu);
}
2 - write it's second parameter while you are using it :
cbd(cu,cu*cu);
Your cbd method, as it's currently defined, takes two arguments - cu and sqd. If you want to keep the way your main calls it, you need to rewrite it with just one argument:
public static String cbd(int cu){
int cbd = cu * cu * cu;
return cu + " cubed is " + cbd;
}
Related
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 1 year ago.
Improve this question
I'm in college and am trying to learn Java but I'm having a bit of trouble with some code. The "if (totCreditsEarned >= 180)" is erroring in my Eclipse, specifically the part in parentheses, and it's unable to give any suggestions to fix it.
import java.util.Scanner;
public class CSC161lab6_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Credits Earned: ");
String totCreditsEarned = scanner.nextLine();
System.out.println("Credits Earned is: " + totCreditsEarned);
if (totCreditsEarned >= 180) {
System.out.println("Wow!");
} else {
System.out.println("Oof!");
}
}
}
180 is an Integer. totCreditsEarned is a String.
You will need to convert totCreditsEarned to an Integer
if (Integer.valueOf (totCreditsEarned) >= 180)
...
Of course if you enter a non-integer value it will fail
The full compilation error is.
CSC161lab6_1.java:13: error: bad operand types for binary operator '>='
if (totCreditsEarned >= 180) {
^
first type: String
second type: int
1 error
It's telling you that you can't compare an int and a String using >=. You need two ints, so totCreditsEarned must be an int or converted to an int before you can compare them.
You can read it from the Scanner as an int. So no need to convert after.
int totCreditsEarned = scanner.nextInt();
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 4 years ago.
Improve this question
I'm writing a program with 3 options. So the first one is about the employee- (create / remove / update / get information about employee / save to file). Before creating a new employee I have to choose his type(programmer or qa)(difference between them is that programmer have a specific programming language and qa have amount of worked hours). So moving forward when I create a new user I have to enter name / surname / age / prog.language;
The second option in my program is that I can create a team which must be made from 3 employees. So from a list of employees you select one for team lead and other 2 for 'workers'.
And the last one is that you can create a task.
(you need to give a name for task, specific language which is required from a second team member and amount of worked hours from a third member). So this task can be later assigned to a specific team.
So lets talk about my problem right now:
Creating new employees, making new teams works 100%, also creating new tasks works fine as well, but when I try to check does my selected team meets requirements for tasks I'm receiving tons of errors. I've tried to select specific member from a team and check his programming language and receiving null. However, after debugging I saw that information comes,but when i try to reach exactly that language appears null.
Here's my code how looks my programmer class:
package com.wep;
public class Programuotojas extends Darbuotojas {
protected String programavimoKalba;
#Override
public String toString() {
return "Programuotojas: " + vardas + ",pavarde " + pavarde + ",amzius " + amzius + ",programavimo kalba " + programavimoKalba;
}
public Programuotojas(String vardas, String pavarde, int amzius, String programavimoKalba) {
super(vardas, pavarde, amzius);
this.programavimoKalba = programavimoKalba;
}
Programuotojas(){}
public String getProgramavimoKalba() {
return programavimoKalba;
}
public void setProgramavimoKalba(String programavimoKalba) {
this.programavimoKalba = programavimoKalba;
}
}
And here's my try to check his language:
KomanduValdymas.getInstance().komanduArray.get(0).getPirmasDarbuotojas(programuotojas.getProgramavimoKalba());
KomanduValdymas is a class where I create new teams. If u need more code from there let me know. Thanks, hope you guys got my problem
private void pridetiDarbuotoja() {
System.out.println("[1] Pridėti programuotoją");
System.out.println("[2] Pridėti testuotoją");
Scanner SI = new Scanner(System.in);
int userSelects = Integer.parseInt(SI.nextLine());
if (userSelects == 1) {
System.out.println("Iveskite:");
System.out.println("Varda, pavarde, amziu, darbine programavimo kalba");
String enters[] = SI.nextLine().split(" ");
darbuotojuArray.add(new Programuotojas(enters[0], enters[1], Integer.parseInt(enters[2]), enters[3]));
System.out.println("Darbuotojas itrauktas i sarasa");
} else {
System.out.println("Iveskite:");
System.out.println("Varda, pavarde, amziu, isdirbtas testavimo valandas");
String enters[] = SI.nextLine().split(" ");
darbuotojuArray.add(new Testuotojas(enters[0], enters[1], Integer.parseInt(enters[2]), Integer.parseInt(enters[3])));
}
darbuotojuValdiklis();
}
You appear to be under the impression that creating a new Programuotojas will update the value of your variable programuotojas automatically. That is not the case.
You need a statement that starts with programuotojas = in order to affect such a change.
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 6 years ago.
Improve this question
/**
* this is from the r2j class
*/
public String getCurrentState() {
trace("getCurrentState() starts... ...and ends with value " + currentState + ", " + STATES[currentState]);
return currentState + ", " + STATES[currentState];
}
/**
* provide the identity of the specified state
* #return which state is of a given number
*/
public String getSpecificState(int stateNum) {
trace("getSpecificState() starts... ...and ends with value " + stateNum + ", " + STATES[stateNum]);
return stateNum + ", " + STATES[stateNum];
}
You call bot.getSpecificState(bot.getCurrentState()). bot.getSpecificState() expects an int parameter, but bot.getCurrentState()returns a String. Java complains, because the passed Stringis not compatible with the expected int.
The class R2JCD2 has method getSpecificState which is probably expecting an int parameter and you seem to be passing it a string parameter, i.e. the return value of method getCurrentState of the same class. If you provide the R2JCD2 class source then we would know for sure.
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 wonder how did I get the result of '6.03' and '23.24'? System.out.println(a+b+c) does not add up to those 2 numbers?
Why is there a return c + "" + a? There is no output result show c + a?
The full code as follows:
MyProgram:
public class MyProgram
{
public void start()
{
String result;
result = lots(2+1, 3, "3");
result = lots(22, 1.2, "4");
}
private String lots(int a, double b, String c)
{
System.out.println(a+b+c);
return c + "" + a;
}
}
MyApplication:
public class MyApplication
{
public static void main (String[] args)
{
MyProgram p = new MyProgram();
p.start();
}
}
It makes sense because you're concatenating some string at the end of all of that math.
For the first run, you pass in 3 and 3.0 as your first two numerical parameters. That adds to 6.0 (it's been since upcast to double). You now then do string concatenation on 3 to arrive at 6.03 for your answer.
Trace through the second execution of the method to arrive at a similar answer. Remember: + is overloaded in Java to mean either numeric addition or string concatenation.
You overwrite result after you get it the first time, but even then, you don't do anything with it. I'd argue that you don't really need the return statement. If it were there, then you'd actually return the string 422*, since again, it's all string concatenation at that level.
*: This is from the last standing run if you printed it out after both were executed. If it were printed out each time, you'd see 22 first.
a+b+c is (a+b)+c
The first addition adds the two numbers.
The second addition converts the result of the addition into a string, then concatenates it with the final string.
So ((2+1) + 3) = 6.0
and 6.0 + "3" = "6.0" + "3" = "6.03"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know that primitive types called by value(a copy of value has been sent)
My code:
static int a = 5;
public static void main(String[] args) {
System.out.println("a= " + a);
setA();
System.out.println("a= " + a); //Why is not 5?
setA2(a);
System.out.println("a= " + a); //Why is not 7?
}
public static int getSeven() {
return 7;
}
public static void setA() {
a = 6;
}
public static void setA2(int n) {
n = getSeven();
}
Output:
a= 5
a= 6
a= 6
Why other two output is 6 again?
Why a is changed?
a is not an object!
The second output is 6 since you set it to 6 by the preceding row (function setA()). setA() is setting a primitive outside of the function but in the class, so this is also available in main().
The third output is 6 since you set the primitive n instead of a. It is a primitive so it has no reference to a anymore.
a is global variable.
setA() will set the value of a as 6. So every where the value of a will be six.
getSeven() is returning 7 but you are not capturing it. So a will remain as it is.
try it like this a=getSeven();
setA2(int n)is assigning n to 7 but not to a.
try it like this
public static void setA2(int n) {
a = getSeven();
}