Im having trouble taking a number value from a string on a webpage im testing.
Im currently using:
String total_points = potential_points.getText();
System.out.println("Potential points are: " + total_points);
int pot_points = Integer.parseInt(total_points);
The problem is the selector is actually returning two strings aswell:
"Potential Points"
":"
"1476"
How can i extract the 1476 and use it as an int. Ive attached the html.
Potential Points: 1473
Entry:Free To Play
You need to get the result by using substring method as below
String total_points = potential_points.getText();
int startIndex=total_points.indexOf(":")+2;
int endIndex=total_points.length();
String result=total_points.substring(startIndex,endIndex);
int no=Integer.parseInt(result);
Simplified the above code as below
String result=total_points.substring(total_points.indexOf(":")+2,total_points.length());
int no=Integer.parseInt(result);
Related
Is this the right way to set text in TextView programmatically?
points_txt.setText(R.string.you_have + current_points + R.string.points);`
It shows me a ResourcesNotFoundException error for the string while I can see the string in the strings.xml file.
points_txt.setText(getResources().getString(R.string.you_have) + current_points + getResources().getString(R.string.points));
You get a ResourcesNotFoundException because you're adding int values (resource identifiers are mapped to int values at compile time) instead of concatenating Strings.
The sum of the various resource identifiers might even be another valid resource identifier, but that would only happen accidentally. Nevertheless, if you pass an int value into setText(), the runtime tries to find a string resource by that number. In your case, it failed and so your app crashed.
So you have to get the Strings first and concatenate them afterwards:
points_txt.setText(getString(R.string.you_have) + current_points + getString(R.string.points));
points_txt.setText(R.string.you_have + current_points + R.string.points);
This is showing "ResourcesNotFoundException" because "R.string.you_have" is an integer value an "current_point" variable also is an int type
setText() requires the String type...
to get string value of "R.string.you_have" you can use
getResources().getString(R.string.you_have);
points_txt.setText(getResources().getString(R.string.you_have) + current_points + getResources().getString(R.string.points));
To get a string from strings.xml do this:
String you_have = getResources().getString(R.string.you_have);
You are almost there but I feel might be behind a few steps but not sure about this since you haven't shared all of your code.
You need to wire in the TextView first between your Java class and XML
TextView tv1 = (TextView)findViewById(R.i.d.textView1)
Next is setting the String for the textview
tv1.setText(getResources().getString(R.string.you_have) + "current_points" + getResources().getString(R.string.points));
You are basically missing the " " marks which are compulsory when you are assigning hardcoded string.
You must firstly parse that resource to string:
String string = getString(R.string.yourString);
More about that here:
how to read value from string.xml in android?
So answer for your question will be literally as following:
String you_have = getString(R.string.you_have);
String points = getString(R.string.points);
points_txt.setText(you_have + current_points + points);
I have two problem. Please try to solve...
No 1.
Suppose I have two string:-
String one = "Android is awesome";
String two = "Android is";
Now my question is, can I get awesome by comparing those two string like we do with int.
int abc = 50;
int def = 35;
int ghi = abc - def;
output :-
ghi = 15;
So, this is what we do basically with int, long, double... Is it possible also with String?
No 2.
Now, suppose again I have a string-array
ArrayList<String> list = new Arraylist()<>;
list.add("beautiful");
list.add("awesome");
list.add("cool");
Now, if string-comparing is possible, then suppose I have got a new String three from comparing one and two.
So, here,
String three = "awesome";
Now, again, I am using if-statement
if(list.contains("awesome")){
**Problem here starts. See the commented texts**
//Here if **awesome** is found in **list**, so can I get the position of **awesome** in the **list**?
}
Forgive me for a big and rough question. Please help me. I am really so sad with this problem.
If you want the remaining string by comparing two strings, try the following
String one = "Android is awesome";
String two = "Android is";
if(one.contains(two)){
Log.i("remaining string ",one.split(two)[0].replace(" ",""));
Log.i("remaining string ",one.split(two)[1].replace(" ",""));
}else{
Log.i("remaining string ","");
}
Then use the remaining string to find the index from list.Try the following ,
list.indexOf(remainingstring);
I'm trying to select the positions of some value and print the parts of that value by position in java when I'm working on android app.
for example if I have some number four-digit, 4375 if I select the 3rd one system will print 7, 4th one system will print 5..
You can easily select the portion of a string with String.substring() methods.
See https://docs.oracle.com/javase/7/docs/api/java/lang/String.html for more help.
Hence, you could simply convert the digit to String and then use the substring() method get the part you want.
I will just provide my solution as a suggestion:
private String getSubDigit(int value, int postion){
// you probably should check if the value is correct and ready to be processed.
if(Check(value)) {
String temp = "" + value;
return temp.substring(position,position+1);
}else{
return "";
}
}
You can acheive it like this
int values = 4375;
String mValue = String.valuOf(values);
Char mChar = mValue.chatAt(int index);
int selectedValue = Character.getNumericValue(mChar);
Im having a trouble in java. Im creating a HRRN scheduling. I want to print the integer that I input into a textfield area. Please help me to solve this problem. Thankyou!
private void AWTActionPerformed(java.awt.event.ActionEvent evt) {
int firstprocess=1;
if (bt1.getText().equals("")){
double tempbt1 = Double.parseDouble(bt1.getText());
awttotalprocess = (firstprocess + (tempbt1));
AWTCLICK = 0;
jtf_awt.setText(String.valueOf(awttotalprocess+"ms"));
}
I want to print the awttotalprocess into jtf_awt.
Bracketing issue:
jtf_awt.setText(String.valueOf(awttotalprocess)+"ms");
Many classes come with what's called a .toString() method that prints a pre-specified output when joined with a string. You can concatenate or join a string and a variable -in this case an integer- like this:
int i = 50;
String join() {
return "I'm a string, next is a number: " + 50;
}
Keep in mind that int and Integer are different in that the first is a primitive data type, and the second is the object. This isn't an issue for you in this code but in the future if you try to concatenate a string with an object it may end up printing out the memory address as written in the .toString() default method and would require you to #override the method to specify your own string output. The primitive data types are "easier" to combine and don't require such .toString() overriding or .valueOf() shenanigans.
I am loading strings from a text file, eg;
Sunset Blvd 1950.ogg,Sunset Blvd,Paramount Pictures,1950,110,Billy Wilder,4,William Holden,Gloria Swanson,Erich von Stroheim,Nancy Olson
Now I have a class setup that extends from 2 parent classes (Media > Video > Avi/Ogg/etc). And that class holds the following variables;
public Avi(String title, String fileName, int releaseYear, String studio, String director, String castNames, double runtime, int cast) {
super(title, fileName, releaseYear, studio, director, castNames, runtime, cast);
}
Now I load the text file in using a buffer reader and a loop, but heres the problem, the cast names (Which come last in the text file, are also separated with commas but since I am using a splitter already I am not sure how to get every cast member into a simply string such as "Larry Davis,Eddy Murphy,Etc Etc" that can be returned later on. Also using a different splitter for cast names is not an option
if your cast starts at William Holden, you can do
line.split(",", 8);
I assume that by splitter you mean the String method "split".
If so, does your text file always have the same structure ?
Meaning is there always the same number of elements before the cast names ?
Because the String method "split" can take a second parameter specifying the number of elements to retrieve (cf. link to String API)
None of these solutions worked but heres what I came up with that worked:
String castNames = "";
int splitLength = split.length - 7;
for (int i = 0; i < splitLength; i++) {
castNames += split[7 + i] + ",";
}
Avi avi = new Avi(split[1]/*title*/
, split[0]/*filename*/
, Integer.parseInt(split[3])/*releaseyear*/
, split[2]/*studio*/
, split[5]/*director*/
, castNames/*castnames*/
, Double.parseDouble(split[4])/*runtime*/
, Integer.parseInt(split[6])/*cast*/);
return avi;
Having a symbol as the string separator as well as being valid data is not a good idea and results in code that is prone to errors. Of course you can work around that - some people before me have suggested ways to do it - but I strongly recommend that you change your input and remove the ambiguity.