BestJewSinceJC 700 Posting Maven

I'm sure I can help you fix your binarySearch method (it looks like everything else in your program is correct). However, your instructions specifically state: "The method should use sequential search of ArrayList to find the target account." Your program doesn't use a sequential search, it uses a binary search. A sequential search would go through the ArrayList one element at a time, checking to see if each element is the one you're looking for. A binary search starts at the middle, checks to see if the element you're looking for is greater than or less than the middle. If it is greater than the middle, you now know that the element you're looking for is to the right of the middle, so now you have to look through everything to the right of the middle (using the same procedure). Your binarySearch method uses a while loop, and inside that while loop, it continuously calls Collections.binarySearch. Doing this doesn't make sense because Collections.binarySearch only needs to be called once . . and it returns either the index where your element was found ( >= 0 obviously) or it returns an integer less than 0 if your element wasn't in the array.

Now, you weren't completely off - the fact that you were using a while loop and going through your array means that you were probably trying to do a sequential search (if you look at each index, 0 through the end of the array, in order, …

BestJewSinceJC 700 Posting Maven

If you change your JOptionPanes from using "frame" as their first argument to using "null" as their first argument, your program will work. Alternatively, you could continue to use "frame" as the first argument to your JOptionPanes and add the line "frame.dispose()" after each JOptionPane is created and it will work.

BestJewSinceJC 700 Posting Maven

It'd be extremely helpful if you mentioned how you want to split the Strings into substrings (e.g., do you want them to be split around spaces? Around a particular letter? Or what?)

If you read the String class documentation you'll see a method called split() that takes a regular expression as an argument; the method splits the String around matches of the regular expression. So if you were to create a regular expression that matched against the letter "K", then passed that into myString.split(regexForK), then that method call would return a String[], which is exactly what you want.

BestJewSinceJC 700 Posting Maven

I think you mean this:

http://www.j2ee.me/developer/technicalArticles/Programming/serialization/

(And related articles about Java Object Serialization)

BestJewSinceJC 700 Posting Maven

You could use a Timer to help you draw your lines at specific intervals. You could also use Thread.sleep(), as you mentioned - if that isn't working there must be something wrong in your implementation. Can you post all of your (relevant) code, such as your repaint() method, everywhere you call repaint(), and where you attempted to put Thread.sleep()? And have you looked at this?

http://java.sun.com/docs/books/tutorial/essential/concurrency/sleep.html

BestJewSinceJC 700 Posting Maven

With the modification I made earlier, your program seems like it works. . so if you take the code I posted earlier and update the code on your machine with it, your program should run correctly.

BestJewSinceJC 700 Posting Maven

If an alternate approach helps at all: strstr in this case, since it returns "a pointer to the located string (or null ptr if it isn't found)" you can (hypothetically) read your entire file into an array, then use strstr to find the first occurrence of whatever you're searching for. While you still find occurrences, keep calling strstr. The trick is to use pointer arithmetic to figure out where in the array to start searching on each iteration of your while loop. So basically: (pseudocode)

while(ptr != null){
- ptr = strstr(arrayposition, strToLookFor);
- arrayposition = now do some pointer arithmetic to find out the place in the array where you just saw the last substring.
}

(edit) after re-reading your code, it looks like you did already realize this. But maybe my approach will be a slightly easier approach.

BestJewSinceJC 700 Posting Maven

By bit operation I think he literally means an operation that directly operates on/manipulates bits. Although the only way I see to do this is to set aside an array of that many bits (via creating an array of 4 integers, which are 32 bits each in Java), then by using some specific operation such as OR (where each int in the array was initialized to 0). Probably not the solution he wants though.

BestJewSinceJC 700 Posting Maven

I looked through your code, and I don't see a reason why you should be continuously adding and removing the mouse listener. If there is no reason to do so, don't do it. (I.e. it adds nothing to your program's logic, so what use does it have? Your ClientTurn_flag takes care of whose turn it is; you don't need to remove the listener). Other than that I changed the placement of one of your statements, it will likely do nothing though . . If you post the Server's code as well, I will run it and I'll help you debug it.

package game;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.*;

public class Clientms2 extends JFrame implements MouseListener
{
	private static final long serialVersionUID = 1L;
	
	JFrame container;
	static InetAddress address;
	static Socket s;
	static String host = "localhost";
	static BufferedOutputStream bos;
	static OutputStreamWriter osw;
	static BufferedInputStream bis;
	static InputStreamReader isr;
	static int port = 5656;
	static String message;
	static boolean ClientTurn_flag = true;
	//static MouseListener l;
	
	static int tempx, tempy;
	static int mouseX, mouseY;
	static StringBuffer x, y;
	
	public Clientms2()
	{
		super("Client 1: Player 1");
		setSize(800, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		addMouseListener(this);
	}
		
	public static void main(String args[])
	{
		Clientms2 client = new Clientms2();
		client.setVisible(true);
	}

	@Override
	public void mouseClicked(MouseEvent e) 
	{
		if (ClientTurn_flag)
		{
			mouseX = e.getX();
			mouseY = e.getY();
			removeMouseListener(this);
	
			try 
			{
				address = InetAddress.getByName(host);
				s = new Socket(address, port);
		
			} catch (IOException e1) { e1.printStackTrace();	}
			
			System.out.println("CLIENT SENDS: (" + mouseX + ", " + …
Ezzaral commented: Agreed. +9
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

Again, refer to javaaddict's post for more information on why nobody is helping you.

BestJewSinceJC 700 Posting Maven

I don't know how people get that ugly. Nevermind the bad dressing.

BestJewSinceJC 700 Posting Maven

I would make a guess and then try the substitution method. I'm not very good with recurrences though so use this advice at your own risk (of failure). But the guess I'd make first would take into consideration that for large n, n/2 + root n is controlled by n/2. That square root at the end of the recurrence doesn't matter, I don't think, because it's a constant. After you make the guess though, you substitute and see if it works, so even if your logic sucks, as mine might have, as long as you can do algebra, it doesn't matter.

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

First you need to define what you consider a palindrome. Typically a palindrome is considered any String with the same characters (in both directions) regardless of case and regardless of spacing. So your isPalindrome method should

1. Remove all whitespace from your String (you can do this by using google) and typing "Java remove whitespace from string". I'd be careful to look at the solutions first, because I just took a look and some are much simpler than others.
2. Convert the entire thing to uppercase or lowercase (doesn't matter which) . . just use the toUpperCase method.
3. Scan each character at the front and at the back, making sure they match, until you reach the middle. If you can't figure out how to do this you can google for a palindrome program, there are plenty of examples.

BestJewSinceJC 700 Posting Maven
BestJewSinceJC 700 Posting Maven

I'd agree that using netbeans would be helpful, but which editor he uses is ultimately up to him. Seems strange to me to respond that you found errors but not to bother mentioning any though.

edit: sorry

Also, OP, I don't have any advice to really offer you right now, but you should not make variables that start with uppercase letters! It's convention. Principal should be principal. And
public double FinalBalance(); looks like you should remove the ; .. isn't that supposed to be a method?

BestJewSinceJC 700 Posting Maven

Is it true that code tags now "know" what forum they're in? Because I was only posting one line of code so I didn't use the =Java tag in the Java forum, yet it got posted with the Java code tags instead of the regular code tags. If that is true, it's definitely cool because now new members won't be able to post with the other tags. But maybe I'm just going crazy.

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

Spanking is fine. If you want to be really technical and define a spank that is fine, anything that does not leave a bruise is ok by me.

BestJewSinceJC 700 Posting Maven

Rugby is definitely a simpler game than American football. It has fewer rules and fewer plays (in terms of coaching) and fewer formations. How could such a game not be simpler?

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

Post in a regular thread. This is a code snippet which is not used for posting questions. And next time post with code=Java tags

BestJewSinceJC 700 Posting Maven

You should probably actually write a helper method to do this. The helper method would figure out all of the numbers that divide perfectly into a given integer. It would return an ArrayList/array of these results. Then your other method add these numbers together to see if the Integer was a perfect number.

In order to figure out all of the numbers that divide perfectly into another number, you can use a for loop. Keep in mind that the % operator will tell return the "remainder" portion of a division. For example, 6 % 1 = 0, 6 % 2 = 0, 6 % 3 = 0. However, if you do 7 % 2 = 1 because 7/2 is 3 Remainder 1. Also, when writing your for loop, keep in mind that you only have to go half-way to the number - for example, if the number is 100, nothing above 50 can possibly divide evenly into 100, so it is a waste of time to check.

Hope that helps.

BestJewSinceJC 700 Posting Maven

Considering that you just said you will be receiving a struct, as long as you know the format of the struct, you can use an array of structs to handle your message queue. This link describes how to create/use arrays of structs.

http://www.asic-world.com/scripting/structs_c.html

As for the threading question, you would need to share the array of structs between your two threads. Since I've never programmed multi-threaded programs in C, I don't know how it would work exactly (without global variables), but I'm sure someone else will be able to explain that to you.

BestJewSinceJC 700 Posting Maven

System.out.println("ERROR! FLAGRANT WINDOWS ERROR!! Dunno what you did, mate, but you sure screwed things up over here! You're lucky I don't throw the Blue Screen of DEATH at you!! Now enter a positive number!!!(Greater Than 0)"); //print error message1

Haha. I have to say you have a great sense of humor. And I can promise you that this site has many capable java programmers who are more than willing to help you with your project, provided you put in the effort yourself, which it is clear that you are doing. Don't give up. Anyway:

I noticed that you are using nextLine() to read in a double. Use nextDouble() instead. Remember that when the user enters a number such as "5.67" or whatever, when they hit enter, that puts two things in the input stream: 5.67 and '\n' (the newline character, from hitting enter). So after you call double whatever = nextDouble(), on the next line, just call nextLine() which will get rid of the '\n'.

Anyway, in order to print things out the way you want to, override the toString() method in your Storage class. For example, drop the following method into your Storage class:

public String toString(){
String s = donationsYTD + " " + donationAmount;
return s;
}

Then from within AddressBook class, call System.out.println(entry.toString()).

Other than that, I don't know what you mean by the "decimal formatter not working properly". Are you trying to make sure that your value is printed out …

BestJewSinceJC 700 Posting Maven

Again: I'm reiterating because I feel very strongly about this.

The 'new' buttons are distracting and unnecessary. They don't add any features to the site - let me explain. If I have already been in a thread, I know approximately where I left off, and there is already an indicator that there are new posts there (the link is dark blue or something) so I know to go back. If I haven't been in a thread before, then I already know I haven't been in there, the link is still blue, and the 'new' button does nothing except tell me what I already know: that I haven't read any posts in there.

So you see - in either case - I don't see what that button contributes except an eyesore. I'm not trying to be rude because I think this site rocks, and I think almost every feature is pretty good layout-wise. . but this one. . ugh.

BestJewSinceJC 700 Posting Maven

Post in code=java tags. And post what error you're getting, we can't really do mind reading.

BestJewSinceJC 700 Posting Maven

Your question makes no sense. Please re-explain & give code also.

BestJewSinceJC 700 Posting Maven

cgeier already showed you how to get input from the user and how to store that value into a variable. And you'd calculate using the get methods, if anything. That's why it's called "get". You can't calculate anything by using a set method, because set methods by definition are there for setting the value of a variable.

If you want the user to be able to create a Cylinder, you'd do something like cgeier had above, the only difference being that after you stored their input into variables, such as radius, you'd say Cylinder cyl = new Cylinder(radius, whatever); then when you wanted to do your calculations you could do something like

double answer = cyl.getRadius() * cyl.getWhatever();

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

Ahh. That explains a lot. Thanks a lot, you've been extremely helpful.

P.S. for everyone's amusement, I've spent the last half hour trying to figure out why I kept getting "connection refused" from ftp.gl.umbc.edu and it turns out umbc.edu is completely down, so that might hold some answers. :) Debugging hell.... haha

BestJewSinceJC 700 Posting Maven

Yeah, I already did that on a whim, and thanks for compiling and helping me out - much appreciated. But what does "-ansi" actually mean? When I took C programming we just memorized to use that flag - we were told what it meant - but I forgot not and I see it nowhere online. Hmm.

BestJewSinceJC 700 Posting Maven

What. . ?

I compiled with gcc -ansi -Wall client.c and I'm getting all sorts of errors. I'm not really sure what I could be doing wrong. I must be doing something really stupid though, because I don't think anyone else is having trouble with it. (Either that or nobody else is using it. . but it is an appropriate starting point for implementing an FTP client that sends commands such as "USER" and "PWD" to a server, isn't it?)

Edit: I compiled without the -ansi option and I got no errors. I guess its been so long since I've programmed in C that I completely forgot what the -ansi option does, but I can look that up. Thanks for dealing with my posts/stupidity

:)

BestJewSinceJC 700 Posting Maven

Nice of him to PM you instead of bashing you I guess. But seeing that "edited by" message in your post stopped me from my bashing opportunity, so I'm a little disappointed to be honest... :p

BestJewSinceJC 700 Posting Maven

I suggest that you use System.out.println in a lot of different places to see what's going on with your code... such as right above the call to repaint, to make sure repaint gets called when you are assuming it does.

BestJewSinceJC 700 Posting Maven

Well, after reading Salem's response on another site (funny to run into Salem's post on another board from around two years ago, heh):

Well goo.c has the actual structure, so there is no way for foo.c to know what the members are.

All that goo.h does is basically confirm that a struct called st exists.

In foo.c, you could have
t_st *var;
but you still would not be able to access any members within that struct.

It is, in other words an incomplete type.

Incomplete types are very useful for implementing opaque interfaces. goo.h declares an opaque pointer, which any user just sees the pointer.
The actual content of the struct is private to goo.c.

It's what FILE* should be - all user code just passes around a FILE* without ever knowing what is behind it.

I'm not sure how to proceed, but it is interesting. Doesn't that imply that one of my .h files that is included in my file contains an incomplete structure definition of addrinfo?

BestJewSinceJC 700 Posting Maven

Is your program actually calling repaint? I see no debugging... anywhere.

BestJewSinceJC 700 Posting Maven

We're not going to write it for you so show us some effort and explain why you need help/ what exactly you need help with.

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

After compiling the code found here, I immediately got a number of errors. (Unfortunately, Beej is the starting point my instructor suggested to implement our FTP client's "minimal" commands, but that's another story). Anyway, I got rid of a few of them, then ran into this:

client.c:32 - error: storage size of 'hints' isn't known

(It isn't actually on line 32 in Beej's guide, but it is one of the first lines of the main function - it's the one declaring the struct.)

So I looked up structs, and I saw that what Beej used is valid syntax for declaring a struct; however, the struct itself is not declared anywhere in the code. So I can only assume it is declared in a .h file or something, and I do not know how to get rid of that error. Any suggestions?

BestJewSinceJC 700 Posting Maven

You'll definitely find this community a pretty nice place, good to have you here, and hopefully you'll enjoy it. But you can expect the occasional jab also when you post threads in the wrong forums, :)

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

Whatever decision you make, you should be prepared to accept and to deal with the consequences of that decision, regardless of whether the consequences are what you intended. I disagree that your parents are necessarily right. They might have your best interests in mind; however, they also have their own best interests in mind, as Ancient Dragon just inadvertently pointed out.

Also, you shouldn't come on a forum with a question like "should I have sex" when you simultaneously spell everything in your post incorrectly -- my answer is no, you shouldn't.

:)

BestJewSinceJC 700 Posting Maven

Yeah, I got rated R too, but that quiz sucks. The options don't even make sense in half the questions because they don't cover every possible case.

BestJewSinceJC 700 Posting Maven

The "new" buttons that just started showing up suck. They are annoying to look at and they are unnecessary and they serve little purpose. I can already tell which threads I looked at and I don't care if the thread was posted since the last time I visited the forum (or whatever else it is supposed to mean).