BestJewSinceJC 700 Posting Maven

^ No, that is not what he wanted. He just wants any 0's to be removed from the array. I thought I already answered this in another thread last week but in any case it's pretty simple so I don't understand why all of you guys are making it seem so complicated. In addition to the array, use one variable that contains the current size of the array. Then you just loop through the array and if there is a 0, you 'delete' it simply by replacing it with the next non-zero element in the array. So if you had

1, 2, 0, 4, 5 you'd end up with 1, 2, 4, 5, something (the something can be any value, but your size variable will hold 4 so you know the first 4 values in the array matter and the rest do not).

Shifting the non-zero values to the beginning of the array might be a bit trickier than it looks at first glance but it isn't hard.

BestJewSinceJC 700 Posting Maven

Like I mentioned originally, I was aware that what S.o.S mentioned existed (and I agree with him that it is a better idea) if you want to learn XML in the future, or have any interest in continuing with it in any way. But the quickest way to do your current task would probably be the regular methods such as Scanner and Regex rather than learn how to use JDOM.

BestJewSinceJC 700 Posting Maven

Very carefully. For example, if you use a regular expression, use a while loop to read in all of the text until it matches the beginning tag. Now that you matched the beginning tag, you know that the text you want to replace is next. Now skip over the rest of the text until you see the ending tag (also using a regular expression to find it). You can easily keep track of where you saw the beginning and end by doing that, and it doesn't really matter *where* in the file this occurs, as long as you've stored everything else. Once you see the ending tag, add that to the info to be written back out to file, and then add everything else after the end tag as well.

Edit: The index methods shown here show you how you can tell exactly where in the file the expressions were found. So if you use the Scanner class's nextLine() method, I think the way the XML file is formatted (with each tag on a different line, right?) will make your problem easier to solve. Just read in each line, use your regular expressions to search it. Since you already know what your tag is, though, it shouldn't matter what is inbetween originally... after you find the start tag just get rid of the rest of the line, put your text after the start tag, then add on the end tag, and write that back out to the …

BestJewSinceJC 700 Posting Maven

There are some sort of web services xml bindings for java, but it seems to me that you could just treat it as a plain text file, do a search for <JOBNAME> based on a regular expression, then do your editing and write everything back out to the file again.

BestJewSinceJC 700 Posting Maven

Then I would say the OP is confused by the lack of a no argument constructor. The no argument constructor is only automagically present if the class does not provide any other constructors. I.e., since FileReader has other constructors, the compiler would not provide a no argument constructor automagically, and the only reason one would be there is if someone had explicitly written one.

BestJewSinceJC 700 Posting Maven

You aren't bothering me and I'm happy to help. Just in the future please explain in advance exactly what your restrictions are (I have to/only want to do this using arrays) and what your problem looks like- for example, it would have been hugely useful to know you had "Number, marks, name, nationality, average" for each group.

P.S. you could have had a class that looked like the following:

public class Student {
int number;
int mark;
String name;
String nationality; 
float average;


public Student(int number, int mark, String name, String nationality, float average){
this.number = number;
this.mark = mark;
this.name = name;
this.nationality = nationality;
this.average = average;
}

}

Then you would have only needed one array declared as Student[] students = new Student[100]; and you could have put the students in it by doing this:

//Assuming you already read in, from file, your variables (no, marks, name, nationality, and average)
Student stud = new Student(no, marks, name, nationality, average);

To read in the variables you'd need a slight change since I declared some of them as int, not as Strings... you'd need to use Scanner's nextInt() and nextFloat() methods to read in the int and the float. But overall, this would make your program much simpler. Either way, I'm glad you figured it out and I shouldn't have been so harsh.. Good luck with your exams & whatnot.

Oh, and if you wanted to print out the Students, you could further extend what …

BestJewSinceJC 700 Posting Maven

Why would you think that you came up with a "simpler way" than what I suggested when you don't even know what a class (arguably the most basic unit of OOP) is? Additionally, you not only dismissed my solution to the problem, but then asked another question without answering my question about your problem: to further explain the 3 groups of students. I obviously didn't ask that question for my own good, so the only plausible explanation is that anyone responding to your questions needs to know that information to properly help you. Again, I strongly suggest that you read about the topics I mentioned above, as they will make your project a lot easier to do and more correct. But if you insist on using arrays and only arrays, then at least specify these problem restrictions in advance, as well as explaining your project goals, requirements, and inputs thoroughly, so that people like me don't make the mistake of wasting 20 minutes trying to help you only to be told that you've found a "simpler way".

BestJewSinceJC 700 Posting Maven

I rarely need to use two dimensional arrays and I have *never* needed to use a three dimensional array. If you create an Object that represents one student, then you can use a one dimensional array and store a bunch of Students in it. You mentioned having three student groups. In order to properly design a program, you need to consider how these student groups are related, and how they are different. For example, since they are student groups, I am assuming they have certain things in common (typical students all have transcripts for example, and all have to pay tuition). So where does this knowledge fit into a Java program? Well, it helps you to design classes that make sense. If you don't know about Java classes, Java Objects, and inheritance, I suggest you start getting familiar before trying to do this program. The basic idea is that you can design a Student class and you can store in it everything you want to about a Student. Then you can create an array (1 dimensional), create a bunch of Students, and put the Students in the array. If you want to, you could make your Student class be the parent class, and then you would have the three following classes: "public class StudentGroupOne extends Student", "public class StudentGroupTwo extends Student", "public class StudentGroupThree extends Student".

Anyway, I hope that is helpful but it is hard to discuss it abstractly - if you know nothing about Objects and classes …

BestJewSinceJC 700 Posting Maven

From what I remember SwingWorker is used when you have a long running task that you need to run. The reason you'd use SwingWorker is to run your long running task in the SwingWorker thread so that the task does not block up your GUI, causing it to appear unresponsive. You can use the class from within a Swing application... as the name SwingWorker might imply. You might also be able to use it elsewhere. Also, I should note that asking these types of questions is definitely discouraged. I'm briefly avoiding studying for my computer algorithms test so I answered you but ask something more specific.

BestJewSinceJC 700 Posting Maven

Interesting. Guess that's it then.

BestJewSinceJC 700 Posting Maven

Yeah you've been taught wrong. You have to learn objective c among other tools. This (from my class) is a good reference material:

http://cs491f09.wordpress.com/category/lectures/

But if you have any more questions about the iphone and objective c, etc, post them in the appropriate forum (which I think would be 'other languages' unless you have more questions about Java & the iphone)

BestJewSinceJC 700 Posting Maven

If you're only adding .1 or .25 etc, but the result shows up as something which isn't a multiple of .1 or .25, then y was clearly not equal to a multiple of .1 or .25 to begin with. In other words your initial y value must have been something weird; how was y initially set?

BestJewSinceJC 700 Posting Maven

This is definitely interesting. Before I get into my crazy idea of how to do this (that involves bit stuffing and using a special character to denote the start and end of images) let me make sure I understand the issues. Mixing images and text is never really done in the same file, and I don't think there are any 'standard' ways to do this. Do you plan to read back in the images and Strings properly later? Because if you just start writing them to a file together how will you know which is which? You need some way to denote where an image starts and ends. I hardly think writing both to the same file is your issue - being able to read them back in correctly later is your real problem. Yes/no? And any other more intelligent members here have any suggestions? (Before I go off on a tangent about bit stuffing, because there is probably a much easier way to do this?)

BestJewSinceJC 700 Posting Maven

Why is there so much code in your remove method? All you need to do is search linearly through your array looking for an item with the name that matches the String you're searching for. That should take maybe three lines or so of code.

method remove(string removeMe)
for (every element in the array)
if (current element isEqualTo removeMe)
then remove the current element from the array

BestJewSinceJC 700 Posting Maven

You could also (more correctly) fix it by using error checking. try... catch. Exception handling. Look them up. The poster above me, his solution won't work if a user goes back and edits something into a text field that isn't a double. Then you'll get the error again. Error checking will always work.

BestJewSinceJC 700 Posting Maven

For what it's worth I agree with Masijade (about learning to code GUIs by hand first, then moving on to builders if you want to). Doing so was very helpful to me and it made it a lot easier to use the GUI builder as well. If you jump into the GUI builder without knowing a lot of things first, you will end up confused and unable to modify key sections of the GUI builder's code to get it to do what you want.

BestJewSinceJC 700 Posting Maven

For specific components see here: http://java.sun.com/docs/books/tutorial/uiswing/components/index.html

(for example, if you want to use a button in your app, look there to see how, or if you want to use a table, etc)

BestJewSinceJC 700 Posting Maven

Are you trying to manually do a character by character search to match words? That is a mistake. Technically it could work but the amount of effort would be enormous and fruitless. I see that your "words" can actually be multiple words with spaces between them. My first thought is to create a regular expression for each "bad word" and then scan through your input, seeing if that bad word/pattern is in the input. If it is in the input, then you can do something such as *** it out. For learning about regular expressions, see here:

http://java.sun.com/docs/books/tutorial/essential/regex/

If you read for a few minutes you'll see that their examples give you exactly what you need and you don't even need to get your hands dirty to accomplish your task. Matching shit to SHIT or Shit or other variations of lowercase/uppercase is also just as easy.

BestJewSinceJC 700 Posting Maven

No, I don't think there are non-trivial ways to do it with a standard grading system, other than the obvious ways which have been listed in this thread already. I don't understand the system you listed, but consider that since 90 and 100 are both usually considered A's, you can't really index an array to get the letter 'A' because what indexes would you use? 9 and 10, after division to get the tens digit, are both A's. So your array would not work without a lot of extra work.

BestJewSinceJC 700 Posting Maven

http://en.wikipedia.org/wiki/Hash_table

If you have specific questions ask them and someone will be happy to help, but if you're going to ask things like that, use google.

BestJewSinceJC 700 Posting Maven

Not to be repeating myself. . but call of duty came out recently. .

BestJewSinceJC 700 Posting Maven

I actually PMed Dani with this same question a few weeks ago, but I received no response. I didn't want to post it on the forums though. If you guys want to get a one time donation system set up, let me know when I don't have to go out of my way to give you money. But I imagine the real reason no one time system is set up is that it actually makes more money to set up a monthly system because people who might just make a one time donation and then never bother again will sign up monthly instead. Which is perfectly ok if that's the case. But you have to consider people like me who are students, or simply don't want to donate periodically, but would like to do so once in a while.

BestJewSinceJC 700 Posting Maven

Your LoadStudents method doesn't make sense to me. You said while(s.hasNextLine()) but you never read anything in, so I'd think that would be an infinite loop. Additionally, you kept adding a new Student with the argument 'tempPerson', but tempPerson doesn't seem to be changed anywhere. You'll need to post more code...

BestJewSinceJC 700 Posting Maven

There is no reason you'd ever pass "this" as an argument. "this" just refers to the Object of the classtype that you are currently in. So if your class name is Car, and you say "this." anywhere within a method (other than main) in the Car class, then "this." refers to the instance i.e. the Car Object. So passing "this" to a constructor makes no sense, unless you are in a situation where composition/aggregation is involved. (And even then you'd typically not need to use the "this" keyword).

BestJewSinceJC 700 Posting Maven

I haven't worked with Applets, but I think they work the same way as Swing basically? (Edit: confirmed - one whole point of JApplet is that it works with Swing). Use a JPanel or whatever the standard applet window is called, make the layout a GridLayout(x, y) where x and y are the 10x15 (the number of cells you need). Then add one JButton for each cell. Set the text of each JButton to "#" and color it blue or white depending on what it should be.

I realize that is somewhat undetailed, but I think you are going wrong by trying to draw all these rectangles yourself. If you use a strategy similar to what I mentioned above, it will be a lot easier.

BestJewSinceJC 700 Posting Maven

Cool. Mark the thread as solved please.

BestJewSinceJC 700 Posting Maven

Did you add the helpMenu to your JPanel? You didn't provide enough code to see what's going on. The method you posted looks fine but I'm not sure if you ever added the helpMenu (that was returned) to anything. PS after you add helpMenu to the JPanel you might want to call revalidate()

BestJewSinceJC 700 Posting Maven

if e.isSelected makes no sense because you already know which item is selected, and you already know that it's selected because otherwise the actionPerformed method wouldn't have been called. And if you want help writing the rest of the method you should probably mention what you want it to do, which you never said. Do you think we know what your program's requirements are?

BestJewSinceJC 700 Posting Maven

What is subpanel? Up till line 9 your code seems ok to me, after that I have no idea what you are trying to do.

BestJewSinceJC 700 Posting Maven

If you have purchased a version of Windows, if you then use a different, even stolen version of Windows but can prove that you purchased it previously. . it is still illegal, but highly unlikely that you would ever be prosecuted for it. The same as if you bought a CD before, lost it, still had the receipt, then downloaded the entire thing off the internet.

BestJewSinceJC 700 Posting Maven

Then do System.out.println("f") afterwards. lol.

BestJewSinceJC 700 Posting Maven

You're probably storing it in a double variable. For example

double result = 0.0;

result = average(x,y);
Now result is a double which is a larger type than float.

BestJewSinceJC 700 Posting Maven
return (float)((x+y)/2.0);

OR

return ((float)(x+y))/2.0;
BestJewSinceJC 700 Posting Maven

no problem. If you want to go that route, I don't see how he could be upset. There are a lot of tutorials online on how to use structs.

BestJewSinceJC 700 Posting Maven

Please don't PM daniweb members. Personally I don't mind, but in general, that is the rule here. If people want to help they will respond in your thread. Regarding your PM, if your professor mentioned that it was extra credit to use more than a 2D array, then I must be mistaken about my entire post, so ignore all my posts in this thread. Thanks.

BestJewSinceJC 700 Posting Maven

My advice was to not use a 4 dimensional array when one is not needed. In fact my claim is that using a 4d array in this case is not good programming. If you were to declare a 2d array with this data:

seats where MAX SEATS is 50
people where MAX PEOPLE is 10

You would then have array[10][50] which is 10 arrays of 50 elements each = 500 elements. But you only have 60 elements. It seems to me that the larger your MAX sizes are declared, the more space you are wasting, unless I am either that tired, or your algorithm is that strange. Additionally, using a 4D array is obviously making your program difficult to debug, so why don't you switch to a sensible alternative? If you don't believe me, then wait for the C experts to come in here, they'll be quick to point out (rightfully so) that I'm wrong if I am wrong.

BestJewSinceJC 700 Posting Maven

That is actually good logic - but it assumes that the MouseListener wouldn't get re-added due to the method calls being blocked/having to wait for receive, so it probably runs into the same problem as the variable does. Although I'm not sure and honestly don't feel like investigating. But I'm glad you have it working now, wish I could've been of better assistance.

PS mark solved

:)

BestJewSinceJC 700 Posting Maven

Well, thank god that somebody else stepped up, because I was getting quite frustrated and it isn't even my program. Haha. Although after a quick look I don't see why your version is any different than mine was, but I suppose it is those two while loops that truly make the difference. But no need to explain unless the OP is curious - I'll take a look at it when I get up the effort to look at this thing without getting frustrated again.

BestJewSinceJC 700 Posting Maven

why would you post 'php code error' in the java forum?

BestJewSinceJC 700 Posting Maven

You should switch to using a struct to represent your ticket class, destination, seat, and time. . All four of those things are obviously related data. You're only going to cause yourself headache trying to use a 4d array for this purpose, and using a 4d array like that doesn't make sense for your data . Why not create a struct to group the related data and then create an array of structs?

BestJewSinceJC 700 Posting Maven

My example was pretty long, but try to grasp the concept and I think you'll be able to complete your program. Again, the basic idea I'm proposing is that you multi-thread your application so that the read() method doesn't block the main thread. I'm not sure of the best way to do this, but I'd first try to create a new Thread in which you'd run the receive() method. Hypothetically, you could also make sure that any time the mouse listener method was called it happened in a different thread, but I don't know how you'd do that (since it depends on an event). So if it was my program I'd try the first option. Anyway I really hope this works out for you. I'm no expert in Socket programming or in multithreaded programming, and everything I said here could be completely wrong. But I know the basic concepts of each, so hopefully that little knowledge will be helpful to you here. Good luck

:)

BestJewSinceJC 700 Posting Maven

Sorry, took too long and couldn't edit my last post. Update:

Ok, so this seems to be the issue: since read() blocks while it waits for input, when the client's receive() method is executed, all subsequent mouse clicks on the client "don't happen" until after the client finishes receiving the data via the read() method. So when you click out of order, you are putting an extra event on the thread which doesn't get executed until after read() is done. What you really want to do is "throw away" the click and pretend it didn't happen at all. But I think you need to multi-thread your application. Hopefully someone else on Daniweb will verify this so that I'm not unintentionally leading you astray, but as of now, I believe this to be accurate.

This code will demonstrate to you what I'm talking about. If you look at the ClientTurn_flag variable, you'd expect that once you click (which calls receive() and sets ClientTurn_flag to false), the next time you clicked on the client's window, it should print out "wasn't client's turn". However, it doesn't do that, since the read() method is blocking the thread. In fact what *I think* happens is this. Lets say you run the program, then click:

1) Click client's window
-At this point ClientTurn_flag is true.
-client sends it's coordinates to the server.
-client calls receive method and waits for the server to take it's turn. The receive method calls read(), …

BestJewSinceJC 700 Posting Maven

The error either has to do with memory consistency, or the fact that the InputStream class's read() method blocks while it waits for input (and the read() method gets called multiple times when you keep clicking on the Server's window when it is the client's turn). I'll figure it out soon enough. But if I don't, I think you should read about these avenues as well.

Update:

Ok, so this seems to be the issue: since read() blocks while it waits for input, when the client's receive() method is executed, all subsequent mouse clicks on the client "don't happen" until after the client finishes receiving the data via the read() method. So when you click out of order, you are putting an extra event on the thread which doesn't get executed until after read() is done. What you really want to do is "throw away" the click and pretend it didn't happen at all. But I think you need to multi-thread your application. Hopefully someone else on Daniweb will verify this so that I'm not unintentionally leading you astray, but as of now, I believe this to be accurate.

BestJewSinceJC 700 Posting Maven

Actually you are correct, I did not test my modifications thoroughly enough. The problem seems to reside in the server class though, not the client class. I'll take a look again.

BestJewSinceJC 700 Posting Maven

I found the problem. Your code is way more complex than it needs to be. Essentially all you *need* in the client is the "clientTurnFlag", the send method, and the receive method. Initially in your main method you don't do anything except for opening the client's window and setting it visible. Your problem was basically setting the flags to false at the incorrect times. Here is your (much updated) code. I removed a lot of unnecessary code. There are logically only a few places where you should be setting your flags to true and false. Btw, you only need to accept the socket once - you shouldn't be accepting the same connection over and over again on your ServerSocket.

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(400, 400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		addMouseListener(this);
		try {
			address = InetAddress.getByName(host);
			s = new Socket(address, port);
		} catch(Exception e){System.out.println("You fail at socket programs sucka!!!");}
	}
		
	public static void main(String args[])
	{
		Clientms2 client = new Clientms2();
		client.setVisible(true);
		
		
	}

	@Override
	public void mouseClicked(MouseEvent e) 
	{ …
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

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

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