BestJewSinceJC 700 Posting Maven

Not saying that these data structures are the most efficient, but one thing you could do is read the entire file into a String (would be inefficient), then search by substring. Continuously increase the size of the substring until it either didn't match the word you're looking for or you found a match. If it didn't match the word (lets say you're looking for the word PROGRAM and it says PRA) you would stop there, move the part of the text you're looking at up a few characters, and start over.

I'm not sure what examples the other posters are pointing you towards, but they will probably be the easier route. This is what I'd do if I wasn't going to look for classes and methods that make my life easier.

BestJewSinceJC 700 Posting Maven

Making it an advanced book from the perspective of someone who is just learning Java. :)

BestJewSinceJC 700 Posting Maven

^ Thats a beginner's book? Sounds like an advanced book to me.

BestJewSinceJC 700 Posting Maven

The method you were supposed to override is called contains, not contain.

BestJewSinceJC 700 Posting Maven

Well. . post the error message you're getting.

BestJewSinceJC 700 Posting Maven

I'm pretty sure your problem is in this code segment:

if(this.getFirstNode()==null)
        {
            this.setFirstNode(node);
            this.setLastNode(node);
        } 
       
        else
        {
            Node temp;
            temp=this.getFirstNode();
           
            while(temp.getNext()!=null){
                temp=temp.getNext();
            }
            temp.setNext(node);
            this.setLastNode(node);
           
        }

Consider what happens the first time you create a Node. This code gets called, and the node sets itself as both the first and the last node. Up to here, we're fine, because technically, it is both the first & the last node. Now, it will skip the else statement and go to the temp.setNext. The problem is, temp is your first Node. (temp is 'node'). Now, you just set temp's "next" node to itself. Can a node be its own next node? I don't think so.

Edit: Although I'm not 100% sure that what I just said is correct, because with what you said you got for output, my response doesn't make sense. But take a look anyway, hopefully it will help.

BestJewSinceJC 700 Posting Maven

The first problem is that Java supports method overloading, meaning

public void actionPerformed(ActionEvent e, pig )

is not the same as

public void actionPerformed(ActionEvent e)

So you should be getting an error saying that you didn't override ActionListener's actionPerformed method... ? Are you?

BestJewSinceJC 700 Posting Maven

Static methods exist for the entire class -- although I'm not sure if this is technically correct, you can think of it as there being 'one copy' for the entire class. Static methods exist whether or not you create an Object of that class type, hence the reason why you can invoke them the way Ezzaral explained. More about static methods:

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

BestJewSinceJC 700 Posting Maven

Also, getting standard deviation is a multi-step process, is it not? Do you think it would be worthwhile to make different methods to do the separate parts? For example, standard deviation includes finding the mean, and I would think a method that finds the mean would be reusable and logically, would be a separate part of computing the standard deviation. Just a thought. (As a side note, I don't know how much it applies to a small example like this, but separating code into logical chunks also makes error checking easier)

Also, check out how to create objects in Java - a Vector might be created by

Vector animals = new Vector();

You have to specify the type.

BestJewSinceJC 700 Posting Maven

Absolute Java is a good beginner's book. It covers the concepts with simplicity and clarity, and goes in depth enough that you can explore the concepts further on your own without too much trouble.

BestJewSinceJC 700 Posting Maven

Yeah, I understand. My advice would've been to do all the math by hand and see if the x and y values fall within the desired range. But that works better (if it does actually work).

:)

BestJewSinceJC 700 Posting Maven

So you're saying to create a Rectangle, for example, that does not appear at all on his GUI? Then he can set the position of this invisible rectangle and see if the user clicks there with contains?

BestJewSinceJC 700 Posting Maven

Post your updated code, then, and your updated errors (if you have any new/different ones). Post your code in code tags and for the first CODE, put so it will have line numbers.[CODE=Java] so it will have line numbers.

BestJewSinceJC 700 Posting Maven

Then you need to write code in your actionPerformed method that will detect when the button is clicked, get the text from wherever the paragraphs are, and read through it, counting the number of times you see '\n\n' (or however you decide to count paragraphs). After you do all of that, use the setText method on your textField to display the number of paragraphs you saw.

edit: I just checked your code and it should already do all of that. You need to write some error code to make sure that

1. It's already going into that if statement (the right button was clicked & the code that I described above executed)
2. Your method to count paragraphs actually works correctly

etc

BestJewSinceJC 700 Posting Maven

I'm not too sure what you're asking but. . do you know what Applets are? This might be irrelevant, if so, feel free to ignore me. But since you've been getting no comments. .

BestJewSinceJC 700 Posting Maven

Are you doing bit operations to count? Create a variable called nrParagraphs and increment it, nrParagraphs = nrParagraphs + 1, each time \n\n is read in. Alternatively, you could increment it every time a tab was read in.

BestJewSinceJC 700 Posting Maven

Good call.

BestJewSinceJC 700 Posting Maven

There is no such method called personAdd in Ghu in what you've posted, yet there is an error saying there is. .

BestJewSinceJC 700 Posting Maven

To answer your question about the equation:
According to your project description, Profit per department = Sales for that department - Cost for that department. Since the data you are provided with is cost per employee and total number of employees, Cost per department = (Cost per employee in that department)(Total number of employees in that department). You are already given the sales. Total profit is the profit for each department added up.

In code form, things are done the same way as they are logically. If your equation was Force = Mass * Acceleration, you would have the following code:

double force = 0;
double mass = 10;
double acceleration = 1.5;

force = mass * acceleration;

//Force is now equal to 15.

Overall comments about your code
Java is an Object Oriented Programming Language. At the lowest level, think about being "Object Oriented" like this: If you were to program a Car program in Java, the "Car" would be represented as an object. The car would itself be an object, and it would contain other objects such as a Steering Wheel, Seat, Radio, Engine, etc. Objects are represented in Java by classes. For example,

public class Car{
int height;
int weight;
Engine carEngine;  
}

And Engine would itself be defined in another class. FYI, this link contains a lot of helpful information: http://java.sun.com/docs/books/tutorial/java/javaOO/index.html. I'm giving you this comment because I think it might be helpful to you to think about each …

BestJewSinceJC 700 Posting Maven

No problem. Don't forget to mark as solved if you don't have any more questions.

BestJewSinceJC 700 Posting Maven

I have to disagree with you Curtis. A "Data Structures" course typically assumes that you already have a knowledge of things like stacks and queues, which are generally taught in lower level courses. Not to rehash this argument. And in my opinion, any solution to the problem is helpful, as long as you explain the advantages and disadvantages of each. So there's nothing wrong with telling him how to do it with a doubly linked list as long as he's aware that a stack is probably the more logical way to do it.

BestJewSinceJC 700 Posting Maven

If you want to use that code, I'd suggest you simply copy and paste it into another file, and edit it so that it sends its output to a text file rather than prints it on the screen. You can use PrintWriter to do this.

BestJewSinceJC 700 Posting Maven

while (coinInsert == '1' || coinInsert == '0.5' || coinInsert == '0.2' || coinInsert == '0.1')
{
coinInc(coinInsert);

sumCoininsert = sumCoininsert + coinInsert;
while (sumCoinInsert < 1.20)
{
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();

}

That code doesn't make sense. Think about it logically. You want to do the following:

While the user hasn't yet paid for their drink, wait for them to put in money, and add up the money that they put in. Stop once they put in enough money and give them their change.

Your code doesn't do this because it has two while loops and you are adding up the money in the wrong place. You can combine your while loops and add up the money properly like this:

while (sumCoinInsert < 1.20 && (coinInsert == '1' || coinInsert == '0.5' || coinInsert == '0.2' || coinInsert == '0.1'))
{
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();
sumCoininsert = sumCoininsert + coinInsert;
}

There may still be some issues (such as the fact that I didn't tell you what to do with your coinInc() method -- I'll leave that to you to figure out) and the fact that the code I gave you will not work since you'll need to modify it to prompt the user to tell you the first coin they are putting in -- but that is the general idea. I'm also not sure how you are giving the …

BestJewSinceJC 700 Posting Maven

In order for the actionPerformed method to be called, you need to use whateverThingItIs.addActionListener(Ghu); where Ghu is an instance of your Ghu class. So, for example, if you had a JButton called button and a Ghu called myGHU, it would have to say button.addActionListener(myGHU); . Your code is pretty hard to read but it seems like you never used this method, so that is why actionPerformed will never be called & it will never work.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/uiswing/

Start there. Ask any questions you may have that can't be easily answered by reading.

BestJewSinceJC 700 Posting Maven

milesdriven = keyboard.nextInt();

I'm not exactly sure what Peter Budo is talking about (not that he's wrong, he probably thinks you were talking about a GUI). But the problem here is that you declared your variable as a double (which can hold decimals such as 32.5 etc), but you tried to read in an integer. You code should say

milesdriven = keyboard.nextDouble();

So, to answer your question, int is used to store an Integer value. An Integer being -3, -2, -1, 0, 1, 2, 3 and so on in both directions. Double is used to store decimals such as .12, 34.2, etc. However, in your code, you declared the variable milesdriven as a double, but you tried to read in an Integer. Look at the Javadoc for the nextInt() method.

http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

If you scroll down and find the 'nextInt' method (under Method Summary), look at the return type (where it says int in plain text to the left of nextInt). That indicates that it is returning an int. So when you used the method, you tried to store an int (what it returned) into a double -- hence the error.

Hope that helps. :)

BestJewSinceJC 700 Posting Maven

You don't sound ignorant and that is not what I was suggesting. You sounded like you want a ready made solution, but that isn't what we help with. For the profit comment your teacher is simply telling you that you have the wrong equation. It isn't calculus. And it appears that your teacher gave you the correct equation (Sales - Cost = Profit). You just need to implement that in your code.

What is the proper equation? And how would I input a loop command to do x amount of departments? Or a infinite amount?

You could have a do while loop that requests the user to enter information or Q to quit, and if the user entered Q, you would exit the do while loop. And I would think what the teacher wrote to you is the proper equation - what is the equation you used? I looked through your code but what I see is nowhere near Profit = Sales-cost.

BestJewSinceJC 700 Posting Maven

You have to specify what you need help with. Frankly, nobody here wants to sort through your project description and your code to figure out why you're having trouble. If you want help you need to make the effort yourself. You should start by dividing your code into logical units (for example, if you were doing an ATM program, you might have these 'logical units': menu selection, withdraw money, deposit money, check account balance). It looks like you're too far into your program to do that now, so if you did, great. In any case, identify what portion of your code doesn't work and post that section of code, including what it is supposed to do and why it doesn't work. Ask specific questions. If your program is simply missing a section of code that it needs, write it and if you have problems, then ask us. I'm more than willing to help, but it doesn't seem like you really asked anything other than to have your work done for you. If this isn't the case than please elaborate.

BestJewSinceJC 700 Posting Maven

Problems:

1. In the method pieceCaptured(), you never declared the variable "i", so you cannot use it as the array index.

2. You declared the arrays "whitePiece" and "blackPiece" inside a method (originalPlaces method), so the scope of those arrays is that method (which means that those arrays cannot be used outside of that method). I suggest you read about variable scope on google, but to quickly summarize:

These aren't an exact definition of scope, or a perfect description, but some guidelines to help you understand:
when you declare a variable inside a class, the variable has the scope of that entire class
when you declare a variable inside a method, the variable has the scope of that method
when you declare a variable inside of a loop, it has the scope of that loop

http://www.dickbaldwin.com/java/Java020.htm
^ read that, specifically, ctrl+F and type "scope", then look at the Q & A section where he defines scope and then explains the examples.

BestJewSinceJC 700 Posting Maven

No one is going to sort through all of that just to give you an explanation that is quite simple anyway. One thing you could do is check to see how many usernames your program currently has, and if it's 0, then display a screen allowing the user to create a username and password. However, a better thing to do (depending on what your project guidelines are) would be to have a menu that allows the user to choose between signing in on an existing account or creating a new account.

BestJewSinceJC 700 Posting Maven

To determine the size of the array:

-Create a variable called size that represents the size the user wants the array to be
-Ask the user what they want the size of each array to be
-Initialize the arrays using the variable, size, that you created earlier.

Once you get that far post your code and I'll help more.

BestJewSinceJC 700 Posting Maven

It's not a problem. Whatever amount of help you need is fine, as long as you're making an effort. Time isn't a factor.

BestJewSinceJC 700 Posting Maven

Make a loop in the main class that is like

while(true)
{
-Accept incoming connection & spawn a new thread for it
}

There may be a better way but when I made a program to accept 4-5 connections at once, this worked well. I'm not sure if continuously sitting in a while loop would be a good idea for a real-world application. (I'm pretty sure it wouldn't be). But that is the idea.

BestJewSinceJC 700 Posting Maven

Error check your code and tell us at what point your code doesn't work and show us the method where it doesn't work. If you can't do that then you won't get any help.

BestJewSinceJC 700 Posting Maven

^ That's a very good solution, I was just going to suggest going through the String in reverse order and printing the current character.

BestJewSinceJC 700 Posting Maven

I'd suggest reading about what static methods and static variables are, then you might start to understand why you are getting those errors, rather than guessing what you need to remove to get rid of the error. If you're using Eclipse it can be helpful because it has a lot of error detection, but you still need to understand why you get the errors that it reports to begin with.

BestJewSinceJC 700 Posting Maven
BestJewSinceJC 700 Posting Maven

That's probably because you didn't import anything. In order to use the class you have to import it. I would guess the import is "import javax.swing.*;". You put imports above your class body.

BestJewSinceJC 700 Posting Maven

You have code that says: validInput = false; Then you said while (validInput), which will never execute because you previously set validInput to false. So no, nothing was ever read into your scores array as far as I can tell.

BestJewSinceJC 700 Posting Maven

If you want help, then identify, specifically, what you are trying to do and why it does not work. We aren't going to scour your program for errors and give you answers. We will help you if you specify what you need help with. Start by dividing your payscale program into logical units. That is, what does it need to do? You've already done so, above:

1. It needs to prompt user for "Hours worked" and "Pay rate"
2. Determine if there is overtime
3. Calculate overtime at "time and 1/2"
4. Calculate regular pay
It must then print out:
1. Total hours worked
2. Regular hours
3. Overtime hours
4. Pay rate
5. Regular pay
6. Overtime pay
7. Total pay

Now, write test code to make sure that those things are working properly. Since your program isn't working, clearly, not all of those work properly. Tell us which one doesn't, and post the code for it. Then we can help you further.

BestJewSinceJC 700 Posting Maven

Undoubtedly it's because you are attempting to read the file incorrectly (for example, you're using the String class' substring method, when you should be reading in a double). But you should use print statements and a catch statement to print out your exceptions/errors.

ballXpos = Double.valueOf(line.substring(19)).doubleValue();

^ doesn't work.

BestJewSinceJC 700 Posting Maven

Yeah, and initially, I didn't realize that was the case

BestJewSinceJC 700 Posting Maven

The class with the buttons has to implement ActionListener. I see that you've implemented the actionPerformed method, which is the method that's called when the button is clicked, but you also have to declare your class as VendingMachine extends JFrame implements ActionListener. In addition, you have to call the addActionListener method on your buttons.

Read this link for more information on ActionListeners. http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html

BestJewSinceJC 700 Posting Maven

I meant GUI package as in graphics package, OpenGL being an example of one, java 3d being another

BestJewSinceJC 700 Posting Maven

Every line starts with a String. So read it in. Then use a while loop to read in each integer on the line until -1 is the last integer you read in. Then repeat this process until you are at the end of the file.

BestJewSinceJC 700 Posting Maven

I don't believe you at all ImBestAtJava. Someone close this.

BestJewSinceJC 700 Posting Maven

Out of curiosity, what are you using for this project (as far as GUI package)? Java 3d?

BestJewSinceJC 700 Posting Maven

Post your code. If you can't, we can't help

BestJewSinceJC 700 Posting Maven

This was the worst attempt at disguising homework I've ever seen. Lol.

BestJewSinceJC 700 Posting Maven

Make it synchronized to avoid memory inconsistency errors