Variable from Separate Function Cannot Print in Main - java

public class practiceclock{
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 5;
addTonum(x);
System.out.println(gby);
}
public static void addTonum(int gby) {
gby = gby + 1;
}
}
Why doesn't this print out 6? I'm trying to print out the integer gby from my "addTonum" function. I know it's pretty basic.

In java, when you pass a variable as an argument into a method, you are passing a copy of it, not the original instance. To fix this, you can either make the variable global, or you can have the method return the integer.
Also, use
gby +=1;
As a shortcut.

Related

Why i am getting default value of a after declaring a=b+c in my java code

package abc;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
A n= new A();
System.out.println(n.b);
System.out.println(n.c);
n.j ();
}
}
class A
{
int b;
int c;
A(){
b=3;
c=8;
} int a=b+c;
void j() {
System.out.println(a);
}
}
This may solve your problem.
THIS IS A WORKING EXAMPLE
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
A n= new A();
System.out.println(n.b);
System.out.println(n.c);
n.j ();
}
}
public class A {
int b;
int c;
int a;
public A(){
b=3;
c=8;
a = b+c;
}
void j() {
System.out.println(a);
}
}
You are adding b and c in your declaration of a. So that arithmetic is done at the same time as the b and c (just after them) during the initialization of n the instance of A.
The order of instructions when initializing an instance of a class is to initialize the field variables first, and then call the constructor A (). It is not the order that they appear in the lines of the program.
So the sequence is to
enter the program at main ()
call the constructor new A ()
the "new" knows that it has to initialize the fields first
initialize b (to 0)
initialize c (to 0)
initialize a to b + c (to 0 = 0 + 0)
start running the actual constructor A ()
set b to 3
set c to 8
exit the constructor
return to main ()
print n.b
exit the program
Note that the value of a is computed outside of the constructor. So adjust your placements so that the right order that you want is executed.
This kind of walk through the code is a really good way to iron out bugs. Soon you will be doing it in your head like second nature.
Put a debug point in
int a = b+c
you will know the reason. If something doesn't work, pls debug and see if it is taking you somewhere near the solution.
Answer:
Execution order is like this.
Instance variables are instantiated first, then follows the constructor.
Going by this, all the variables are not initialized to a value in your code, so default value of 0 is assigned to them. Hence "a" gets 0, even though b and c are assigned to a value later in the constructor.

Trying to print a method using another method

Not sure where I'm going wrong with this. I've asked someone in my class and they said there should be an argument with "toonRijSterren". when I do this I just get more errors, could someone have a look and tell me where I'm going wrong?
public static void main(String[] args) {
int aantal = 0;
toonRijSterren(aantal);
toonSterrenVierkant(aantal);
}
public static void toonRijSterren(int mpAantal) {
while (mpAantal < 6) {
System.out.print(" * ");
mpAantal++;
}
}
public static void toonSterrenVierkant(int mpAantal) {
for (int mpAatal = 0; mpAantal < 6; mpAantal++) {
System.out.println(toonRijSterren());
}
}
ther error line is in the brackets of the last toonRijSterren());
toonRijSterren is void method which means it does not return any value and therefore you can not put it inside System.out.println() or you can not assign it to some variable.
toonRijSterren expects an int argument which you have missed while calling it.
Given below is an example of how you should call toonRijSterren:
public static void toonSterrenVierkant(int mpAantal) {
for (int mpAatal = 0; mpAatal < 6; mpAatal++) {
toonRijSterren(mpAantal);
}
}
You are not passing the argument when you call your method.
Try this:
System.out.println(toonRijSterren(mpAatal));
First of all, your function toonRijSterren takes an int type parameter (according to its declaration), so you need to pass to it another argument. For example:
toonRijSterren(mpAantal)
Second, the function toonRijSterren returns void. That means, it just does an operation (in this case, printing) without returning anything. What you're trying to do is to use its return value (which doesn't exist) as an argument to System.out.println, which causes an error (because println expects an argument of some type).
You could achieve what I think you're trying to do with the line:
toonRijSterren(mpAantal);.
The function itself prints the values, so the println here is unnecessary and causes an error.
You are missing the parameter in your toonSterrenVierkant() function where you calling toonRijSterren.
Here is the corrected version of your code:
public static void toonSterrenVierkant(int mpAantal) {
for (; mpAantal < 6; mpAantal++) {
toonRijSterren(mpAatal);
}
}
As your methed toonSterrenVierkant(int mpAantal) has a int parameter, you must pass an int value as an argument in the last toonRijSterren(). For example, replace the line System.out.println(toonRijSterren()); with System.out.println(toonRijSterren(1));

scan error : the value of local variable scan is not used

import java.util.Scanner;
public class KillBill {
public KillBill() {
// TODO Auto-generated constructor stub
}
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
}
}
IN 11th LINE .IT SAYS THE VALUE OF LOCAL VARIABLE SCAN IS NOT USED
If a variable is set and not used, it gives an error. You could correct the warning by using Scan, with something like:
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner (System.in);
String text = scan.nextLine();
System.out.println(text);
}
The idea here is that every variable is being used now.
Well, to be entirely honest, you're Scanner object ISN'T BEING USED elsewhere in the code you've provided. But don't worry, this is just a warning (yellow jagged underline) and not an error (red jagged underline). In this case, it's just telling you that your program, at the moment, has an unused variable, whose deletion won't affect the code than what it currently is. Remember, a computer still has to read each line, and reading unused variables, especially objects, might slow your program down, even though by only a fraction of a millisecond. So it is advised to delete this unnecessary lines of code.
But remember, this is all relevant for the CURRENT situation, and not if you're gonna make changes to your program, by literally USING your unused variable.
Try this.
public class KillBill {
public KillBill(Scanner scan) {
// TODO Auto-generated constructor stub
int value = scan.nextInt();
System.out.println(value);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
new KillBill(Scan);
}
}
That problem/warning is caused by unused variable. Not a serious issue.
This is not a problem to declare Scanner and not use it, the real problem is the signature of your main method signature, it should not UpperCase you have to use :
public static void main(String[] args) {
// ^^-----------------In your program it is M
Note java use CamelCase your names of variables shold start with lower case for the good practice (Scan should be scan)

Compiler can't identify my variable

I'm running this simple program to play around with arrays and nested for loops. For some reason my compiler can't identify the variable "r"? I don't know why it is doing this. Any suggestions?
public class ForLoop {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] mat = new int[4][8];
for(int r=0;r<mat.length;r++);
{
for(int c=0;c<mat[r].length;c++)
{
mat[r][c]=r*c+c/2+r*(c+1);
}
System.out.println(mat[0][2]);
}
}
}
The semicolon terminates the for body immediately (as an empty expression) here
for(int r=0;r<mat.length;r++);
{ //<-- not part of the for.
remove the semicolon so that the next block is part of the for loop
for(int r=0;r<mat.length;r++) {

When the use sort an array, fix and write private static void sort

My Main Code is :
public class Arrays {
public static void main(String[] args) {
//////////////// Sorting Arrays
String [] castNames = new String [6];
castNames[0] = "Zareyee Merila";
castNames[1] = "Hosseini Shahab";
castNames[2] = "Bayat Sareh";
castNames[3] = "Peyman Moadi";
castNames[4] = "Hatami Leila";
castNames[5] = "Farhadi Sarina";
Arrays.sort(castNames);
for (int number = 0 ; number < 6 ; number++) {
System.out.println(number + " : " + castNames[number]);
}
}
}
How can I fix this line of code:
Arrays.sort(castNames);
Without having to write this one:
private static void sort(String[] castNames) {
// TODO Auto-generated method stub
}
Okay, this question is a bit vague, but I expect you're just running into an issue with name collision.
You've named your class Arrays. This class has no static method called sort. There is, however, a utility in Java called java.util.Arrays which does implement a static sort method.
Your code is not calling the Java utility class, unless there's an import statement you haven't included.
Try changing the line to this: java.util.Arrays.sort(castNames);
Otherwise, you might consider renaming your Arrays class to something else.

Categories

Resources