BestJewSinceJC 700 Posting Maven

I can't figure this out!! I am using removeMouseListener right after a click is done (provided its the right person's turn)

I would say that this is a logical error. It might work if done properly, but in my opinion, the proper way to do this would be to keep both client and server aware of whose turn it is at all times. If the client clicks when it isn't their turn, don't tell the server about it. If the server clicks when it isn't their turn, don't tell the client about it. And on both client and server, if it is their turn and the other player sends them "click coordinates", then ignore it, but throw an Exception (or display an error message) because it is an error. (Example: if it's the server's turn but the client sends them click coordinates, then it's obviously an error). So what I'm saying is that you shouldn't remove the mouse listeners - instead, implement "talking about whose turn it is" between client and server, and do so such that they always know whose turn it is.

With that said, I'll try to help you debug the code you currently have, but I can't promise that I'll be able to fix whatever it is.

BestJewSinceJC 700 Posting Maven

I'm supposed to merge a ordered data of two files into a third file, keeping the data in order. I'm suppose to create a MergeFiles application that merges the integers ordered from low to high in two files into a third file, keeping the order from low to high. Then should merge the two files by taking one element at a time from each, and the third file should contain the numbers from both file from lowest to highest. so, i saved the numbers in wordpad as data1.txt, and data2.txt.

Data1: 11 25 36 45 56 78 90
Data2: 1 3 5 7 54 32 78 99

Let me make sure I get what you said: I'm assuming that according to what you said, for the two files above, a third file would be produced:

data3: 1 3 5 7 11 25 32 36 45 56 78 78 90 99

So your only requirement is to order them from lowest to highest and put them in a third file? Or do you also have to read in the numbers in alternating fashion? Either way, since the order in which you read in the elements is trivial, I'll explain how this works.

Options:

1) Ordering your elements from lowest to highest is called sorting. To do so, you will need to use a sorting algorithm. So you could create an array (or ArrayList), read all of the numbers from both files in, and sort the array. …

BestJewSinceJC 700 Posting Maven

If you ask a more specific question, or tell me exactly what isn't working, I'll help you out. After 12 straight hours of coding an iphone project that uses the google maps API, kinda tired though.

edit: when I say specific, that isn't what I meant, you were plenty specific, sorry. Its just that there are a lot of examples in this thread, so if you want help with a particular thing, you should repost the specific method/code you are talking about, say what you want to accomplish (i.e. what the code is supposed to do) and tell us any other info that would be helpful. Otherwise, if you're just stuck in general (i.e. you don't understand some concept) please restate it a little differently and I'll help you.

BestJewSinceJC 700 Posting Maven

It seems like you have three Strings, firstName, middleName, and lastName. If that is the case, use

String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

instead of the substring method you're using.

BestJewSinceJC 700 Posting Maven

Oh man! Can I buy all of them???

BestJewSinceJC 700 Posting Maven

You're using filename as if it was a method, but it is not a method. It is a variable of type String. And also, since filename was declared in the main method, you cannot use it in any other method. If you want to use a variable from one method in another method, you must pass it as a parameter. Examples of parameters for your isValid function are your AccountList and target.

BestJewSinceJC 700 Posting Maven

You could also make a small class:

public class TrackWord{
String word;
int lineNumber;
int wordNumber;
}

Where word is the word itself, line number is the line the word was seen on, and word number of the word's position within that line. So for the sentence "I have a massive hangover" every word would have a line number of 1 and "massive" would have a word number of 4. You could easily implement this by doing the following:

int lineNumber = 1;
while(scanner.hasNextLine()){
String theLine = scanner.nextLine();
//theLine is now the next line from your file
Scanner lineScanner = new Scanner(theLine);
        int wordCount = 0;
        while(lineScanner.hasNext()){
                String nextWord = lineScanner.next();
        }
lineNumber++;
}

I intentionally left out some stuff from the above code (as per our homework rules), but that is the basic idea. To fully implement it, you'd have to increment wordCount in the appropriate place, and then you'd have to create a new TrackWord object, populate it with the name of the word and the line number and word number it was seen at, then add the TrackWords to an array.

P.S. Since you want to track every line where 'fast' was seen, you could modify the class I showed above so that it had an ArrayList of line numbers. Then every time you saw the word fast, you could check to see if you already have a TrackWord Object for the word fast. And if you do, then you could add the line …

BestJewSinceJC 700 Posting Maven

That would be nice, but if you read his code and his errors, he clearly is having problems with passing the correct arguments into his current methods. Creating more methods at this point will only add to the chaos. His problem right now is that he needs to grasp how methods work and how the compiler enforces the correct parameters being passed.

BestJewSinceJC 700 Posting Maven

You also have the error message:

[ The method sequentialSearch(Scanner, String) is undefined for the type AccountNumber ]

That's because your method header is defined as "public static int sequentialSearch(int[] inputFile,int filename);" ... but if you look in your code, you called it using

sequentialSearch(inputFile, filename);

The problem is that the name of the parameter does not matter when you are calling a method, it is the type that matters. Since you declared "inputFile" as a Scanner (in your main method), then you called sequentialSearch using a Scanner, which it does not take, you got an error.

Again: the sequentialSearch method, according to your code, takes two parameters: int[] and int, but you passed it a Scanner, hence the compiler gave you an error.

Hope that helps

BestJewSinceJC 700 Posting Maven

Duplicate local variable 'target' means exactly what it says - you have the same variable declared twice in the same method, which is not allowed. In your method header you have "ArrayList AccountList, String target" but then below, you said "int target" which is attempting to re-declare the variable target. You can't do that, name one of them something different or get rid of one of them. PS, the reason for the "ArrayList is a raw type" message is because of Java generics (don't worry about it for now), just know that if you are expecting your method to take an ArrayList of Strings (which you are), then you would say "ArrayList<String> AccountList". And btw, variable names, by convention (i.e. people almost always do it this way) are named in camel case, starting with lower, so you should make it "ArrayList<String> accountList".

BestJewSinceJC 700 Posting Maven

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Using next() is the easiest way to go and will work for what you are describing. And I'm linking to the documentation because if you read the method description for next(), it will become clear what the method does. Alternatively, you could create a while loop

while(scanner.hasNext()) System.out.println(scanner.next());

And just look at what it prints to answer your question.

javaAddict commented: Good solution +4
BestJewSinceJC 700 Posting Maven

Oops, posted at the same time as you Ezzaral.

BestJewSinceJC 700 Posting Maven

Well, what is the size of your array when it draws two lines next to each other? Is it one, or two? Do some debugging. If the array's size is two, then no wonder it looks like that. If its one, my theory is kaput, but have you even made this simple check?

BestJewSinceJC 700 Posting Maven

If you want to find a specific word from within a file, another way to do it would be to use Scanner's next() method, then use String.equals to see if they match.

BestJewSinceJC 700 Posting Maven

Ok, glad you got it fixed. The reason I thought you didn't know the API was because it looked like you were trying to write words by drawing the lines individually and connecting them. But anyway, my mistake. Mark this solved if you have no more questions.

BestJewSinceJC 700 Posting Maven

Just use the drawString method to draw your words. g.drawString(parameters go here) http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html

You can set the Graphics contexts' font and color and stuff before you draw the string, and it will draw in that font and color. At least so says the API if you read that link.

majestic0110 commented: Helpful-as always! +3
BestJewSinceJC 700 Posting Maven

Your question has been asked and answered around a thousand times online. If you want the exact code, find it on google. Otherwise read this, it explains the answer.

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html

BestJewSinceJC 700 Posting Maven

Nobody is going to do your homework for you. You will only receive help here if you show effort on your assignment.

BestJewSinceJC 700 Posting Maven

Your statement on line 21 does nothing because 'gallons' is 0. Saying gallonsused++ adds one to gallonsused. Inside your while loop, what you want to be doing is saying gallonsused = gallonsused + gallons. (Or, equivalently, gallonsused+=gallons). You also only need to prompt the user for the gallons used once -- right after the start of the while loop. You have it in there twice.

You have a couple of other mistakes but they are along the same lines. Once you update those that I mentioned I'll come back and see if I can give you some more help.

BestJewSinceJC 700 Posting Maven

Use an if statement in your program instead of a while loop and your program will work as expected. You do know how to use while loops vs. how to use if statements, don't you? It seems like you don't.

BestJewSinceJC 700 Posting Maven

You can 'wrap' the array by creating a new Float[] array and then in a for loop, add each float primitive type to the Float array.

BestJewSinceJC 700 Posting Maven

Use Scanner, not BufferedReader. Create the Scanner then use scanner.nextLine(). Print out the line that nextLine() returns, and you're done. You could use BufferedReader also and just read in bytes while its not = '\n' also, I suppose.

BestJewSinceJC 700 Posting Maven

They're being removed because you said

if(Balls.get(X).intersects(Balls.get(Y)) == false) {
Balls.remove(X);
}

That doesn't make logical sense to me. It should be if that == true, then remove one. It's going to be false almost all the time. Your statement removes X every time X and Y don't intersect, whereas you stated above that you want to remove X every time X and Y do intersect.

BestJewSinceJC 700 Posting Maven

What is an OBOB? And if it is solved, mark as solved.

BestJewSinceJC 700 Posting Maven

Because q+3 is longer than the total length of your rS array. You cannot access an array at an index longer than it's largest index. This is probably because your 'max' variable isn't a good indicator of rS's length (you are using 'max' as a bound on how large q+3 can be, which makes the assumption that max is always, at most, 3 less than the size of rS - which I'm guessing it is not since you went out of bounds).

If that didn't make sense, let me rephrase: You need to make sure that your 'max' variable isn't going to allow 'q' to become large enough that q+3 is larger than the length of your array. If q+3 is, at any point, larger than the length of your array, you are going to go out of bounds. (PS keep in mind that arrays are indexed by length-1 so you should actually make sure that q+4 isn't larger than the length of your array).

BestJewSinceJC 700 Posting Maven

In addition to what Peter said, you might find some of the responses here helpful: http://www.daniweb.com/forums/thread232056.html

BestJewSinceJC 700 Posting Maven

To be honest, I'm not quite sure what you're asking, but it sounds a bit out of my league. The only reason JTable itself knows to even *put* the JCheckBoxes into your JTable is because it uses getValueAt(row,column).getClass() to see what class it is from. From the code you posted, you already know where the event is happening. If you want to know what type it is, you could use instanceof, I suppose. But I'm sure you know about that so I'm probably not being very helpful.

PS: You might want to rephrase your question, because there are a lot of people that regularly post in the Java forum who are much better with Swing than I am, so the fact that they haven't responded might indicate that they didn't understand your question either.

BestJewSinceJC 700 Posting Maven

you will have to write a program that reads a 4-digit positive integer from the user and prints out the following information on the screen:

Re-read your project assignment. Pay particular attention to the part that I bolded.

BestJewSinceJC 700 Posting Maven

Is there a method I can invoke that will tell me who the caller was?

You basically already did. Object source = event.getSource() returns the Object that caused the event to be fired. So you could just use '==' to determine which Object it was at that point, if you need to.

BestJewSinceJC 700 Posting Maven
Character ch = new Character('\u00C3');
char prim = ch.charValue();
System.out.println(prim);

Something I just learned how to do after a quick look up in the Character class documentation. I'm sure if you read some stuff yourself and try some things out, you'll find many more useful methods. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html

peter_budo commented: Good pointer +11
BestJewSinceJC 700 Posting Maven

It's because the compiler doesn't think that your method is guaranteed to return either true or false. I.e., it thinks that it's possible to reach the end of your method without returning anything at all. If you're positive that your method will always return either true or false (based on your if statements), then you can go ahead and just put "return true;" at the very bottom of the method and the error will go away. I'd also put "throw new Exception("Shouldn't have gotten this far")" or something similar while you debug.

To drive my point home, consider this code:

public static boolean maybeReturns(int i){

if (i > 100) return true; 

}

The compiler would give the same error since it doesn't think that my method will *always* return something. Your case (in the eyes of the compiler) is the same, except that you code is a lot more complex.

BestJewSinceJC 700 Posting Maven

Just modulus (%) by ten. That will give you the last digit.

BestJewSinceJC 700 Posting Maven

Post the segment of your code where you get your warning and any other relevant portion of your code. In code tags.

BestJewSinceJC 700 Posting Maven

The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline.

javaAddict commented: Smart explanation. Couldn't find it. +4
BestJewSinceJC 700 Posting Maven

If you're done like you say, then post all of your code including the main so that I can run it. I'll see if I can fix it which I probably can, but generics are a little rusty in my mind right now and I see nothing right away.

BestJewSinceJC 700 Posting Maven

Cool, glad you got it to work. If there's anything you still have trouble with let me know and I'll help you out later today. I'm going to do a really fun Iphone program so I'll probably be off until then though.

Oh, and mark as solved if you don't have any more ?s

BestJewSinceJC 700 Posting Maven

Because phrase is a String object, and the "equals" method for the String class is implemented so that it compares two Strings to see if they are the same. If you are interested, you should read about the following topics:

Method overriding
Inheritance

And also, mark solved threads as solved

BestJewSinceJC 700 Posting Maven

Whether or not the stack is empty for an invalid equation depends on your implementation. However, if in your implementation you "pop" an element off of the stack and then compare it to the next '}' or']' or ')', your stack would be empty even if there was an invalid condition.

(Ex: {) ):

Stack after seeing { = {
See ')' and pop top of stack. Top of stack, '{', is not the correct element for ')'. Therefore your stack is empty because you just popped the only element on it, but you have an error condition.

I'm not saying you don't have an error in your stack class, but you can easily come up with an error when your stack is empty if you are popping the elements off the stack before comparing them. If you post all of your classes again (w/ whatever your most up to date code is) I'll look at them later and help you figure out what's wrong.

BestJewSinceJC 700 Posting Maven

I really don't understand what you are saying. How would I initialize "phrase" if i put

while (!phrase.equals("quit")) {

so soon? I'm really new with java so please be kind to me.

Phrase is initialized in the code that you posted. It is initialized to "", which is the empty String. In order to quit using the code Masijade showed you, after you say "please enter a sentence or phrase" inside of the while loop, you would need to read in the user's input, then store it into the phrase variable. If that doesn't make sense, then read about the Scanner class. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Relevant topics: Where it says,

For example, this code allows a user to read a number from System.in:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

But you cannot read in Strings with the nextInt method, so read about the nextLine() method.

BestJewSinceJC 700 Posting Maven

Your syntax is wrong, that is all. .

if ((myStack.empty()==true) && (line.charAt(j) == ')')||(line.charAt(j) == ']')||(line.charAt(j) == '}')) {

Should be:

if ((myStack.empty()==true) && ((line.charAt(j) == ')')||(line.charAt(j) == ']')||(line.charAt(j) == '}'))) {

Because you want to check "if the stack is empty and any one of }, ], or ) is seen, then . . ." but look at your if statement more closely: It will be executed any time a ']' or a '}' is seen, or whenever both the stack is empty and a ')' is seen.

And haha, your error is ironic if that is it, considering the project assignment is about grouping of operators, which was the cause of your error. (But no offense, I've made similar mistakes before)

:)

BestJewSinceJC 700 Posting Maven

keyword - *generally*, i guess you get the special treatment

He's a sponsor. As an admin, it makes sense to give sponsors little things like that.

BestJewSinceJC 700 Posting Maven

I updated my last post, you should take a look. And at this point, I'd recommend that you first develop the code for the parentheses then. After you get that working, then add in logic for the brackets and curly braces. It would be pointless to help you complete the separate stacks solution, since the algorithm itself is flawed.

BestJewSinceJC 700 Posting Maven

You can't use three separate stacks because consider the following input:

{(})

That input isn't valid, and should result in an error. But if you use separate stacks for each symbol, it will appear legitimate and it won't cause an error. And I'll take a look at your code momentarily.

edit: It looks like you're using separate stacks, which I do not think is correct for the reason I listed above. The order in which the braces/brackets/parens are seen is very important, and using different stacks does not keep track of the order they were seen in. You should implement one stack where you use a few comparisons to make sure things are valid.

You'd have the following comparisons:
1) the case where you just saw a }. You'd have code similar to this pseudocode:

typeOfBrace = readInNextBrace();
if (typeOfBrace == '}' or typeOfBrace == ')' or typeOfBrace == ']') //You have to make sure its a closed type before you pop.
topElement = stack.pop();

if (typeOfBrace == '}'){
if (topElement == '{') // there's no error!
} else //there's an error, do something!

2) the case where you just saw a ]. Pseudocode:

typeOfBrace = readInNextBrace();
if (typeOfBrace == '}' or typeOfBrace == ')' or typeOfBrace == ']') //You have to make sure its a closed type before you pop.
topElement = stack.pop();

if (typeOfBrace == ']'){
if (topElement == '[') // there's no error!
} else //there's an error, do something!
BestJewSinceJC 700 Posting Maven


What if there are say three left parentheses on the stack, but then a right bracket appears? How would the program know that the bracket doesn't match, since there are only parentheses on the stack?

edit: If a closed bracket is encountered when a '(' is on the top of the stack, then you know already that you have an error. So when you see a '}', you should attempt to pop a '{' off of the top of the stack. If the element on top of the stack is not a '{', then you have an error. (If this doesn't make sense, read the rest of my post first, then re-read this).

Should each character be tested every time to see if it's a "closer" and if it matches with whatever "opener" is on top of the stack?

When you see an open bracket, you should push an open bracket onto the stack. When you see a closed bracket, you should pop an open bracket from the stack. If you see a closed bracket, and there are no open brackets left on the stack, then you have just encountered an error. After analyzing an entire set of brackets, your stack should be empty, indicating that the same number of open and closed brackets were seen, in an acceptable order. (For example, {{}}}{ has three open brackets and three closed brackets, but it is not in an acceptable order. If your program encountered {{}}}{ it should do …

BestJewSinceJC 700 Posting Maven

Design an application that accepts reader data and produces a count of readers by age groups as follows: under 20, 20-29, 30-39, 40-49, 50 and older."

Essentially these are your requirements and the rest of the paragraph didn't matter whatsoever. I don't really see how we can help without designing the algorithm for you, but as a hint, consider that you only need to do these things:

1. create an array of size 5, one for each of your age groups, anmd initialize each index of the array to 0.
2. read in data and based on what age group each piece of data fits into, add one to the appropriate array index.

BestJewSinceJC 700 Posting Maven

Just some thoughts. .

1) because with the 1/2 condition, if you downsize the array, it will be much more likely that you will then have to subsequently resize the array later (due to having more elements than can fit in the new array you created in contractStack).
2) You should also consider that if you downsize the array based on the < 1/2 condition, then you will be resizing the array more often than you would be with the < 1/4 condition.

BestJewSinceJC 700 Posting Maven

If you want to talk about your lab you're probably better off meeting with your teacher or TA than discussing it here. But if you're going to discuss it here, post the problem and ask specific questions about what you don't understand so that anyone on the site can help you.

BestJewSinceJC 700 Posting Maven

Haha. Pretty amusing that two people from the exact same class ended up here, assuming that is really the case. Apologize for freeposting/upping this thread but that is really funny.

BestJewSinceJC 700 Posting Maven

You need to post your other classes.

firstLevel.add(mainpanel, BorderLayout.CENTER);
firstLevel.add(listpanel, BorderLayout.CENTER);

Looks to me like you're just adding one thing right on top of another. How do you expect to see both?

And there is no sign of CardLayout ever being used in your code. If you want to use CardLayout, that's fine, but I'm not sure how to help when none of your Panels use it. Btw, you don't need to use it anyway. You can just as easily add one "level" to your game at once, and then remove it & add the next level when it is time to do so.

I wish I could be more helpful, but since I cannot run your code and you posted a very minimum selection of code, the best I can say is that I'd simply add the panel that contained the current level, then remove it and add the next level's panel when it was time.

BestJewSinceJC 700 Posting Maven

What is the prize for doing your homework?

I'll give you low rep

:)