Mitch Krah

Ranch Hand
+ Follow
since Sep 06, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mitch Krah

The noise is a definate "you messed up" DONG! I'll bet it is a JBuilder thing.

However, I tried your "write a value that is not possible at the end of the recording, then check for that value when reading" suggestion and it worked great (after I updated some of my code that was not compatible in the "for" loop. The program works great.

I like your code. It is much more simple than what I had. Looks good. I will (also) try that option. But, you got me through the tough part. Raelly appreciate the help. Hope you had a great Easter. I am good now.

Thank you,
Mitch
19 years ago
The light went on! It is a string and I am getting the length of the file name. Wow! That is a relief.

I like your suggestion of catching the EOFException and doing nothing. How do I catch the exception and keep it from DONG!ing me?

Also, I will see about implementing your write -1 at the end of the file.

Thank you,
Mitch

You have provided much, much help!
19 years ago
Thank you. Yes, I noticed that at the end of the read I get (and catch) the EOFException. However, I do not think this is going to be acceptable. I am recording buttons (with associated notes that have been assigned by the listener) and playing them back from a file. I do not know how to elligantly handle the EOFexception? Currently I get a ugly noise and I print out the exception.

I have tried while and for loops to quit at the end of the file and currently am using the following for loop:

try {
System.out.println("Length of file " + s + " is: " + s.length());

for(i = 0; i < s.length(); i++) {
count = dataIn.readInt();
syn.playNote(50, count, 1000);

}
}
catch (IOException exc) {
System.out.println("Read error. " + exc);
}

However, my s.length() keeps returning 8 even when I have 5 or 10 elements. I can't understand what is going on. I printed "s" and "s.length()" to verify that I am getting the length of the same file I have chosen from JFileChooser. And, they are the same. Also, I have opened the selected file (in notepad) and have verified that the # of elements is what I expect (5 or 10), but s.length() still states "8"???

Any suggestions?
19 years ago
Happy Easter! Things are going much better with my program. Please ignore my previous posting. Much has been figured out. I kept reading your suggestions and eventually started to simplify my program to meet your suggestions. Most of the program is working as per requirements. Thank you!

Except;
When reading back from the file, at the end of the file I get an IOException. My program for this part is as follows:

else if (e.getSource() == b[2]) {

jfc = new JFileChooser();
jfc.showDialog(null, "Select a file to open");

try{
dataIn = new DataInputStream(new FileInputStream(jfc.getSelectedFile()));
}
catch(IOException ex) {
System.out.println("Cannot open file. " + ex);
return;
}

try {
count = dataIn.readInt();
while(count != -1) {
syn.playNote(50, count, 1000);
count = dataIn.readInt();
}
}
catch (IOException exc) {
System.out.println("Read error. " + exc);
}
try {
dataIn.close();
}
catch (IOException ex2) {
System.out.println("Read error closing. " + ex2);
}
}

Is there something wrong with my while statement? Doesn't the return value = -1 when the end of the file is reached? Any suggestions/help is appreciated. Note the file is being read and the notes/buttons are being played (that were previously recorded). It is at the end of the file that throws the exception.

Thank you.
19 years ago
OK. I am back. I have completely revamped the program. Taking into account your suggestions and others (e.g. treat the buttons as an integer for recording and playback). I am having problems getting the program to write to a file. When I input an existing file or a new file I get "Write error"/"FileNotFound"/"BadFileDescriptor". I am pasting my ActioPerformed method. Any suggestions/help is appreciated.

public void actionPerformed (ActionEvent e) {

if (e.getSource() == b[0]) {
s = JOptionPane.showInputDialog(null, "Please enter file name: ");
recording = true;
try{
dataOut = new DataOutputStream(new FileOutputStream(s));
}
catch(IOException ex) {
System.out.println("Cannot open file." + ex);
return;
}

}

else if (e.getSource() == b[1]) {
recording = false;

}

else if (e.getSource() == b[2]) {

jfc = new JFileChooser();
jfc.showDialog(null, "Select a file to open");

try{
dataIn = new DataInputStream(new FileInputStream(jfc.getSelectedFile()));
}
catch(IOException ex) {
System.out.println("Cannot open file. " + ex);
return;
}
try {
count = dataIn.readInt();
while(count != -1) {
syn.playNote(50, count, 1000);
}
}
catch (IOException exc) {
System.out.println("Read error. " + exc);
}
try {
dataOut.close();
}
catch (IOException ex2) {
System.out.println("Read error. " + ex2);
}


}

for (count = 0; count < 128; count++) {
if (e.getSource() == buttons[count])
syn.playNote(50, count, 1000);
if (recording) {

try{
dataOut.writeInt(count);
}
catch(IOException ex) {
System.out.println("Write error. " + ex);
}
try {
dataOut.close();
}
catch (IOException ex1) {
System.out.println("Output error. " + ex1);
}

}

}

}



Sorry it is so long.

Thank you,
Mitch
19 years ago
Thank you for the great replys/suggestions. I will review and try to implement your suggestion(s). Unfortunately, I am out on business all week and will not be able to get back to you until Friday. Please don't think I am ignoring you. I will give you a status this weekend.

Don't forget me.

Thank you again for your help.

Mitch
19 years ago
Thank you for your reply.

I am not familiar with the flag set function (new at this Java stuff), and am pretty sure that is not how the program is suppose to work. The information I am trying to record is the sequence of the buttons pushed (which calls the synth class that plays the sound until the "stop" record button is pressed. Then, I am suppose to be able to "replay" the stored file and have it play the sequence/notes recorded. I thought that a "button" in a frame resulted in an button object, therefore I was trying to record this information via ObjectOutputStream. Now I am not to sure.

I have already set up listeners for all the buttons (record, stop, and replay along with the 128 buttons for individual notes - per the synth class). I guess I am still confused about how to proceed. Record objects? I would like to post the whole program and see what you think (by the way, I have already verified the button to note part of the program, now I am trying to add in the I/O part - write to a file and read from a file), but it is 125 lines long and I do not know how readable that would be.

Any additional suggestions are appreciated.

Thank you,
Mitch
19 years ago
I'm stuck.

I have a Frame with 2 panels. The first panel has 127 buttons that play a note when selected. The second panel has 3 buttons (record, stop, and replay). When the record button is pressed the program is to record the (note) buttons until the stop is selected. This is where I am having the problem. I am treating the button presses as an object and using the following code to try to record (at the actionPerformed method):

public void actionPerformed (ActionEvent e) {
int count;
for(count = 0; count < 128; count++) {
if(e.getSource() == buttons[count])
syn.playNote(50, count, 1000);
}

if(e.getSource() == b[0]){
String s = JOptionPane.showInputDialog(null, "Please enter file name: ");

// b[0] is the record button

try {
while (e.getSource() != b[1]) {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(s));

// b[1] is the stop button

out.writeObject(buttons[count]);
continue;
}
}
catch (IOException ex) {
System.out.println(ex);
}

if (e.getSource() == b[1])
out.close();

}

else if (e.getSource() == b[2])

// b[2] is to be the replay button - no code as of yet

;

}

Many questions. Am I correct to try to treat the buttons as objects? How do I loop writing each "note" button without asking for the file name each time? What am I doing wrong with out.close so that it cannot be recognized? etc.?

Any guidance is appreciated. Just to get me started on the correct path. I have been staring at this part of the code (and changing it so often), that I can't realize even what my next step should be?

Thank you.
19 years ago
OOPS! You are so right. The program is now working pretty good. I need to figure out how to convert the time I am getting into seconds and things should be better.

I will definately try your suggestions (when I get some time). Your ideas do appear to be much more LOGICAL, then what I did.

Thank you very much.

Also, I will bookmark the Collections tutorial and see if it helps.

Thank you again.

Mitch
19 years ago
Thank you for the suggestion. I tried to incorporate your suggestion, but ended up messing things up. I will try again later, after I see if I can get my current program to run.

Another question:

When I pick the option to push the getTime to the S stack, I notice that the same "date/time" is sent everytime to the stack I see in the debugger that something is being sent to the stack, but it is the same value everytime. Any ideas on why I am not getting current time each instance that I push? The following is the part of the code that pushes the "date/time":

public static void main(String[] args) {
Date now = new Date();
int n;
Stack S = new Stack();
long stackTime, currentTime, diff;

n = getInput();

while (n <= 4 && n > 0) {
switch (n) {
case 1: S.push(new Long (now.getTime()));

break;
19 years ago
Wow! That was not inherently obvious. I tried your suggestion and it worked great.

However, now I keep getting times of 0. I will have to do some debugging and check the values. But, thank you very much for your help.

Mitch
19 years ago
OK. I guess I am just not getting the syntax (I even looked up Java doc). I tried:

Long.longValue(S.pop());

But, I get "cannot resolve longValue method of java.lang.Long?

What am I missing here?

Thank you.
19 years ago
I am trying to load a time stamp into a stack using the push method. However, when I try to push the .getTime onto the stack I get the following error:

cannot be applied to (long). So, I had to use Long (new *.getTime()).

Also, I want to give the amount of time the element was in the stack. So, when I pop I want to subtract the current time from the time that was input on the stack to get the time on the stack. This gives me an error, "operator - cannot be applied".

The following is my code.

Date now = new Date();
int n;
Stack S = new Stack();

n = getInput();


switch (n) {
case 1: S.push(new Long (now.getTime()));
n = getInput();
break;
case 2: if (S.isEmpty())
System.out.println("No sausage left in the tray");
System.out.println("You just got a sausage from the tray that is "

// here is where the trouble starts! - operator on S.pop()

+ (new Long (now.getTime()) - S.pop()) + " seconds old");
n = getInput();
break;

Any help/suggestions would be appreciated.
19 years ago
I do not think synchronization will meet the intent of the program. The threads are suppose to be "racing" and the first one to finish is to exit the program so that the wining thread can be identified. Synchronization (my understanding) uses monitor and lock to hold a resource until the current thread is complete.

Or, am I not understanding synchronization?

Any additional guidance is appreciated.
I am writing a multiThread program. The object of the exercise is to create a threaded class that counts/displays to 10,000. Write a main driver that instantiates 10 instances and let them race. The first thread that completes and returns, I am to invoke System.exit() and determine whiech thread won.

My problem is that unless I put a sleep value of 100 or greater I get multiple threads identifying that they were the first to finish before the System.exit is exercised. The following is my code (that works because of the sleep of 100 ms - only counted to 100, because it would take to long to get to 10,000 for results). When the 100 ms sleep is removed I get multiple threads declaring they finished first.

Anyone have any guidance?

class Racers implements Runnable {
int count, n;
Thread thrd;


// Construct a new thread
Racers(String thrdName) {
thrd = new Thread(this, thrdName);
count = 1;
thrd.start(); // start the thread


}

// Begin execution of the new thread
public void run() {
System.out.println("Child Thread " + thrd.getName() + " starting.");
try {
do {
Thread.sleep(100);
System.out.println("In Child Thread " +thrd.getName() + ", count is " + count);
count++;
}while (count <= 100);
System.out.println("Child Thread " + thrd.getName() + " finished first and Won!");
System.exit(0);



}

public class RunRace {
public static void main(String[] args) {

Racers rcr1 = new Racers("1");

Racers rcr2 = new Racers("2");

Racers rcr3 = new Racers("3");

Racers rcr4 = new Racers("4");

Racers rcr5 = new Racers("5");

Racers rcr6 = new Racers("6");

Racers rcr7 = new Racers("7");

Racers rcr8 = new Racers("8");

Racers rcr9 = new Racers("9");

Racers rcr10 = new Racers("10");

}
}