Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'mm working on this program in which connects Prolog and Java GUI. And i am encountering this problem where i don't know how many solutions the prolog program is gonna pass to java and therefore i can't declare the String array with fixed-length. This is my code:
String[] options; int i;
Query qMeat = new Query(new Compound("meat", new Term[] {new Variable("X")}));
i = 0;
while(qMeat.hasMoreSolution()){
options[i] = "" + qMeat.nextSolution().get("X");
i++;
}
I am getting this NullPointerException, which i guess because i didn't initialize the String array to null. And i don't know how to do so. I tried java.util.Arrays.fill(options,"") But not helping =(
Please help.
If you don't know the required size of the array in advance, you should use an ArrayList instead.
List<String> options = new ArrayList<>();
while(qMeat.hasMoreSolution()){
options.add("" + qMeat.nextSolution().get("X"));
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This might be a bit of a stupid question, but I can't figure this out! I'm working on a minigame that involves one player being randomly selected as the terminator, and all the rest as weaklings. I have an ArrayList of players, and I can get it to choose the terminator, and then I need it to copy players into weaklings, but skip terminator. The code I'm using doesn't copy anything at all into weaklings. Here's the code I'm using, could someone a bit more experienced help me out please:
for (int i = 0; i < players.size(); i++ ) {
if (!players.get(i).contentEquals(terminator)) {
players.add(players.get(i));
}
}
System.out.print("The other players are: " + weaklings + ".");
}
You add to the players again: players.add, instead of weaklings.add.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am getting both these errors (on the marked line) with the following code in java;
String data[] = file.getInput();
while(data[0] != "X"){
String ID = data[0];
String firstName = data[1];
String lastName = data[2];
data[] = file.getInput(); //errors occurr here
}
Note that file.getInput() is a method that returns an array of Strings from a CSV file using the InputReader.
Just remove the [] from data.
data = file.getInput();
Remove the ‘[]’ from data :
data = file.getInput(); ‘Do the statement like this’
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
When using Java, what is the difference between the two following assignments:
String upperCaseDataType = "myName";
string lowerCaseDataType = "myName";
Do the 2 mean the same at compile time, just like in C#?
Thanks very much for your help.
string is not a class or type in Java
No. string s = "myName"; is not legal Java, and will not compile. Also, those are assignments (not assertions).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
String x = "5+7";
String []n = x.split("\\+");
System.out.println(n[0]); // =5
System.out.println(n[1]); // =\
Your code should work fine, but I'd try to make it more robust:
String test = "5 + 7";
String[] tokens = test.split("\\s*\\+\\s*");
for (String token : tokens) {
System.out.println(token);
}
The \\s* will allow for possible white-space between the numbers and the + char.
use
x.split("[+]");
That will split it correctly.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I guess its a rather simple question but i just cant find my mistake.
int[] myIntArray = new int[20];
myIntArray[5] = 5;
int a = myIntArray[5];
TextIO.putf("arr[i]: d%",a );
The error I get is Illegal format string in TextIO.putf() method.
So I assume the value at the index 5 is not an int?
The error message says exactly what the problem is: your format string is wrong. You probably meant %d (or better yet, %d%n to add a newline).
The format string elements are in the form
%[modifiers]type
not
something%
Change d% to %d.