8 Replies - 413 Views - Last Post: 05 May 2013 - 06:29 PM Rate Topic: -----

#1 tc356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 13

music box using parallel arrays

Posted 04 May 2013 - 08:53 AM

I'm tring to get the dragon board to play a song using parallel arrays. All of my errors are at static void beep(String note, char length). Not sure what I need to do to fix it.
    import java.io.*;
    import java.util.*;

    public class MusicBox
    {
	public static void main(String[] args)
	{
		MCU mcu = null;

		try
		{
			mcu = new MCU(new Communicator(args[0]));
		}

		catch (IOException e)
		{
			System.out.println(e);
		}

		mcu.println("Press sw2",0);
		mcu.println("to play song.",1);
		int more = null;

		do
		{

			String[] notes = {"E4","D4","C4","D4","E4","E4","E4","D4","D4","D4","E4","G4","G4","E4","D4","C4","D4","E4","E4","E4","E4","D4","D4","E4","D4","C4"};

			char[] lengths = {'Q','Q','Q','Q','Q','Q','H','Q','Q','H','Q','Q','H','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','W'};


			for(int i=0; i< note.length; i++)
			{

				static void beep(String note, char length)
				{
					int frequency, nLength;



					switch(note)
					{
						case "C4":
							frequency = 523;
						break;

						case "D4":
							frequency = 587;
						break;

						case "E4":
							frequency = 659;
						break;

						case "G4":
							frequency = 784;
						break;

						default:
							frequency = 0;
						break;
					}

					switch(length)
					{
						case'Q':
							nLength = 250;
						break;

						case'H':
							nLength = 500;
						break;

						case'W':
							nLength = 1000;
						break;

						default:
							nLength = 0;
						break;
					}

					mcu.sound_on(frequency);
					mcu.msDelay(nLength);
					mcu.sound_off();
				}
			}
		}while(more.equals("4"));
	}
   }



Is This A Good Question/Topic? 0
  • +

Replies To: music box using parallel arrays

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: music box using parallel arrays

Posted 04 May 2013 - 09:59 AM

I won't even read your code. You should not be using parallel arrays, and you have not shared what the error is.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: music box using parallel arrays

Posted 04 May 2013 - 02:22 PM

Post your errors
but one thing for sure


    static void beep(String note, char length)
    {


in Java, you can't, as you do here, declare a method in the middle of another method
Was This Post Helpful? 0
  • +
  • -

#4 tc356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 13

Re: music box using parallel arrays

Posted 04 May 2013 - 03:31 PM

View Postpbl, on 04 May 2013 - 02:22 PM, said:

Post your errors
but one thing for sure


    static void beep(String note, char length)
    {


in Java, you can't, as you do here, declare a method in the middle of another method



I knew I was missing something. Sorry about that. These are all on same line of code.
static void beep(String note, char length)

error 1: illegal start of expression >static

error 2: illegal start of expression >void

error 3: ';'expected

error 4: <identifier> expected

error 5: not a statement

error 6: ';' expected
Was This Post Helpful? 0
  • +
  • -

#5 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: music box using parallel arrays

Posted 04 May 2013 - 03:54 PM

View Posttc356, on 04 May 2013 - 03:31 PM, said:

I knew I was missing something. Sorry about that. These are all on same line of code.
static void beep(String note, char length)

error 1: illegal start of expression >static

error 2: illegal start of expression >void

error 3: ';'expected

error 4: <identifier> expected

error 5: not a statement

error 6: ';' expected


correct me if i'm wrong but it looks to me that your errors are caused by you trying to declare a method inside of another method. As pbl said, you can't do that. It's expecting an expression so it's giving you errors telling you that that's not how an expression works. Look into Java Expressions if you're having trouble with the difference.

Maybe try to declare frequency and nLength outside of main. Declare your beep method outside of main. Then inside your main method you'll want to call
beep(note,length);
for each note and the length of each note.
Was This Post Helpful? 0
  • +
  • -

#6 tc356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 13

Re: music box using parallel arrays

Posted 05 May 2013 - 08:55 AM

Here is my new code and new errors. I appreciate the help! Thank you very much.

import java.io.*;
import java.util.*;

public class MusicBox
{
String[] notes = {"E4","D4","C4","D4","E4","E4","E4","D4","D4","D4","E4","G4","G4","E4","D4","C4","D4","E4","E4","E4","E4","D4","D4","E4","D4","C4"};

char[] lengths = {'Q','Q','Q','Q','Q','Q','H','Q','Q','H','Q','Q','H','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','W'};

	static void beep(String note, char length)
	{
		int frequency, nLength;

		for(int i=0; i<notes.length(); i++)
		{

			switch(note)
			{
				case "C4":
					frequency = 523;
				break;

				case "D4":
					frequency = 587;
				break;

				case "E4":
					frequency = 659;
				break;

				case "G4":
					frequency = 784;
				break;

				default:
					frequency = 0;
				break;
			}

			switch(length)
			{
				case'Q':
					nLength = 250;
				break;

				case'H':
					nLength = 500;
				break;

				case'W':
					nLength = 1000;
				break;

				default:
					nLength = 0;
				break;
			}

		}
	}
	public static void main(String[] args)
	{
		MCU mcu = null;

		try
		{
			mcu = new MCU(new Communicator(args[0]));
		}

		catch (IOException e)
		{
			System.out.println(e);
		}

		mcu.println("Press sw2",0);
		mcu.println("to play song.",1);
		int more = 4;

		do
		{





				beep(note,legnth);
				mcu.sound_on(frequency);
				mcu.msDelay(nLength);
				mcu.sound_off();
				sw1 = mcu.getSwitch();

		}
		while(more ==4);
	}
}



error1: in for loop, non-static variable notes cannot be referenced for static context

error2: cannot find symbol in for loop

error3: cannot find symbol at beep(note,length);

the other errors are where I'm call mcu. I thing those will go away after fixing these errors.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: music box using parallel arrays

Posted 05 May 2013 - 11:04 AM

Again post you error as they are displayed.
The message displayed by the compiler include the line number, that allows to fic the problem faster

The variables notes and length belong to a MusixBox object
You hhaven't created any MusicBox object

The only method that should be static in your program is the main() method
This method should create a MusicBox and then access it's methods
A main() mehod should only have a few lines.. something like


        MCU mcu;

        MusicBox(MCU mcu) {
            this.mcu = mcu;
        }

        void start() {

		mcu.println("Press sw2",0);
		mcu.println("to play song.",1);
		int more = 4;

		do
		{

				beep(note,legnth);
				mcu.sound_on(frequency);
				mcu.msDelay(nLength);
				mcu.sound_off();
				sw1 = mcu.getSwitch();

		}
		while(more ==4);
         }


	void beep(String note, char length)
	{
		int frequency, nLength;

		for(int i=0; i<notes.length(); i++)
...
			}

		}
	}
	public static void main(String[] args)
	{

		try
		{
			MCU mcu = new MCU(new Communicator(args[0]));
                        MusicBox mb = new MusicBox(mcu);
                        mb.start();
		}

		catch (IOException e)
		{
			System.out.println(e);
		}



Was This Post Helpful? 0
  • +
  • -

#8 tc356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 13

Re: music box using parallel arrays

Posted 05 May 2013 - 05:06 PM

I totally re-did my code. I only have two errors now which is much better then what I had before. Any tips are greatly appreciated.
import java.io.*;
import java.util.*;

public class MusicBox
{

	public static void main(String[] args)
	{
		MCU mcu = null;

		try
		{
			mcu = new MCU(new Communicator(args[0]));
			MusicBox mb = new MusicBox();
		}

		catch (IOException e)
		{
			System.out.println(e);
		}

		String[] notes = {"E4","D4","C4","D4","E4","E4","E4","D4","D4","D4","E4","G4","G4","E4","D4","C4","D4","E4","E4","E4","E4","D4","D4","E4","D4","C4"};

		char[] lengths = {'Q','Q','Q','Q','Q','Q','H','Q','Q','H','Q','Q','H','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','Q','W'};

		int frequency, nLength;


		for(int n=0; n<notes.length(); n++)
		{

			switch(notes[n])
			{
				case "C4":
					frequency = 523;
				break;

				case "D4":
					frequency = 587;
				break;

				case "E4":
					frequency = 659;
				break;

				case "G4":
					frequency = 784;
				break;

				default:
					frequency = 0;
				break;
			}
		}

		for(int l=0; l<notes.length(); l++)
		{
			switch(lengths[l])
			{
				case'Q':
					nLength = 250;
				break;

				case'H':
					nLength = 500;
				break;

				case'W':
					nLength = 1000;
				break;

				default:
					nLength = 0;
				break;
			}

		}

		mcu.println("Press sw2",0);
		mcu.println("to play song.",1);
		int more = 4;

		do
		{





				mcu.sound_on(frequency);
				mcu.msDelay(nLength);
				mcu.sound_off();
				int sw1 = mcu.getSwitch();

		}
		while(more == 4);
	}
}



errors:
C:\unzipped\NewestJava64[1]\NewestJava\Java\MusicBox.java:29: error: cannot find symbol
for(int n=0; n<notes.length(); n++)
^
symbol: method length()
location: variable notes of type String[]
C:\unzipped\NewestJava64[1]\NewestJava\Java\MusicBox.java:56: error: cannot find symbol
for(int l=0; l<notes.length(); l++)
^
symbol: method length()
location: variable notes of type String[]
2 errors

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: music box using parallel arrays

Posted 05 May 2013 - 06:29 PM

a main() method should have a few lines... typically 2-5

so I complelty wasted my time trying to shouw you how rto code in Java
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1