BestJewSinceJC 700 Posting Maven

If you're asking if you can output the contents of the JTable into the file, then yes, you can. Using two for loops (nested/one inside the other) go through each row and each column and print whats at the index into the text file. Use a PrintWriter to output to the file.

BestJewSinceJC 700 Posting Maven

There are also a lot of jobs that are related to computer programming, that you might not think of if you are focusing strictly on coding. Some people here who are in industry might be able to give more examples, but here are some topics to look into:

Software configuration management
CMMI
Testing
Software Design (& User Centered design)

BestJewSinceJC 700 Posting Maven

Ok, now I understand. Imagine a Node that has two attributes. The first attribute will be the value, which is an Integer. The second attribute will be another Node. (This Node will be the 'next' node, which is what they are calling the link). So basically if you had four nodes, containing: 1 2 3 4, (I'm just going to call the Nodes node1, node2, node3, and node4). So the first Node would be '1', and it would have a link to node2. The Node that contains 2 would have a link to node3. So now lets say that we want to remove a particular Node, node3. Your book is saying that you would need to find the Node before node3. In this case, that is node2.


Your book is saying that in order to remove node3, you would need to use node2.removeNodeAfter(). The code for removeNodeAfter() would look like this:

public void removeNodeAfter(){
link = link.link;
}

Explanation: Since we called the method with node2.removeNodeAfter(), "link" is the variable from node2. Remember that "link" contains the next Node. In this case, the next node is node3, so link is node3. Then link.link is the same as saying node3.link, which is node4. So saying link = link.link; is making node2 point to node4. Essentially, we have just deleted nodeThatContains3.

If that explanation didn't make sense, keep this in mind: In my example, I called the method with node2. So "link" refers to …

BestJewSinceJC 700 Posting Maven

[Warning: This is coming from a Java programmer with no C++ experience]

Assuming that clicking 'X' generates an event, and C++ has method overriding - override the method that closes the window when the user clicks 'X', and do nothing in that method. That way, nothing will happen when the 'X' is clicked.

BestJewSinceJC 700 Posting Maven

What? Be more specific and explain your problem fully

BestJewSinceJC 700 Posting Maven

In your paint() method, call super.paintComponent(g). It will clear the screen. Alternatively, you could use this piece of code from inside your paintComponent method:

g.clearRect(0, 0, this.getWidth(), this.getHeight());
BestJewSinceJC 700 Posting Maven

For the first error, you're either using a constructor that does not exist, or you didn't properly import the class.

BestJewSinceJC 700 Posting Maven

No, it would not, because I declared "ints" as an array of Objects. . not as an array of Integers. Then, knowing it was actually storing Integers, I casted the value that was at the index to an Integer. It runs correctly.

BestJewSinceJC 700 Posting Maven

If you must, cast each element individually to the appropriate type.

public static void main(String[] args) {
	
		Object[] ints = new Integer[10];
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(1);
		ints = list.toArray();
		System.out.println((Integer)ints[0]);
		
	}
BestJewSinceJC 700 Posting Maven

peter - I looked at the compareTo method, it does the same as getTimeInMillis basically, except it compares the two times in millis for you.

OP - if you did firstDate.compareTo(secondDate) it would return a value indicating which one was greater. Have a look at the documentation.

BestJewSinceJC 700 Posting Maven

You have a syntax error. Is rosterList an ArrayList? If so, look at the get method here. You'd have to use return rosterList.get(yourIndexHere);

If rosterList is a two dimensional array, then you would have to use return rosterList[row][column].

And the reason for your error is because when you put the brackets [], that means you are trying to get the index of an array. But there is no array there.

BestJewSinceJC 700 Posting Maven
public Object getValueAt(int row, int col) 
        {    
            return rosterList.get(row);
            for(int i = 0; i < 28; i++) <------- unreachable statement error
            {
                return col = i;
            }
        }

You said return rosterList.get(row) which causes whatever element is at rosterList.get(row) to be returned. When you return something, you are literally "returning" to wherever you were before (so if you called getValueAt from the main method, you'd return to main). In other words, the method you are in exits -- and since the method exits before you reach the for loop, it is giving you an unreachable statement error.

BestJewSinceJC 700 Posting Maven
BestJewSinceJC 700 Posting Maven

You both do realize that we can't access area 51 right?

They're aware. If it's anything like most sites, not a whole lot goes on in the employees only area/mod lounge/whatever its called anyway. (Although the site I adminned only has 100,000 members and this one has 5x that, so I'd imagine this one has a larger staff).

BestJewSinceJC 700 Posting Maven

You can mark the thread as solved by clicking the link at the bottom of the thread that says "mark as solved". It should be a blue link underneath the very last post, next to the yellow button that says 'Reply to Thread'. Basically, it's just a way for the person who originally created the thread to acknowledge everyone who posted in it. It's also helpful because then people will know the thread is solved. If you can't find it though, or if for some reason it isn't there, don't worry about it, its no big deal. To me, its cool just to know that I was able to help someone solve their problem and learn more programming in the process

:)

BestJewSinceJC 700 Posting Maven

Without looking at your code, (gotta be somewhere soon) I'd recommend that you use compareTo to compare the two Dates to each other. If you aren't using the Date class to store your dates, look at the compareTo method for whatever class you're using

BestJewSinceJC 700 Posting Maven

for(i = a; i >= 0; i--) // move all the integers by one
b[i+1] = b;


You are getting your error because b = a, (you declared the array b as being size a), and you're setting i to a also. So "i + 1" is greater than the size of b.

(i + 1 is a bigger index than b can hold)

BestJewSinceJC 700 Posting Maven

As hard as I tried, I couldn't find anywhere where you told us where the problem is. Since we can't read your mind this might be useful information.

BestJewSinceJC 700 Posting Maven

public double getRestockingFee() // calculate restocking fee
{
return super.getValue() * .05;
}

I'm personally very busy right now, so i apologize if you knew this already, but you are calling the method from your parent class when you use super.getValue() . . not the method from the class you are in. I just don't see a reason why you'd want to do that, considering you overrode the getValue method, is why I even mention it.

wedunnit commented: Great explanations and help! Problem solved! +1
BestJewSinceJC 700 Posting Maven

Yes but the DOWN key only wil make it move to the next line. The ENTER key is not supposed to make it move to next line in the textarea right? My problem is that when i press ENTER, the text in textarea move to lblDisplay but the cursor in textarea moves to next line after ENTER. How can i fix that?

I just told you. Use the setCaretPosition method. I'm pretty sure if you get the length of the text that is in the text field, then set the caret position to be at that length, then it will be sitting after the last character that the user typed. I've never used the method, but hey.. you're asking for solutions so try stuff out when it's suggested. The reason that typing enter is jumping down a line is pretty obvious. . that's what the enter key does.

BestJewSinceJC 700 Posting Maven

It should say power = po; in the Lamp(boolean) constructor. That sets the power to whatever value (true or false) that was passed into the constructor. Consider this example:

public class ExampleClass{
boolean something = false;

public ExampleClass(boolean value){
     something = value;
}

public static void main(String[] args){
     new ExampleClass(true);
}

}

^ The example above will work, because what's on the left side of the "=" sign gets it's value set to whatever is on the right of the "=" sign. So if the constructor was

public ExampleClass(boolean value){
     value = something;
}

It would just set value (the parameter of the constructor), to whatever something was. So since something = false (where it says boolean something = false), we'd be setting value to false, which is not what we want to do.

BestJewSinceJC 700 Posting Maven

I don't know about your second question, but the reason it is moving to the second line of the Text Area is because you are appending \n to the text area, which just adds a new line. If you wanted to set the text to something else, use the setText method. If you want to set the position of the cursor, I believe the setCaretPosition method will help you there. http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)

BestJewSinceJC 700 Posting Maven

Error 1: The DVD class does not have a method called getItemValue, hence the error. You have to write the method.

Error 2: This isn't actually an error. Once you correct the previous error, it will go away, I believe.

BestJewSinceJC 700 Posting Maven

Like the error message says, you're trying to call a method that takes int,int,int but you're passing int,int,int[]. So either re-declare the method as taking an int[] as one of the parameters, or call the existing method correctly.

BestJewSinceJC 700 Posting Maven

You still have a lot of errors.

  1. The "main" method should be declared as public static void main(String[] args), and this declaration should never be changed to something else. The reason is because it is just a standard that main is the method which will run when you start the program, so changing it's usual declaration is confusing.

  2. You said DVD[] DVD = items, which might work, but does not make sense. Delete it, and instead of saying DVD[0] = whatever, say items[0] = whatever.

Change those couple things and then tell me what errors you are getting. And re-post your code using the Java code tags. To do this, just click everything you normally would, but then when it says (CODE) change it to (CODE=Java) (only for the first CODE tag)

BestJewSinceJC 700 Posting Maven

Not supposed to give away code, as this doesn't really help you learn, but any explanation I give you will be much more complicated than the code itself is, and might confuse you, so here is how you do it.

public String toString(){
String s = "";
if (power==true){
s = "Power: ON";
} else{
s = "Power: OFF";
}
return s;
}

All that code does is has an if statement that decides if power was true, and if so, sets a String to say "Power = on". Otherwise, the else statement gets executed and it says "Power = off". Also, please consider that the toString() method is supposed to convey the meaning of whatever Object you have. So the toString() method of a Car class might tell you what type of Car it is (Toyota, Honda, etc). So I'd recommend only override the toString method (what you are doing is called overriding, since the Object class already defines a toString method) if what you're doing actually describes the Object.

BestJewSinceJC 700 Posting Maven

You can fix it by declaring the total2 variable. You declared total, you need to do the same thing for total2. You also need to read some java tutorials and learn to fix obvious things like this yourself.

double total2 = 0;

BestJewSinceJC 700 Posting Maven

So you have an array of your numbers? I'm assuming they are integers? Then use

int result = array[index] - the mean;

Your first number is array[0], second number is array[1], and so on. If you had an array that was declared as int[] array = new int[3]; your 3 numbers would be stored at array[0], array[1], and array[2].

BestJewSinceJC 700 Posting Maven

Read
http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

Particularly, where it defines what an abstract method is.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon). . .

Then compare that definition to how you have coded your method. You'll notice that you cannot declare a method "abstract" while also defining the method body. So either get rid of the "abstract" and the semicolon, or get rid of the brackets and the return picture.

Invalid:

abstract ImageIcon getPicture();
{
	return picture;
}

Valid:

abstract ImageIcon getPicture();

Valid:

ImageIcon getPicture()
{
	return picture;
}
Laidler commented: Helped me fix my abstract method error +1
BestJewSinceJC 700 Posting Maven

for(int i = 0; i < n; i++)
{
double total2 = total/n;
double[] all2 = Math.sqrt(total2);
}

where in the formula does it say to take the average over and over again? You're only supposed to take the average once when you are finished with the other stuff.

1. Compute the mean (average) of all of your values
2. Compute (your number-the mean) squared. Do this for every number in your list.
3. Add the numbers from step 2.
4. Divide the total from step 3 by the number of numbers you had in your original list.
5. Take the square root of the total in step 4. This is your answer.


Step 4 should not be done inside a for loop, otherwise, it will be done over and over again, when the formula requires that it is done only one time.

BestJewSinceJC 700 Posting Maven

You can't instantiate an abstract class or call an abstract method as far as I know. What exactly are you trying to do? Post the code where you're trying to "get the abstract method to work".

BestJewSinceJC 700 Posting Maven

Your formula for standard deviation only works for an entire population. If you're taking the standard deviation of a sample, then you need to divide by (n-1), not by n. (Note: This might not be important to you, depending on what your teacher said.)

In any case, it looks like you are not following either formula properly (you're leaving parts out), read the "basic example" and you will immediately see what parts you are missing. .

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

BestJewSinceJC 700 Posting Maven

I don't know too much about KeyListeners, but

if ((e.getKeyCode() == KeyEvent.VK_ALT) && (e.getKeyCode() == KeyEvent.VK_ENTER)) {
				txtArea.setText(txtArea.getText());
				txtArea.setText("\n");
			}
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				lblDisplay.setText(txtArea.getText());
				txtArea.setText("");
				txtArea.requestFocus();
			}

doesn't look like it could possibly work. If the first of those statements was true, then the second one would always be true as well. . and don't you only want one of those two things to be executed? You might want to read the Java Sun tutorial on KeyListeners. Furthermore, this statement txtArea.setText(txtArea.getText()); does nothing. You just got the text from txtArea, then set the text in txtArea to what it already was. Why? If I were you I'd check to make sure that the code inside those if statements is actually being reached.

BestJewSinceJC 700 Posting Maven

From where you're currently at, there really isn't much need to erase everything. You just need to put an array in the Invent2 class, then add each dvd to the array after you create it.

Invent2{

//declare an array of DVDs here

main method(){
dvd = new DVD(1, "Pulp Fiction", 8, 12.99);
array[0] = dvd;
System.out.println(dvd);

dvd = new DVD(2, "Resevoir Dogs", 9, 12.99);
array[1] = dvd;
System.out.println(dvd);
}

}

The reason I said you didn't need three classes is because the Inventory class isn't really necessary. You can take all the code you were going to put in Inventory and put it in Invent2 instead. Since all the Inventory class does is contain an array that holds your DVDs, it makes more sense to combine this with the other class.

Also, I apologize if I confused you with my multiple posts. I'm kind of tired and at one point I briefly gave you incorrect information, although I don't think you saw this information (as it was only there for a minute or two).

BestJewSinceJC 700 Posting Maven

Every time you set dvd = new DVD(), you are overwriting what you previously put into dvd. Again, consider this example:

DVD[] allMovies = new DVD[10]; -- This statement creates an array big enough to hold 10 DVDs.

DVD movie = new DVD(lionKing); -- This statement creates a new variable of type DVD called "movie". It then puts the DVD lion king into the movie variable. (What it actually does is a bit more complex, but this is what it amounts to)

movie = new DVD(granTorino); -- Now we just put gran torino into the movie variable. So where is lion king now? Nowhere.

BestJewSinceJC 700 Posting Maven

Another student in your class posted on this forum not too long ago. At least I'm assuming that's the case, since you guys basically have the same exact program other than the movie names. Anyway, your problem is that you are overwriting your dvd in the Invent2 class.

edit: Nevermind. That was you. I can't really improve the advice that I gave you originally, but you did not follow it. To do this program, you should only need two classes: the class where you create the DVDs, and the DVD class. I already told you how to create a DVD and put it into the array:

public class DVD{
DVD[] dvds = new DVD[10];

public static void main(String[] args){
DVD dvd = new DVD(whatever);
dvds[0] = dvd;
DVD dvd2 = new DVD(aDifferentDVD);
dvds[1] = dvd2;
}

}
BestJewSinceJC 700 Posting Maven

Well explain what, specifically, you need help with. i.e. X method ]doesn't work, and it is supposed to do Y. Or I need help with reading in the file. etc.

BestJewSinceJC 700 Posting Maven

Yeah sorry, I probably should have told you to look at the Scanner javadoc, since I could not remember what the exact constructor was. I really meant to point you in the direction of Scanner though, not give you exact code, although in this case I should have looked it up or mentioned that I wasn't sure. Anyway, like javaAddict said, you can use a File object to do it properly. That and other ways are shown here

BestJewSinceJC 700 Posting Maven

Well, you declared a double[] doubleArray. So you have to change the method header of the total_average method to double[], not to double, since you want to pass in an array of doubles. Then in your main method just say

total_average(doubleArray);

BestJewSinceJC 700 Posting Maven

I don't know what the name of your class is, but I do think I know why you are getting static reference errors.

You are in a static method, main (look at the declaration of the method - it says static), and you are probably trying to put things into an array you declared in your class body (so inside of your class, but not within any method) that is not declared as static. By default, anything that is not declared as static is automatically declared by the compiler as public, and therefore is not static. Consider the purpose of static-ness (static methods or static variables) to begin with. Static methods and variables have one copy for the entire class, whereas every time you create a new Object, that Object gets a new copy of each of the non-static class variables. So with that in mind, consider that your static main method cannot do anything with non-static variables, since you have to make an Object for them to even exist! And with that in mind, read this for any more information, http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

Also, as far as your code in general: Your input file has two things per line, an X and a Y. Using a 2-D array is not a bad idea at all. You should use the Scanner class's nextInt() method to read in each X and Y. The way the nextInt() method works, it would just give you whatever the nextInt was in the …

BestJewSinceJC 700 Posting Maven

For testing whether or not a Character is a digit, you can use the Character class's method isDigit(). So, for example,

String s = stdin.readLine();
for(int i = 0; i < s.length(); i++){
    if (s.charAt(i).isDigit()){
       System.out.println("It's a digit!");
    }
}

But for your purposes, if you don't want any "weird" characters in your String, you should check to see if each character is anything other than a-z, A-Z, or ' and any other character that are allowed. This will obviously mean less code and more efficient code. Java uses the Unicode character set, so look into that to figure out how to code something that will basically say

if (myCharacter < a || myCharacter > z)

BestJewSinceJC 700 Posting Maven

I think he wants to do something more like
int total = 0;
//where array is an array of strings
for (int i = 0; i < array.length; i++){
total+=Integer.parseInt(array);
}

Ezzaral commented: Yeah, I think you're right. +21
BestJewSinceJC 700 Posting Maven

And the unofficial way would be to put the code in a method, then call the method in the appropriate places.

BestJewSinceJC 700 Posting Maven

Good observation Vernon. Man, I'm slacking lately. It's those exams coming up.

:(

BestJewSinceJC 700 Posting Maven

You can do all of this in one class if you really want to, but there is no need to do so - you can just as easily do it the way you have things set up. The way to make a button do something when it is clicked is to have your class implement ActionListener. Then in the actionPerformed method, you would do textFieldName.setText("text or variable name in here");

See this for more information and examples. If you don't know about ActionListener, or Listeners in general (which it appears you don't by looking at your classes) read the link they provide to ActionListener.

BestJewSinceJC 700 Posting Maven

You can use the removeElementAt method to remove the last item. To know where the last item is, your program can either keep track of the size of the Vector, or you can use the Vector class's size() method. http://java.sun.com/javase/6/docs/api/java/util/Vector.html

BestJewSinceJC 700 Posting Maven

Yeah, there is a difference between finding it by accident and looking for it. Although I have heard not to mix the two before (perhaps on this forum?) it is hardly ingrained in my mind, therefore I probably never would have seen it.

BestJewSinceJC 700 Posting Maven

wow, Ezzaral, that is a pretty amazing catch. I was going to fool with the code a little more tonight, but. . unnecessary now. Doubt I would have ever noticed that anyway.

BestJewSinceJC 700 Posting Maven

It's painful to try to separate the view from the controller in a typical Swing UI. In practice it almost always compacts down to a UI / Business Objects(s) 2-layer model.
J

It seems to be practically the same thing with three tiered architecture. At least for small applications.

BestJewSinceJC 700 Posting Maven

GMT-5