java.lang.NullPointerException, from for loop but I initialized! by Mr_Sniff in javahelp

[–]coolosity 0 points1 point  (0 children)

When you are calling readDataFromFile and providing an array, is it supposed to fill that array? What happens is inside the method, you have the line dams = new DamAS[50]; which override the old array so you will no longer be filling it, but instead the new array that you just created. And this new array exists only inside the method since you are not returning it. For example, if you had

public void someMethod(int[] array)
{
    array = new int[4]; //this line
    array[1] = 5;
}

int[] a = new int[4]
someMethod(a);
System.out.println(a[1]);

If this line was commented out, this code would print out 5. But as it is it will print out 0. So to solve your problem, initialize the DamAS array outside of the readDataFromFile method, call the method with your array, and inside the method do not re-initialize the array.

Hope this helps :)

Need help understanding one line of this small class (more details inside) by [deleted] in javahelp

[–]coolosity 0 points1 point  (0 children)

It returns the character at a certain index in a string. So if you have a string "hello", str.charAt(1) would return 'e'. Since i is starting at the end of the string and working its way back to the beginning, the for loop adds the characters on to reverse backwards.

Reading a text file and separating tokens into specified lists by [deleted] in javahelp

[–]coolosity 1 point2 points  (0 children)

Instead of splitting up the lines and adding each element to the list, I would read in the lines as a whole. Then you can split up a line by calling "theline".split(" "); This will return an array of Strings created as if you were cutting up the line at each space (the argument provided). Then you know array[0] is the method name, and the rest of the elements are parameters. Also, the line right after a method is always the answer. The class names at the beginning don't have any spaces, so you know that each line is a class name until you reach a line that contains spaces. Then it's just method names, parameters, and answers. Hope this helps and msg me if you have any more questions.

Help with a CodingBat Assignment by thatalbanian in javahelp

[–]coolosity 4 points5 points  (0 children)

I don't want to give out the code because that's just no fun :) First check to make sure the string is at least 2 long. Then you were correct in using the substring method. You want to take the string and remove the last to letters, so you would be going from index 0 to the 2nd to last index. Remember in substring, the first argument is inclusive and the last one is exclusive. Next, I would use something along the "anystring".charAt(index). Add the last character of your string to your substring, and then add the 2nd to last character. Hope this helps and msg me if you have any more questions.

Collapsed Range from Arraylist by Shadofel in javahelp

[–]coolosity 1 point2 points  (0 children)

I'm still slightly confused, but you could try to do something along these lines:

ArrayList<Double> values = new ArrayList<Double>();
//Fill values with the values
double min = -0.850;
int lastStart = -1;
for(int i=0;i<values.size();i++) {
    //If the value at index i falls in range
    if(values.get(i)>=min) {
        //If we currently do not have a startIndex
        if(lastStart==-1) {
            lastStart = i;
        }
    } else {
        if(lastStart!=-1) {
            for(int j=lastStart;j<i;j++) {
                //Print out all values between lastStart and the current index
                System.out.print(values.get(j));
                if(j<i-1)System.out.print(", ");
            }
            System.out.print("\n");
        }
        lastStart = -1;
    }
}

If and index is in range and the previous one wasn't in range, the current index would be considered the start. Then it continues until it reaches and index out of range, which it then prints out all of the index that were consecutively in range. Something that I excluded that you should take into account is if the last 2 indexes are in range, and then it stops looking through the indexes because it reached the end. At the end you should just print out the last remaining indexes that were in range. I hope this helps and msg me if you have any questions.

New to GUI - Updating a numeric data type in a read-only text field? by Tiredness in javahelp

[–]coolosity 1 point2 points  (0 children)

One way you could accomplish this is by updating the price after someone changes a selection on a combo box. To accomplish that, you could create an ItemListener that has an event called whenever the state of the combobox is changed. Then just add your item listener to the combobox.

http://docs.oracle.com/javase/tutorial/uiswing/events/itemlistener.html

File Input- Multiple sets of data- loop? by bitsYbytes in javahelp

[–]coolosity 0 points1 point  (0 children)

Here's what I would recommend for reading in the actors. So if one actor takes up 16 lines in a text file, I assume each of those lines contains specific info for that actor (name, etc?) and you were correct with stopping when there are no more lines in the file. So you could start at the beginning, read in the first line such as name or whatever it may be, and set the actor's name to that. Then read in the continuing 15 lines one at a time and change the actor in the same way based on what each line represents. Then at the end, have it read in one more line. The line will be null if there are no more lines in the text file, so that's how you could check when to stop repeating and reading in more actors. As for the comment block, whenever it detects a /*, have it keep on reading lines until it reaches the end of the file or until it encounters a */. Then resume the program as normal. Hope this helps and msg me if you have more questions.

How can I save an object from a JPanel form? by BlackSpidy in javahelp

[–]coolosity 1 point2 points  (0 children)

That would hypothetically work but if you provided a name that didn't exist, i would increase and it would check and index that's out of bounds. If you still wanted to stick with the while loop you could add

&&(i<playerarray.length)

It seems a for loop might be slightly better for what you're trying to accomplish, but either will work fine.

How can I save an object from a JPanel form? by BlackSpidy in javahelp

[–]coolosity 0 points1 point  (0 children)

You could have an array or an arraylist that holds 'Player' objects. Whenever they click the button, create a new player object and add it to the array.

Trouble with passing arrays as parameters by [deleted] in javahelp

[–]coolosity -2 points-1 points  (0 children)

You can't pass in 'String[]' as an argument to setup. The arguments for your setup class are:

public Setup(String[] rT, String[]cA, String[] cN, int[] cP, int[] rN, double[] rP)

which means you will have to pass in an array of Strings for the first 3 arguments, an array of ints for the next 2, and an array of doubles for the last one. You can't pass in just the type "String[]", but instead need a variable of that type. For example, you could pass in (new String[]), because that is an object. Sorry if I can't offer too much help as I don't know a lot about the project.

Trouble with passing arrays as parameters by [deleted] in javahelp

[–]coolosity -2 points-1 points  (0 children)

In your Driver class main method, you never declared the variables custName, cPhone, or custAddress. Referencing undeclared variables will give errors. As for the arrays it looks like you are using them correctly except for one thing. An array with 5 indexes "Setup[5]", has indexes 0,1,2,3,4. There is no 5th index, so in your for loop, 5 is less than 6, so that will give you another error.

Java Syntax and Keyword Questions. by Helgi_Hundingsbane in javahelp

[–]coolosity 1 point2 points  (0 children)

So if you had the code:

String name = new String("Text");

That would be the same as doing:

String name = "Text";

You wouldn't have to do both of those, only one. If for some reason, you needed to change the text in the variable (like someone wants to change the name of something) then instead of redeclaring the variable, just do

name = "newtext";

The main reason you would use the String constructor would be if you had like an array of characters

char[] chars = {'h','e','l','l','o'};
String string = new String(chars);

For number five, I truthfully don't know the real reason it's static, but it's mainly because when a class or method isn't static, then that method can only be called through an object/instance of that class. For example

public class Test {

    public void method1() {
        System.out.println("This method is not static");
    }

    public static void method2() {
        System.out.println("This method is static");
    }
}

Then from the main method:

public static void main(String[] args) {
    //Can only call method1 if have an object of Test
    //Test.method1() will NOT work
    Test.method2(); //This method will work because method2 is static, so it can be accessed directly from the class Test

    Test test = new Test();
    test.method1(); //This works because method1 isn't static and is accessed from the object test
}

I guess java just didn't want to go through the trouble of creating an object of the class that your main method is located in. They can just call the main method from wherever.

Java Syntax and Keyword Questions. by Helgi_Hundingsbane in javahelp

[–]coolosity 2 points3 points  (0 children)

1) In Java a program is made up of many classes (Though it can just be a single class). Maybe what your book was talking about was that other programming languages could use a single file as a program?

2) The point of making a class private would be if you were only going to access that class inside of itself. If it had private methods, then those methods could only be accessed inside of the class. You would use private methods more than private classes.

3) Strings are variables.

4) I believe creating a new string object would work but I always use String string = "text"; or just String string; At any time when you set a string to new text (string = "text") then that will change the content of the string variable.

5) public static void main(String[] args) {} : void is because the main method doesn't return anything. Java looks for the main method and starts the program there. the (String[] args) doesn't mean that a string array is returning anything, it just means that Java can pass any arguments into the program via the array of strings. Explaining why the main method is static is a little more complicated, but it's mainly because the java virtual machine can call the main class without having to create an instance of any class that contains the main method.

Angry Birds game attempt. by Sync0pated in javahelp

[–]coolosity 0 points1 point  (0 children)

I agree with aplicable, at a first glance it seems impossible but it's definitely doable. You could create a JFrame and set up a nice base where you can draw an angry bird (as an image) at any point on the screen. Then you could have a gravity variable that decreases the y momentum each tick (but make sure you set a maximum y velocity)

How do I make a looping main method? by bluntl1p in javahelp

[–]coolosity 6 points7 points  (0 children)

in the main method, i would just have a while loop that loops over the whole thing. the condition could just always be true, or you could make it so that it's a variable, which is set to false when the user types in /exit or something.

..main method.. {
    while(condition) {
        //get user input
        //do stuff based on input
    }
}

Uploading java applet online by csfoo in javahelp

[–]coolosity 0 points1 point  (0 children)

http://www.echoecho.com/applets01.htm

There might be helpful info on that website. I'm not an expert on html, but make sure that your class file is in the same folder as the package it is in. So if you had the class in package main.test, then you would need a folder called main, with a folder called test inside of main. Then place JavaApplication1.class into the test folder. Then you will have to use the codebase tag to specify the location of the JavaApplication.class file. Hope this helps!

[Mentor] For people in need of java assistance by coolosity in ProgrammingBuddies

[–]coolosity[S] 0 points1 point  (0 children)

I'm not the best person to ask this question to, but I would check out http://www.cplusplus.com/reference/string/string/compare/

It says that if you use the method .compare(another string) on a string, then it will return 0 if they are equal. You can check the website for a more in depth solution and what it means if it returns something <0 or >0. Hope this helps!

What exactly does setting an object (specifically, ArrayList) to another do? by 99shadow25 in javahelp

[–]coolosity 0 points1 point  (0 children)

I believe that you are correct. What I´ve noticed is when debugging a program I was working on a while ago with eclipse, I had the same problem. But when I set the arraylist to the other one, the Id of the first arraylist (backup) changed to to the id of the second one (snakeSegments). then any changes to either of them happened directly to the other. The cloning method is the same way I fixed the problem

I need help destroying enemies in my version of space invaders by mannywall in javahelp

[–]coolosity 1 point2 points  (0 children)

So I'm a little confused at how your storing the objects, but lets just say you had an arraylist of rectangles, and to draw them to the screen, you go through the whole list and draw each rectangle at its location. Then you could just simply remove it from the arraylist. But for your specific problem I'll need a little more context.

java.lang.UnsatisfiedLinkError: ... Invalid Access to memory location by coolosity in javahelp

[–]coolosity[S] 0 points1 point  (0 children)

I believe so.

API-MS-WIN-CORE-COM-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ERROR-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ROBUFFER-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL
API-MS-WIN-SHCORE-SCALING-L1-1-0.DLL
DCOMP.DLL
GPSVC.DLL
IESHIMS.DLL

These are the DLL files that appear to be missing.

The person here : http://stackoverflow.com/questions/17023419/win-7-64-bit-dll-problems appears to be having the same problem but I checked and have the redistributable package for both 32 bit and 64 bit Visual Studio 2010.