I'm completely new to java, would like some help.
I have a class named class1 and within the class are 2 functions f1() and f2()
I take a number as input in f1(). I would like to have that number in f2() as well.
I did this by returning the number from f1() to main and then accessing the same from f2() but then I tried to do the same with a number and an array together at the same time and failed.
Can someone please help me out, I need to have the array and number which are initialized to appropriate values in f1() to be available to me in f2()
Thanks in advance. :)

Recommended Answers

All 3 Replies

Member Avatar for ztini

Not sure what you're looking for...perhaps you could provide examples of the method calls that evidently did not work?

Or try something like this:

public class Class1 {

	private int[] input;
	
	public void f1(int input) {
		f1(new int[] {input});
	}
	
	public void f1(int[] input) {
		this.input = input;
	}
	
	public void f2() {
		System.out.println(input);	
	}
	
	public static void main(String[] args) {
		Class1 c = new Class1();
		c.f1(10);
		c.f2();
	}
}

Ok my problem:

public class Admin {
int nodes[][];
public int [][]input_nodes() throws IOException
    {    
//input values to the nodes array according to rules
return(nodes)
}
void add_nodes(int nodes[][]) throws IOException
    {    
//use the previously manipulated nodes array in this function
}
}

This is how I have been doing it:

public static void main(String[] args) throws IOException
    {
        Admin ad = new Admin();
switch(case)
{
case 1:
ad.input_nodes();
break;
case 2:
ad.add_nodes(ad.input_nodes());
break;
}

The problem with my code is when I select the case for input_nodes() it works fine, but when I select add_nodes() it first accesses input_nodes(), outputs the whole of that function and then goes on to add_nodes().
While the output I need is perfectly fine, there arises a redundancy in the function call of input_nodes()
I hope I was able to properly explain my problem.
How do I get rid of my problem?
Thanks in advance. :)

Ok my problem:

public class Admin {
int nodes[][];
public int [][]input_nodes() throws IOException
    {    
//input values to the nodes array according to rules
return(nodes);
}
void add_nodes(int nodes[][]) throws IOException
    {    
//use the previously manipulated nodes array in this function
}
}

This is how I have been doing it:

public static void main(String[] args) throws IOException
    {
        Admin ad = new Admin();
switch(case)
{
case 1:
ad.input_nodes();
break;
case 2:
ad.add_nodes(ad.input_nodes());
break;
}

The problem with my code is when I select the case for input_nodes() it works fine, but when I select add_nodes() it first accesses input_nodes(), outputs the whole of that function and then goes on to add_nodes().
While the output I need is perfectly fine, there arises a redundancy in the function call of input_nodes()
I hope I was able to properly explain my problem.
How do I get rid of my problem?
Thanks in advance. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.