Creating a Gender field [closed] - java

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 made following code for Gender field in a form and i can't find my error. Any help is appreciated
Gender =
if (male.isSelected()) Gender="Male";
else if(female.isSelected()) Gender="Female";
I am new to NetBeans and this Site. So Please Help Me
I get the error in if statement

You should not initialize String with if instead use :
String gender = "";
if (male.isSelected()) {
gender = "Male";
} else if (female.isSelected()) {
gender = "Female";
}

Related

Tried Java reflection's ".getDeclaredField" method but still meet "NoSuchFieldException" [closed]

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
I'm trying modification of an instance's fields through Java reflection.
Here is my original code.
for(Field f: customerClass.getDeclaredFields()) {
System.out.println(f);
}
System.out.println("\n");
System.out.println(customerClass.getDeclaredField("firstName"));
for(int i=0; i < columnNames.length; i++) {
System.out.println(columnNames[i]);
field = customerClass.getDeclaredField(columnNames[i]);
field.setAccessible(true);
And the results.
private java.lang.String Customer.firstName
private java.lang.String Customer.lastName
private java.lang.String Customer.firstName
"firstName"
java.lang.NoSuchFieldException: "firstName"
at java.lang.Class.getDeclaredField(Class.java:2070)
I am wondering why "customerClass.getDeclaredField("firstName")" works, but "customerClass.getDeclaredField(columnNames[i])" throws an Exception, since columnNames[0] == "firstName".
If you'll look at your output for columnNames[0], you'll see that it's not firstName, it's "firstName". Remove the quotes and it should work.

'Not a statement', '; expected' [closed]

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’

Difference between String and string [closed]

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).

How to initialize a String array with unknown length to null? [closed]

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"));
}

I need to get two numbers of a string like like this "5+7", but when im using .split(\\+) only get the first, how can i get both? [closed]

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.

Categories