I am having trouble converting an int to string.
I use java to connect to a database (Access).
Now I'm trying to retrieve the value from a YES/NO Field Access but it'sa check box
jlbABC.setText(rs.getString("RightWrong"));
The answer I get is 1 if it is True(YES), and 0 if is False (NO)
How do I change the answer from 1 to YES and 0 to NO?
I don't understand at all what you want, but here is how I normally convert any integer into a string literal.
String values[] = new String[]{"NO", "YES"};
int myValue = ... wherever it comes from;
String myConvertedValue = values[myValue];
You may also check if myValue is in bounds of the array of values.
If you are trying to set the value of a string based on if it is a 0 or a 1 you could use this.
int i = rs.getInt("RightWrong");
String yesno = i == 1 ? "YES" : "NO";
This has the benefit of saying that the result will be "YES" if the value is 1 or "NO" otherwise.
Related
String s1 = sh.getString("name", "");
int a = sh.getInt("age", 0);
For this code I would like to show empty spaces for s1 and a if there is no input entered. When I run the application String s1 it shows empty space but int a showd 0 since the default value is 0.
Is there any way to show empty space instead of 0?
I have tried puting
Integer.parseInt("")
But it was not working.
Is there any solution for this?
Thank you.
You can add a condition to check if it is 0 or not. Using null as suggested by answer by Frank can be used but it is not preferred. Instead you can try this:
String text = a == 0 ? "" : String.valueOf(a);
textView.setText(text);
button.setText(text);
You just n need to parse it as string you can't check empty if you are using int variable So just get your age as String and also send it with string via intent you can also parse it as int after getting it from intent
Like option 1
Intent intent =new Intent(this,B.class);
intent.putExtra("name", name);
intent.putExtra("age", age.toString);
Here you can get Like
String s1 = sh.getString("name", "");
String age= sh.getString("age", "");
Here you can check age is empty then if you want this value as int you can Parse it as int a=Integer.parseInt(age);
Hope this will help you
I'm trying to accept a user field in the form of a jTextArea (Search box). Then take this text and compare it against an ID OR Name and return if its inside of any.
Compare User Entry to ID
Compare User Entry to Name
Essentially check the user entry against a String and an Int.
I've got the following however am getting NumberFormatException.
String name = "Window";
int id = 12;
if (name.contains(searchText.getText().toLowerCase()) || id == Integer.valueOf(searchText.getText().replaceAll("[^0-9]", ""))) {
// TRUE STATEMENT
}
So if a user enters "Win" it will return true. If they enter "test" it will return false. However if they enter "1","2" or "12" it will return true since the ID contains these.
I think I'm overthinking this one and could use some help.
Thanks in advance
if (name.toLowerCase().contains(searchText.getText())
|| Integer.toString(id).contains(searchText.getText())) {
System.out.println("TRUE");
}
so in my program the user has to register and one of the fields to enter is the phone number.
I want to check if the first 3 numbers they enter is valid or not
if (TextUtils.isEmpty(PhoneNumber) || PhoneNumber != "055, 050, 056, 052") {
PhoneNumber.setError("Please enter a valid phone number")
return;`
}
this is my code but it set off so many errors, can somebody please help me with this?
PhoneNumber != "055, 050, 056, 052" is wrong.
You can create a string array and store them there.
String okNums = {"055", "050", "056", "052"};
and then check if the phone number is valid
if (TextUtils.isEmpty(PhoneNumber) ||
!Arrays.asList(okNums ).contains(PhoneNumber)) {
PhoneNumber.setError("Please enter a valid phone number")
return;`
}
turn your pattern into an array and search if the phone is beginning with any of those
String d = "055, 050, 056, 052";
String p = "055 66";
String[] pArr = d.split(",");
for (String patt : pArr ) {
System.out.println(patt.trim());
System.out.println(p.indexOf(patt)==0);
}
//Get the string from the edit text by:
String number = yourEditText.getText().toString();
if(number != null && number.matches("639[0-9]{9}"))
//do what you need to do for valid input
else
//do what you need to do for invalid input
matches() ensures that the entire string cooresponds (exactly) to the regular expression that it takes. 639[0-9]{9} says that the string must start off with 639 and then be followed by exactly 9 digits (0-9). If you wanted to match "639" followed by 7 to 9 numbers, for example, you would use: 639[0-9]{7,9}. Regular expressions:
A simple solution would be .startsWith()
if (!PhoneNumber.startsWith("055") || !PhoneNumber.startsWith("050") || !PhoneNumber.startsWith("056") || !PhoneNumber.startsWith("052")) {
// does not match
}
Another option is to use a regex for this
if (!PhoneNumber.matches("^(055|050|056|052)\d+$")){
// does not match
}
The worst thing you had done is, your comparison. You can't compare like this, when you need to compare with several values. Instead you have to use something like List#contains method to check whether your PhoneNumber or not.
String[] check = {"055", "050", "056", "052" };
if (TextUtils.isEmpty(PhoneNumber) || Arrays.asList(check).contains(PhoneNumber.getText().toString())) {
PhoneNumber.setError("Please enter a valid phone number")
return;
}
Alright so from what I understood here is a solution, if the phone number has is a one full string you can extract the first 3 numbers using this:
String s="052487978";
String ex = s.substring(0,3));//052
Create a int array and store all the valid options in there
int[] array = {055, 050, 056, 052}; and covert the string ex to int int value = Integer.parseInt(ex); and check if its available in the string that contains valid options.
boolean contains = IntStream.of(array ).anyMatch(x -> x == value);
If it is true its valid and if its false its invalid.
In my Oracle table there are columns with different type.
I want to read all columns as number.
String str = resultSet.getString("col1");
The problem is that if column in database is defined as number, and value is
0.5
the returned string will be
.5
I can not use any other getter like getDecimal() and etc.
If I use:
String str = resultSet.getObject("col1").toString();
I'll get an exception if the value is null.
You could use
String str = String.valueOf(resultSet.getObject("col1"));
as a simple workaround to avoid any exceptions. (Not sure why you can't use resultSet.getDouble("col1") though.)
If you don't want to see an empty string rather than the literal "null" for a null value (which is what String.valueOf()) will produce, you can use:
Object value = resultSet.getObject("col1")
String str = value == null ? "" : value.toString();
I'm curious why the String.indexOf is returning a 0 (instead of -1) when asking for the index of an empty string within a string.
The Javadocs only say this method returns the index in this string of the specified string, -1 if the string isn't found.
To me this behavior seems highly unexpected, I would have expected a -1. Any ideas why this unexpected behavior is going on? I would at the least think this is worth a note in the method's Javadocs...
System.out.println("FOO".indexOf("")); // outputs 0 wtf!!!
System.out.println("FOO".indexOf("bar")); // outputs -1 as expected
System.out.println("FOO".indexOf("F")); // outputs 0 as expected
System.out.println("".indexOf("")); // outputs 0 as expected, I think
The empty string is everywhere, and nowhere. It is within all strings at all times, permeating the essence of their being, yet as you seek it you shall never catch a glimpse.
How many empty strings can you fit at the beginning of a string? Mu
The student said to the teacher,
Teacher, I believe that I have found the nature of the empty string. The empty string is like a particle of dust, and it floats freely through a string as dust floats freely through the room, glistening in a beam of sunlight.
The teacher responded to the student,
Hmm. A fine notion. Now tell me, where is the dust, and where is the sunlight?
The teacher struck the student with a strap and instructed him to continue his meditation.
Well, if it helps, you can think of "FOO" as "" + "FOO".
int number_of_empty_strings_in_string_named_text = text.length() + 1
All characters are separated by an empty String. Additionally empty String is present at the beginning and at the end.
By using the expression "", you are actually referring to a null string. A null string is an ethereal tag placed on something that exists only to show that there is a lack of anything at this location.
So, by saying "".indexOf( "" ), you are really asking the interpreter:
Where does a string value of null exist in my null string?
It returns a zero, since the null is at the beginning of the non-existent null string.
To add anything to the string would now make it a non-null string... null can be thought of as the absence of everything, even nothing.
Using an algebraic approach, "" is the neutral element of string concatenation: x + "" == x and "" + x == x (although + is non commutative here).
Then it must also be:
x.indexOf ( y ) == i and i != -1
<==> x.substring ( 0, i ) + y + x.substring ( i + y.length () ) == x
when y = "", this holds if i == 0 and x.substring ( 0, 0 ) == "".
I didn't design Java, but I guess mathematicians participated in it...
if we look inside of String implementation for a method "foo".indexOf(""), we arrive at this method:
public int indexOf(String str) {
byte coder = coder();
if (coder == str.coder()) {
return isLatin1() ? StringLatin1.indexOf(value, str.value)
: StringUTF16.indexOf(value, str.value);
}
if (coder == LATIN1) { // str.coder == UTF16
return -1;
}
return StringUTF16.indexOfLatin1(value, str.value);
}
If we look inside of any of the called indexOf(value, str.value) methods we find a condition that says:
if the second parameter (string we are searching for) length is 0 return 0:
public static int indexOf(byte[] value, byte[] str) {
if (str.length == 0) {
return 0;
}
...
This is just defensive coding for an edge case, and it is necessary because in the next method that is called to do actual searching by comparing bytes of the string (string is a byte array) it would otherwise have resulted in an ArrayIndexOutOfBounds exception:
public static int indexOf(byte[] value, int valueCount, byte[] str, int strCount, int fromIndex) {
byte first = str[0];
...
This question is actually two questions:
Why should a string contain the empty string?
Why should the empty string be found specifically at index zero?
Answering #1:
A string contains the empty string in order to be in accordance with Set Theory, according to which:
The empty set is a subset of every set including itself.
This also means that even the empty string contains the empty string, and the following statement proves it:
assert "".indexOf( "" ) == 0;
I am not sure why mathematicians have decided that it should be so, but I am pretty sure they have their reasons, and it appears that these reasons can be explained in layman's terms, as various youtube videos seem to do, (for example, https://www.youtube.com/watch?v=1nBKadtFViM) although I have not actually viewed any of those videos, because #AintNoBodyGotNoTimeFoDat.
Answering #2:
The empty string can be found specifically at index zero of any string, because why not? In other words, if not at index zero, then at which index? Index zero is as good as any other index, and index zero is guaranteed to be a valid index for all strings except for the trifling exception of the empty string.