Public method not returning the variable - java

Im new in Java why is my method not returning the variable I put. I want to use the return thing I dont want to use sysout but its not working.
public static void main(String[] args) {
counter("this is a test");
}
public static int counter(String sentence) {
int count = 0;
while (CODE DELETED){
count=count+1;
MORE CODE DELETED
}
CODE DELETED
return count;
}

It does return it, but you neither assign the value, nor do you use it.
Try this:
public static void main(String[] args) {
int result = counter("this is a test");
System.out.println("result = " + result);
}

The method is returning the value, but you are not doing anything with the returned value.
Maybe you misunderstand what "returning a value" means. It does not mean that the returned value is automatically printed to the console.
You'll have to put the returned value in a variable in the main method, and then you can for example print it:
public static void main(String[] args) {
int value = counter("this is a test");
System.out.println(value);
}
You can also print it directly, not storing it in a variable:
public static void main(String[] args) {
// Print whatever the call to the method 'counter(...)' returns
System.out.println(counter("this is a test"));
}

Related

How can i call the method from another class?

Hi i am trying to solve the problem I am facing
public class exam {
public static void main(String[] args) {
test1 a = new test1();
}
int zahl(int x, int y) {
int e;
if(x>y) {
e=x-y;
}else {
e=y-x;
}
if(e==0) {
return 0;
}
int z=0;
int i=1;
while(i<=e) {
z=z+i;
i++;
}
return z;
}
}
what I want to do is to call the zahl method to the test1 class
public class test1{
private exam b;
public void init() {
b = new exam();
}
void test() {
int result = b.zahl(2, 2);
assertEquals(1, result);
}
}
this is what I have tried, but it returns nothing, even though it's supposed to show me error.
You should probably be declaring your functions with the public tag i.e. public void test() if you intend to access them from other functions outside of that package. The usual Class naming convention in Java is with capital first letter, which makes your code more readable for you and others.
For your question, I don't think you are actually invoking the test() method of the test1 class. If you want that method to get called every time, you could place it inside the default Constructor.

New to java; how do I make the String static within conditionals?

public class testing {
public static void main(String[] args)
{
boolean a = true;
if (a) {
public static String word = " ";
}
else if (a == false) {
public static String word = "not";
}
System.out.println(word);
}
}
Instead of printing the value, it tells me "Illegal modifier for the variable word; only final is permitted.
I tried to use public static final String word = "not";
but I still got an error saying that it is wrong.
The variable should be created outside of your main:
public class Testing {
public static String word;
public static void main(String[] args)
{
boolean a = true;
if (a) {
word = " ";
} else {
word = "not";
}
System.out.println(word);
}
}
Alternatively you can create a variable inside you main as well.
public class Testing {
public static void main(String[] args)
{
boolean a = true;
String word;
if (a) {
word = " ";
} else {
word = "not";
}
System.out.println(word);
}
}
When variables, blocks or methods are made static, they are made available during load time. Rest all reserves memory during run time. All the local variables created inside a static method are present in the method stack, which as a package is already present during load time. So creating a static variable inside method, be it be static or non-static method, is not allowed.

Trying to increment local variable from a separate method but not working. Confusion about Activation Stack/Record

public class Demo {
public static void main(String[] args){
Demo instance = new Demo();
instance.init();
}
public void init() {
int size = 0;
inc(size);
System.out.println(size);
}
public int inc(int size){
size++;
return size;
}
}
When I call the code above, the number zero is returned.
Even declaring size as a class attribute instead of a local variable does not solve the problem. I understand that when a method is complete, the corresponding record (containing local variable and such) is popped off of the activation stack. But, if the size variable is declared in the init() method, and then incremented and returned in a separate method (inc()), shouldn't size be equal to 1?
When incrementing you do not assign the value to anything, it increments it, but it does not store it anywhere so the value remains 0, try doing like this.
public class Demo
{
public static void main(String[] args)
{
Demo instance = new Demo();
instance.init();
}
public void init()
{
int size = 0;
size = inc(size);
System.out.println(size);
}
public int inc(int size)
{
size++;
return size;
}
}
or like this
public class Demo
{
public static void main(String[] args)
{
Demo instance = new Demo();
instance.init();
}
public void init()
{
int size = 0;
System.out.println(inc(size));
}
public int inc(int size)
{
size++;
return size;
}
}
size = inc(size);
will solve your problem, since you are not using a public scoped variable.
If you want to make this a bit elegant (at least I think this will be a bit more handy), then you need to declare a variable as a class variable.
I will illustrate this to you:
public class Demo {
int size; //global range variable
public static void main(String[] args){
Demo instance = new Demo();
instance.init();
}
public void init() {
this.size = 0;
inc();
System.out.println(this.size);
}
public void inc(){
this.size++; //will increment your variable evertime you call it
}
}

Trouble calling args in another method

Pretty new here in java. I'm trying to add methods to my otherwise currently working program to print out amount of characters from an args array. Trying to install a new method, I'm having trouble with calling args. This is my current code, and the red outline is.
static void amountOfCharsInSentence() {
int sum=0;
for (String s: args) { //args on this line is marked red
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence();
}
Any hint or tips in the right direction would be appreciated.
args is an argument to main, which means you can only use that argument (which is like a local variable) within main.
If you want to use it elsewhere, you have two options. The most suitable is to pass it to your other function as an argument:
static void amountOfCharsInSentence(String[] args) {
// Declare argument here -----------^^^^^^^^^^^^^
int sum=0;
for (String s: args) { // `args` here is the argument to *this* function
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence(args);
// Pass it here --------^^^^
}
Your other option is to create a static member of your class, which all methods in your class have access to, and assign args to that static member:
class YourClass {
static String[] commandLineArgs;
static void amountOfCharsInSentence() {
int sum=0;
for (String s: commandLineArgs) { // Use the static member here
sum+=s.length();
}
}
public static void main(String[] args) {
commandLineArgs = args; // Assign the static member here
amountOfCharsInSentence();
}
}
...but that's probably not appropriate in this case.
You have not passed the args.
static void amountOfCharsInSentence(String[] args) {
int sum=0;
for (String s: args) { //now it will not give red line.
// red line is indicated because there is no args inside this method
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence(args);
}
Hope this solves your problem

Getting Certain Method Into Main Method in Java

I've written up a code with several different methods. I can't seem to find the correct way to get the last method I have into the main method so it can print out the correct output.
CODE:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
int num = in.nextInt();
} // this is my main method
public void results (int num) {
for (int i = 1; i < num; i++) {
System.out.print(space(num - i));
System.out.println(method1(i));
}
for (int i = 0; i < num; i++) {
System.out.println(method2(num-i));
System.out.print(space(i));
}
} //this is the method that I want inside my main method
I thought I could simply put System.out.println(results(num)); into my main method but that doesn't work. Can anyone explain what I'm doing wrong and help me with this problem?
Your main method is static, but your results method is not. Either make results be static, or declare a new instance of your class to use inside of main.
public class MyClass
{
public static void main(String[] args) {
results(1);
}
public static void results (int num) {
}
}
or
public class MyClass
{
public static void main(String[] args) {
new MyClass().results(1);
}
public void results (int num) {
}
}
You can't pass what results returns to anything such as System.out.println, because results has a void return type. But results already is printing out information, so just call results.
results(num);

Categories