1

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
2
  • 1
    override toString in Note class Commented May 8, 2018 at 5:29
  • 1
    It would be useful to see what you got and what you want in the question. Use the edit link below your question to add it. Commented May 8, 2018 at 5:29

3 Answers 3

3

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());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely no problem, and thanks for taking the time to explain it !
3

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

4 Comments

This is exactly what I am after! However, how would I get it to get and set the duration and pitch to multiple different values by looping it through the noteArray for the Note class?
You can use setduration and setpitch.
Could you elaborate please? I've tried putting a loop into the "setduration" part but I keep having trouble.
@Tipzil You need to drop static from many places and fix the contructor of Note class. See working demo here
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.