I can normally identify an integer by typing in int something = 0; to initialize or something.
This time I would like to get an integer like I do a String from a JtextArea.
String strcname = cname.getText();
**int strage = age.getInt();**
String stremail = email.getText();
String strphone = phone.getText();
Obviously I am getting an error here but not sure how to have this excepted as an integer any ideas?
You could use:
int strage = Integer.parseInt(age.getText());
Related
This conversion is not working, I am getting an exception,
not sure why
String str = "Draw: 1";
int draw = 0;
String temp;
temp = str.substring(5,7);
draw= Integer.parseInt(temp);
System.out.println(draw);
Because you're trying to parse " 1", notice the space. Adding a trim() should solve it.
temp = str.substring(5,7).trim();
Actually, I'm downloading HTML code with iON library and in this HTML page there is just a string like |5.00| or it can be |10.00|. So how can I get the value that is in middle of |?
The problem is that I don't have the exact string |10.00| but all the HTML code so I have to use something like if(htmlstring.contains("|").
But then, I can't get how to get the value from that |.
You can use indexOf()
String htmlstring = "abcd|10.0|1234";
int firstIndex = htmlstring.indexOf('|');
int lastIndex = htmlstring.indexOf('|',firstIndex+1);
String value = htmlstring.substring(firstIndex+1, lastIndex);
Try :
String val = "|10.00|";
String output[] = val.split("\\|",val.length());
val = output[1].substring(0,val.length()-2);
I have a problem with String.format In android I want replace { 0 } with my id.
My this code not working:
String str = "abc&id={0}";
String result = String.format(str, "myId");
I think you should use replace method instead of format.
String str = "abc&id={0}";
str.replace("{0}","myId");
you have 2 ways to do that and you are mixing them :)
1.String format:
String str = "abc&id=%s";//note the format string appender %s
String result = String.format(str, "myId");
or
2.Message Format:
String str = "abc&id={0}"; // note the index here, in this case 0
String result = MessageFormat.format(str, "myId");
You have to set your integer value as a seperate variable.
String str = "abc&id";
int myId = 001;
String result = str+myId;
try this,
String result = String.format("abc&id=%s", "myId");
edit if you want more than one id,
String.format("abc&id=%s.id2=%s", "myId1", "myId2");
The syntax you're looking for is:
String str = "abc&id=%1$d";
String result = String.format(str, id);
$d because it's a decimal.
Other use case:
String.format("More %2$s for %1$s", "Steven", "coffee");
// ==> "More coffee for Steven"
which allows you to repeat an argument any number of times, at any position.
I have a string that might look like this:
searchText = search:kind:(reports) unit.id:(("CATS (WILLIAMS)"~1) OR ("DOGS (JAMES)"~1))
I need to extact any values that may exist in the quotation marks so in this case it would be:
CATS (WILLIAMS) and DOGS (JAMES)
Not sure I understand how using indexOf and subString will get me a text string since they depend on integer values... Can someone show me some examples of how this might be done? Thanks
Ok figured out the basics, but I need it to extract every value in " " not just the first instance. The below extacts the value then converts the name into an id then replaces the name with an id, but ONLY does the first instance.
String unitIdStart = "\"";
String unitIdEnd = "\"~";
int unitIdStartIndex = searchText.indexOf(unitIdStart);
if( unitIdStartIndex != -1 ) {
int unitIdEndIndex = searchText.indexOf(unitIdEnd, unitIdStartIndex);
if( unitIdEndIndex != -1 );
{
String unitName = searchText.substring(unitIdStartIndex+1, unitIdEndIndex);
Unit backToId = UnitRepository.getIdFromName(unitName);
String unitId = backToId.getId().toString();
String searchTextWithUnitId = searchText.replace(unitName, unitId);
i am trying to remove all text except last 10 characters but getting error in android
09-15 16:22:58.146: E/AndroidRuntime(13630): java.lang.StringIndexOutOfBoundsException: length=228; index=263
but it is working on java when i tried it in separate class.
here is what i tried? can anybody tell me if there is any issue regarding substring in android?
String allchar =
"PJGOGROG fnkmslkjsfsdmnsgdnklnsgklsgknlgrf jkghijgdlkjkgjdfkjgf kjlgdfkjlfdgfjklklj
kljfdkjlfkjldfdkjslkljdfskjlfsjkldfsjklkljs
fdsjklsdfkljsfdkljkhgfhhgdfsgkhdfskghfdskghfdsghsfdghafevbhfsvgydcgubcdmgycdgfehkhfeghjgh68
Alias12345";
String number = "";
String reqtext = allchar.replaceAll("\\s+","");
number = reqtext.substring(reqtext.length()-10 );
System.out.println(number);
Well if substring() doesn't work, you can try doing it manually:
String allchar =
"PJGOGROG fnkmslkjsfsdmnsgdnklnsgklsgknlgrf jkghijgdlkjkgjdfkjgf kjlgdfkjlfdgfjklklj
kljfdkjlfkjldfdkjslkljdfskjlfsjkldfsjklkljs
fdsjklsdfkljsfdkljkhgfhhgdfsgkhdfskghfdskghfdsghsfdghafevbhfsvgydcgubcdmgycdgfehkhfeghjgh68
Alias12345";
StringBuilder number = new StringBuilder();
String reqtext = allchar.replaceAll("\\s+","");
for(int i = reqtext.length()-10; i < reqtext.length(); i++)
{
number.append(reqtext.charAt(i));
}
System.out.println(number.toString());
String allchar = "PJGOGROG fnkmslkjsfsdmnsgdnklnsgklsgknlgrf jkghijgdlkjkgjdfkjgf kjlgdfkjlfdgfjklkljkljfdkjlfkjldfdkjslkljdfskjlfsjkldfsjklkljsfdsjklsdfkljsfdkljkhgfhhgdfsgkhdfskghfdskghfdsghsfdghafevbhfsvgydcgubcdmgycdgfehkhfeghjgh68Alias12345";
String number = "";
String reqtext = allchar.replaceAll("\\s+","");
number = reqtext.substring(reqtext.length()-10, reqtext.length());
System.out.println(number);
This code works fine for me in android.
Avoid declaring so many variable, try using :
number = allchar.substring(allchar.length()-10, allchar.length());