Tipzil
Tipzil

Reputation: 19

Trying to loop through an array within an array

I am trying to print each part of my noteArray (eg: 19, and then "D" as separate parts) But by using a For loop I get an a mumble up print message for each line. The "processNotes(noteArray)" method is how I want my output to look.

Any help would be much appreciated!

public class question2 {
public static void main(String[] args) {
    Note[] noteArray = new Note[5];
    noteArray[0] = new Note(19, "D");
    noteArray[1] = new Note(10, "C");
    noteArray[2] = new Note(23, "F");
    noteArray[3] = new Note(20, "B");
    noteArray[4] = new Note(32, "C");
    processNotes(noteArray);
    for(Note i : noteArray){
        System.out.println(i);
        }
}
private static void playNote() {
    int numberDuration = Note.getduration();
    String letterPitch = Note.getpitch();
    System.out.println("The note "+ letterPitch +" is played for "+ 
numberDuration +" seconds.");
    return;
}
public static void processNotes(Note[] notes) {
    playNote();
}
}
class Note
{
private static String pitch;
private static int duration;
public Note(int duration, String pitch) {
    this.pitch = "C";
    this.duration = 10;
}
public static int getduration() {
    return duration;
}
public void setduration(int duration) {
    Note.duration = duration;
}
public static String getpitch() {
    return pitch;
}
public void setpitch(String pitch) {
    Note.pitch = pitch;
}
}

EDIT:

Output I would like: The note C is played for 10 seconds.

Output of arrays I get:

Note@6d06d69c
Note@7852e922
Note@4e25154f
Note@70dea4e
Note@5c647e05

Upvotes: 1

Views: 111

Answers (3)

Gyapti Jain
Gyapti Jain

Reputation: 4106

Add the following to your Note class:

public String toString() {
    return "Duration = " + duration + ", pitch = " + pitch;
}

Demo


From object.toString:

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

You can override this method for a more meaningful output.

Suggested further read: The connection between 'System.out.println()' and 'toString()' in Java

Upvotes: 3

Lutzi
Lutzi

Reputation: 412

You have two possibility.

First, override your toString() method so that it prints your notes as you want when you System.out.println().

Second, you can in your loop, instead of printing the note :

for(Note i : noteArray){
    System.out.println(i.getPitch());
    System.out.println(i.getDuration());
}

Upvotes: 3

Suvansh
Suvansh

Reputation: 264

You can just override toString method of the Note class, as sysout implicitly call toString.

Upvotes: 1

Related Questions