The Artima Developer Community
Sponsored Link

Java Answers Forum
Help Cleaning Code

13 replies on 1 page. Most recent reply: Dec 8, 2005 6:43 AM by Tim W

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Help Cleaning Code Posted: Dec 5, 2005 7:59 AM
Reply to this message Reply
Advertisement
Hi everyone,

I would appreciate if someone could help me out cleaning up the code below for a basic program i am doing. Im still very much a begginer but ive added comments to most things to make it easier. The code near the end of class is probably wrong so anyhelp on that would be apprecaited.

Heres the code, thanks for your time:

//
//  BasicMonitor.java
//  BasicMonitor
//
//
import java.util.*;
import javax.sound.midi*;
import javax.swing.*;
import static javax.sound.Midi.ShortMessage.*;
 
public class BasicMonitor {
 
       public static void main (String args[]) {
 
               // Poll system for midi devices
 
               MidiDevice.info[]devicelist = MidiSystem.getMidiDeviceInfo();
 
               // Ask user to select the input device
 
               MidiDevice.Info deviceInfo =
                       (MidiDevice.Info)
                       JOptionPane.showInputDialog (
                                                                                                                                        null,
                                                                                                                                        "Select MIDI Device",
                                                                                                                                        "Program name",
                                                                                                                                        JOptionPane.QUESTION_MESSAGE,
                                                                                                                                        null,
                                                                                                                                        deviceList,
                                                                                                                                        null);
 
               System.out.println ("Selected device = " + deviceInfo);
 
               // See if the user pressed cancel
 
               if (deviceInfo != null) {
                       try {
                               MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
                               device.open();
 
                               // Create the display window
 
                               MidiMonitorWindow win = new MidiMonitorWindow ("Tims Midi Monitor Window")
 
                                       // Create the message buffer
 
                                       MessageByteBuffer buffer = new MessageByteBuffer ();
 
                               // Create a reciever which puts message into buffer
 
                               MonitoringReciever r = new (buffer);
 
                               // Connect the reciever to the devices transmitter
 
                               transmitter t = device.getTransmitter ()
                                       t.SetReceiver (r);
 
 
                               // Setup infinite loop
                               while (true) {
                                       MidiMessage message = bufferObject.get ();
 
                                       // Check if message exists
                                       if (byteData != null) {
                                               byte [] byteData = message.getMessage ();
                                               for (byte b : byteData) {
                                                       int i = b & 0xFF;
                                                       System.out.print (i + " ");
                                               }
                                               System.out.println ();
                                       }
 
                                       // Get status byte and extract command and channel bits
                                       int status = byteData [0] &0xFF;
                                       int command = status & 0xF0;
                                       int channel = status & 0xF0;
 
                                       // Process message according to the command bits
                                       switch (command) {
                                               case NOTE_ON;
 
                                                       // Check if note on or alternative note off
                                                       if (byteData [2]!=0) {
                                                               win.ShowNoteon (channel, byteData [1], byteData [2] }
 
else
win.showNoteoff (channel, byteData [1], byteData [2] }
break
 
case Note_0ff;
win.ShowNoteoff (channel, byteData [1], byteData [2] }
 
case Pitch_Bend;
Win.show PitchBend (channel byteData [1], byteData [2] }
 
case Poly;
Win.show PolykeyPressure (channel byteData [1], byteData [2])
 
case Control_Change;
Win.show ControlChange (channel byteData [1], byteData [2])
 
case Program_Change;
Win.show ProgramChanage (channel byteData [1], byteData [2])
 
After Case;
Default:
 
// System message, is it one were interested in?
if (status == Active_sensing)(
                                                                                                                  win.showActiveSensing();
                                                          {
                                                              else if
                                                              Timing == Clock
                                                              win.timingClock();
 
       else
                                                                System == Exclusive
                                                                 win.systemExlusive();
 
         thread yeild ();
 
        catch (MidiUnavailableException e) {
                                                                                                                   System.out.println ("Exception thrown " + e);
                                                                                                                                       }
                                                                                                                       }


Thanks


Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: Help Cleaning Code Posted: Dec 5, 2005 8:01 AM
Reply to this message Reply
Sorry didnt realsie i double posted.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help Cleaning Code Posted: Dec 5, 2005 11:56 PM
Reply to this message Reply
Your switch case statement is a mess. The main error is that you put a ";" where a ":" belongs.

These are some examples how it should be done

switch (x) {
    case a:
        if (condition) {
            //do something;
        } else {
            //do something else;
        }
        break;
    case b: //don't do anything
        break;
    case c:
    case d: //this can be used as OR statement
        //do something;
        break;
    default:
        //do something;
}

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: Help Cleaning Code Posted: Dec 6, 2005 6:41 AM
Reply to this message Reply
Thanks im not to sure what im doing really. But i will download an editor and try and sort it all out.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help Cleaning Code Posted: Dec 6, 2005 7:06 AM
Reply to this message Reply
Yes, that helps at times ;)

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: Help Cleaning Code Posted: Dec 6, 2005 10:42 AM
Reply to this message Reply
Ive downloaded BlueJ and im using that as it is easy to understand.

Im still having problems correcting some of the code mainly because i havent formatted or indented it well enough and im not sure how to do this.

Would you be able to look at the updated code for me and not fix the errors but just re-format it for me.

The code is below, thanks:

//
//  BasicMidiMonitor.java
//  BasicMidiMonitor
//
//  Created by Timothy Willis on 02/12/2005.
//  Copyright (c) 2005 __MyCompanyName__. All rights reserved.
//
//
//  BasicMidiMonitor.java
//  BasicMidiMonitor
//
//
import java.util.*;
import javax.sound.midi*;
import javax.swing.*;
import static javax.sound.Midi.ShortMessage.*;
 
public class BasicMidiMonitor {
 
       public static void main (String args[]) {
 
               // Poll system for midi devices
 
               MidiDevice.info[]devicelist = MidiSystem.getMidiDeviceInfo();
 
               // Ask user to select the input device
 
               MidiDevice.Info deviceInfo =
                       (MidiDevice.Info)
                       JOptionPane.showInputDialog (
                                                    null,
                                                    "Select MIDI Device",
                                                    "Program name",
                                                    JOptionPane.QUESTION_MESSAGE,
                                                    null,
                                                    deviceList,
                                                    null);
 
               System.out.println ("Selected device = " + deviceInfo);
 
               // See if the user pressed cancel
 
               if (deviceInfo != null) {
                       try {
                               MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
                               device.open();
 
                               // Create the display window
 
                               MidiMonitorWindow win = new MidiMonitorWindow ("Tims Midi Monitor Window")
 
                                       // Create the message buffer
 
                                       MessageByteBuffer buffer = new MessageByteBuffer ();
 
                               // Create a reciever which puts message into buffer
 
                               MonitoringReciever r = new (buffer);
 
                               // Connect the reciever to the devices transmitter
 
                               transmitter t = device.getTransmitter ()
                                       t.SetReceiver (r);
 
 
                               // Setup infinite loop
                               while (true) {
                                       MidiMessage message = bufferObject.get ();
 
                                       // Check if message exists
                                       if (byteData != null) {
                                               byte [] byteData = message.getMessage ();
                                               for (byte b : byteData) {
                                                       int i = b & 0xFF;
                                                       System.out.print (i + " ");
                                               }
                                               System.out.println ();
                                       }
 
                                       // Get status byte and extract command and channel bits
                                       int status = byteData [0] &0xFF;
                                       int command = status & 0xF0;
                                       int channel = status & 0xF0;
                                       system.out.print (status + " " + command + " " + channel);
 
                                       // Process message according to the command bits
                                       switch (command) {
                                               case NOTE_ON:
 
                                                       // Check if note on or alternative note off
                                                       if (byteData [2]!=0) {
                                                               win.ShowNoteon (channel, byteData [1], byteData [2] }
 
else
win.showNoteoff (channel, byteData [1], byteData [2]);
break
 
case Note_0ff; 
win.ShowNoteoff (channel, byteData [1], byteData [2]);
 
case Pitch_Bend;
Win.show PitchBend (channel byteData [1], byteData [2]);
 
case Poly;
Win.show PolykeyPressure (channel byteData [1], byteData [2]);
 
case Control_Change;
Win.show ControlChange (channel byteData [1], byteData [2]);
 
case Program_Change;
Win.show ProgramChanage (channel byteData [1], byteData [2]);
 
After Case;
Default:
 
// System message, is it one were interested in?
if (status == Active_sensing)(
                              win.showActiveSensing();
                                {
                                 else if
                                     Timing == Clock
                                   win.timingClock();
 
                   else
                           System == Exclusive
                                   win.systemExlusive();
                                }
                                   Thread.yeild ();
 
                                   catch (MidiUnavailableException e) {
                                           System.out.println ("Exception thrown " + e);
                                   }
                   }
 

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help Cleaning Code Posted: Dec 6, 2005 10:56 PM
Reply to this message Reply
I can't format the code with so many errors in it.
As long as you don'T set the brackets right, it's no possible.

This is what NeBeans spits out after the auto-format.
//  BasicMidiMonitor.java
//  BasicMidiMonitor
//
//  Created by Timothy Willis on 02/12/2005.
//  Copyright (c) 2005 __MyCompanyName__. All rights reserved.
//
//
//  BasicMidiMonitor.java
//  BasicMidiMonitor
//
//
        import java.util.*;
import javax.sound.midi*;
import javax.swing.*;
import static javax.sound.Midi.ShortMessage.*;
 
public class BasicMidiMonitor {
    
    public static void main(String args[]) {
        
        // Poll system for midi devices
        
        MidiDevice.info[]devicelist = MidiSystem.getMidiDeviceInfo();
        
        // Ask user to select the input device
        
        MidiDevice.Info deviceInfo =
                (MidiDevice.Info)
                JOptionPane.showInputDialog(
                null,
                "Select MIDI Device",
                "Program name",
                JOptionPane.QUESTION_MESSAGE,
                null,
                deviceList,
                null);
        
        System.out.println("Selected device = " + deviceInfo);
        
        // See if the user pressed cancel
        
        if (deviceInfo != null) {
            try {
                MidiDevice device = MidiSystem.getMidiDevice(deviceInfo);
                device.open();
                
                // Create the display window
                
                MidiMonitorWindow win = new MidiMonitorWindow("Tims Midi Monitor Window")
                
                // Create the message buffer
                
                MessageByteBuffer buffer = new MessageByteBuffer();
                
                // Create a reciever which puts message into buffer
                
                MonitoringReciever r = new (buffer);
                
                // Connect the reciever to the devices transmitter
                
                transmitter t = device.getTransmitter()
                t.SetReceiver(r);
                
                
                // Setup infinite loop
                while (true) {
                    MidiMessage message = bufferObject.get();
                    
                    // Check if message exists
                    if (byteData != null) {
                        byte [] byteData = message.getMessage();
                        for (byte b : byteData) {
                            int i = b & 0xFF;
                            System.out.print(i + " ");
                        }
                        System.out.println();
                    }
                    
                    // Get status byte and extract command and channel bits
                    int status = byteData [0] &0xFF;
                    int command = status & 0xF0;
                    int channel = status & 0xF0;
                    system.out.print(status + " " + command + " " + channel);
                    
                    // Process message according to the command bits
                    switch (command) {
                        case NOTE_ON:
                            
                            // Check if note on or alternative note off
                            if (byteData [2]!=0) {
                                win.ShowNoteon(channel, byteData [1], byteData [2] }
                            
                            else
                                win.showNoteoff(channel, byteData [1], byteData [2]);
                            break
                                    
                        case Note_0ff;
                        win.ShowNoteoff(channel, byteData [1], byteData [2]);
                        
                        case Pitch_Bend;
                        Win.show PitchBend(channel byteData [1], byteData [2]);
                        
                        case Poly;
                        Win.show PolykeyPressure(channel byteData [1], byteData [2]);
                        
                        case Control_Change;
                        Win.show ControlChange(channel byteData [1], byteData [2]);
                        
                        case Program_Change;
                        Win.show ProgramChanage(channel byteData [1], byteData [2]);
                        
                        After Case;
                        Default:
                            
// System message, is it one were interested in?
                            if (status == Active_sensing)(
                                    win.showActiveSensing();
                            {
                                else if
                                        Timing == Clock
                                        win.timingClock();
                                
                                else
                                    System == Exclusive
                                            win.systemExlusive();
                            }
                                    Thread.yeild();
                                    
                                    catch (MidiUnavailableException e) {
                                        System.out.println("Exception thrown " + e);
                                    }
                    }


Start with something simple. Learn how to use Java.

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: Help Cleaning Code Posted: Dec 8, 2005 5:20 AM
Reply to this message Reply
Just needed to spend some time at it instead of being lazy. Ive basically sorted it out now. I think i have a couple of errors to do with curly brackets. Look ok to you now?

 
 
public class BasicMonitor {
 
       public static void main (String [] args) {
 
               // Poll system for midi devices
 
               MidiDevice.Info [] deviceList = MidiSystem.getMidiDeviceInfo ();
 
               // Ask user to select the input device
 
               MidiDevice.Info deviceInfo =
                       (MidiDevice.Info)
                       JOptionPane.showInputDialog (
                                                                                                                                        null,
                                                                                                                                        "Select MIDI Device",
                                                                                                                                        "Program name",
                                                                                                                                        JOptionPane.QUESTION_MESSAGE,
                                                                                                                                        null,
                                                                                                                                        deviceList,
                                                                                                                                        null);
 
               System.out.println ("Selected device = " + deviceInfo);
 
               // See if the user pressed cancel
 
               if (deviceInfo != null) {
                       try {
                               MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
                               device.open();
 
                               // Create the display window
                               MidiMonitorWindow win = new MidiMonitorWindow ("Midi Monitor");
 
                               // Create the message buffer
                               MessageByteBuffer buffer = new MessageByteBuffer();
 
                               // Create a receiver which puts message into buffer
                               MonitoringReceiver r = new MonitoringReceiver (buffer);
 
                               // Connect the receiver to the devices transmitter
                               Transmitter t = device.getTransmitter();
                               t.setReceiver(r);
 
                               // Setup infinite loop
                               while(true) {
 
                                       MidiMessage message = buffer.get();
 
                                       // Check if message exists
 
                                       if (byteData != null) {
                                         byte [] byteData = message.getMessage();
                                               for (byte b: byteData){
                                                       System.out.print(b + " ");
                                               }
                                               System.out.print("");
                                       }
 
                                       // Get status bytes and extract command and channel bits
 
                                       int Status = byteData [0] & 0xFF;
                                       int Command = Status & 0xF0;
                                       int Channel = Status & 0x0f;
 
                                       System.out.print(Status +" " + Command + " " + Channel);
 
                                       // Process message according to command bits
 
                                       switch (Command) {
                                               case NOTE_ON:
 
                                       // Check if note on or alternative note off
 
                                                       if (byteData[2] != 0) {
                                                               win.showNoteOn(Channel, byteData[1], byteData[2]);
                                                       }
                                                       else{
                                                               win.showNoteOff(Channel, byteData[1], byteData[2]);
                                                       }
                                                       break;
 
                                               case NOTE_OFF:
                                                       win.showNoteOff(Channel, byteData[1], byteData[2]);
                                                       break;
 
                                               case PITCH_BEND:
                                                       win.showPitchBend(Channel, byteData[1], byteData[2]);
                                                       break;
 
                                               case POLY_PRESSURE:
                                                       win.showPolyKeyPressure(Channel, byteData[1], byteData[2]);
                                                       break;
 
                                               case CONTROL_CHANGE:
                                                       win.showControlChange(Channel, byteData[1], byteData[2]);
                                                       break;
 
                                               case PROGRAM_CHANGE:
                                                       win.showProgramChange(Channel, byteData[1]);
                                                       break;
 
                                               case CHANNEL_PRESSURE:
                                                       win.showChannelPressure(Channel, byteData[1]);
                                                       break;
 
                                               default:
                                                       if(Status == ACTIVE_SENSING){
                                                               win.showActiveSensing();
                                                       }
                                                       else if (Status == TIMING_CLOCK){
                                                               win.showTimingClock();
                                                       }
                                                       else if (Status == SYSTEM_EXCLUSIVE){
                                                       win.showSystemExclusive;
                                                       }
                                                       else
                                                               win.showOtherMessage();
                                       }
                                       Thread.yield ();
                               }
                       }
                       catch (MidiUnavailableException e) {
                               System.out.println ("Exception thrown " + e);
                       }
               }
       }
 

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help Cleaning Code Posted: Dec 8, 2005 5:50 AM
Reply to this message Reply
Much better.

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: Help Cleaning Code Posted: Dec 8, 2005 6:17 AM
Reply to this message Reply
Can you see any errors? Ive apparently missing a curly bracket but i dont see where?

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help Cleaning Code Posted: Dec 8, 2005 6:31 AM
Reply to this message Reply
> Can you see any errors? Ive apparently missing a curly
> bracket but i dont see where?

The compiler is not always accurate. Actually you're
missing a statement terminator ";"

MidiMonitorWindow win = new MidiMonitorWindow ("Tims Midi Monitor Window")

Place it at the end then repost... Do yourself a favor,
download Eclipse or NetBeans. Brace Matching and writing
code in general will be much easier in these two environments.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help Cleaning Code Posted: Dec 8, 2005 6:34 AM
Reply to this message Reply
Oh my baad, you've included it in your last posting...

You should still get yourself a decent IDE. It does minor
things like brace matching to make your life easier.
Personally I recommend Eclipse.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Help Cleaning Code Posted: Dec 8, 2005 6:40 AM
Reply to this message Reply
Another word of caution, from a style perspective if you're
trying to keep track of braces around your blocks of code,
don't introduce too many unnecessary new-line spaces,
don't you think its easier to track of:
if(flag){
   perform_a();
   perform_b();
   while(pending()){
      System.out.println("pending");
   }
}

As opposed to:
if(flag){
 
   perform_a();
   perform_b();
 
   while(pending()){
      System.out.println("pending");
   }
}


and the line where you instantiate a JOptionPane doesn't
have to take more than 1 or 2 lines, you take up 5.

Just a few suggestions to assist you to manage your code.

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: Help Cleaning Code Posted: Dec 8, 2005 6:43 AM
Reply to this message Reply
Thanks for your advice i'll take that onboard.

Flat View: This topic has 13 replies on 1 page
Topic: calling a stored procedure on AS400 Previous Topic   Next Topic Topic: need help connecting to AS400

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use