Java array want to print out all 10 banks but prints 0/tassignment.Branch@19821f?


Share |

want to be able to print out all ten banks at moment prints out 0/tassignment.Branch@19821f what code do i need to use to print all names in the array public class testBranch { public static void main(String[] args) { String branch0="london"; int numBranch=10; Branch[]...


Banks in Preston, ID



Answer (3):

 
?

in your Branch class, you need to overide the toString method. In other words, put a method in your Branch class that looks like this (but I've added a toString method:

You didn't post your branch class, so lets assume it looks like this:

public class Branch{
private String name;
private int id;
public Branch(String name, int id){
this.name = name;
this.id = id;
}

public String getName(){
return name;
}

public String toString(){
return "Branch Name: " + name + " Branch ID: " + id;
}

}

Adding the toString method as above will cause your print statement to work the way you want it to. Alternatively, you can see I also have a getName method. you could print out something like:
System.out.println(counter + "\t"+ branch[counter].getName());

Side Note: you have your /t backwards, should be \t to get a tab in the printout.

 
sudeep_mukherjee120579

The problem with your program is the function called toString() in java types like int, double etc this function is inbuilt, therefore when you print something of int type toString funtion is used to print your variable,
Unfortunately this is not the case with your class called Branch and java does not know how to print the string representation of your class.
Thus what you need to do is to override the toString() function in your Branch class. I have written a small program.(Branch class is an assumption just add the toString function in your original class)
class Branch
{
public int num;
public String name;
Branch(String branchName,int somenumber)
{
name=branchName;
num=somenumber;
}
public String toString()
{
String result="";
//change the following line to anything you want as an output when an object of this class is printed
result=name;
return result;
}

}
public class testBranch {

public static void main(String[] args) {
String branch0="london";
int numBranch=10;

Branch[] branch=new Branch[numBranch]; // declare branch as array of Branch

branch [0] = new Branch ("london ",3);
branch [1] = new Branch ("manchester ",3);
branch [2] = new Branch ("glasgow ",3);
branch [4] = new Branch ("burnley ",3);
branch [5] = new Branch ("blackburn ",3);
branch [6] = new Branch ("preston ",3);
branch [7] = new Branch ("clitheroe ",3);
branch [8] = new Branch ("bolton ",3);
branch [9] = new Branch ("wigan ",3);

for (int counter=0; counter< numBranch; counter++){ // determine Peter's total balance
//mind the escape sequence explaination below
System.out.println(counter + "\t"+ branch[counter]);

}
}
}

One more thing dear just change the /t in your original program to \t for horizontal tab in your output

cheers

 
codrguy

this totally depends on how you have defined the Branch class and specifically the fields it has. you need to access those fields in the for loop.