Hello i have small problem with android studio.
I have made simple code that checks if user has inserted string with http:// if not then add http:// to string.
Here is part of my code:
if (!host.contains("http://")) {
String playlistUrl = "http://" + host + "/test.m3u";
}else{
String playlistUrl = host + "/test.m3u";
}
intent.setData(Uri.parse(playlistUrl));
Android Studio drops me an error on last line (cannot resolve symbol playlistUrl)
intent.setData(Uri.parse(playlistUrl));
But why? If statement should return string playlistUrl....
You're creating the variable inside of the if ... else statement - it means it won't be visible outside.
You can implement it like this:
String playlistUrl;
if (...) {
playlistUrl = ...;
}
else {
playlistUrl = ...;
}
Declare playlistUrl outside the if-else construct:
String playlistUrl;
if (!host.contains("http://")) {
playlistUrl = "http://" + host + "/test.m3u";
} else{
playlistUrl = host + "/test.m3u";
}
intent.setData(Uri.parse(playlistUrl));
you need to do it this way your not using the same variable by redeclaring it
String playlistUrl = "";
if (!host.contains("http://")) {
"http://" + host + "/test.m3u";
}else{
playlistUrl = host + "/test.m3u";
}
intent.setData(Uri.parse(playlistUrl));
You have to declare and instantiate your var playListUrl outside the if statement. The reason for that is that the compiler only translates to bytecode without evaluating expressions.
Related
I am trying to make somekind of version checker for my application.
The idea is to compare the numbers from 2 strings and if 1 set of numbers is bigger then the other a new version has been found.
oldString = 360 some - File v1.52.876 [build 2546]
newString = 360 some - File v1.53.421 [build 2687]
What I need is to compare the set numbers after the 'v' in both strings as there can also be numbers (360) in front of the file, as shown in above example.
Below method checks an arraylist (loadTrackedItems) which contains the files to be checked agains the newly received item (checkItemTrack).
But I am having trouble getting the correct numbers.
Is there a better way to do this?, could somebody be so kind and help a bit.
Thank you in advance.
public static boolean newTrackedVersion(String checkItemTrack) {
final List<String> tracking = new ArrayList<String>(loadTrackedItems);
boolean supported = false;
for (final String u : tracking) {
if (checkItemTrack.contains(u)) {
supported = true;
// get the index of the last 'v' character
int trackindex = checkItemTrack.lastIndexOf("v");
String newItem = checkItemTrack.replaceAll("[a-zA-Z]", "").replace("\\s+", "")
.replaceAll("[-\\[\\]^/,'*:.!><~##$%+=?|\"\\\\()]+", "");
String inList = u.replaceAll("[a-zA-Z]", "").replace("\\s+", "")
.replaceAll("[-\\[\\]^/,'*:.!><~##$%+=?|\"\\\\()]+", "");
long newTrack = Long.parseLong(newItem.trim());
long inTrackList = Long.parseLong(inList.trim());
if (newTrack > inTrackList) {
//Toast.makeText(context,"New version found: " + checkItemTrack, Toast.LENGTH_LONG).show();
Log.w("NEW VERSION ", checkItemTrack);
Log.w("OLD VERSION ", u);
}
break;
}
}
return supported;
}
if you receive only two strings to compare this solution will work try it.
String oldString = "360 some - File v1.52.876 [build 2546]";
String newString = "360 some - File v1.53.421 [build 2687]";
String oldTemp = oldString.substring(oldString.indexOf('v'), oldString.indexOf('[')).trim();
String newTemp = newString.substring(newString.indexOf('v'), newString.indexOf('[')).trim();
int res = newTemp.compareTo(oldTemp);
if(res == 1){
//newString is higher
}else if(res == 0){
//both are same
}else if(res == -1){
//oldString is higher
}
I found source of an application of adt and i imported in android studio, after the Gradle build there shown an single error in java code....i dont know whether it is syntactical or symantic...can u please rectify
String str2;
if (arrayOfString[i].contains("%")) {
str2 = arrayOfString[i].split("%")[1];
}
String str1;
for (Settingss.this.setnum = (Settingss.this.setnum + str2 + ","); ; Settingss.this.setnum = (Settingss.this.setnum + str1 + ","))
{
i++;
break;
str1 = arrayOfString[i];
}
The error occured at 6th line at "str2"..android studio quoted that "str2" may not have been initialized.
This is not the full code, it is just at the error part.
change String str2 to String str2 = null;
the reason Android Studio is throwing the error is because the variable could be declared but never initialized i.e. str2 = XXX;
I have a string (length 3-8) assigned to a variable (text). I want to check whether the 2nd and 3rd characters are NOT numeric (a letter or symbol or space..or anything other than numbers).
Elementary way to do this could be:
if(((text.charAt(1)-'0')>=0)&&(text.charAt(1)-'0')<10))||((text.charAt(2)-'0')>=0)&&(text.charAt(2)-'0')<10)))
{
//do nothing, since this means 2nd and/or 3rd characters in the string are numeric
}
else
{
// Your condition is met
}
You could also use REGEX's , if your checking is still more complicated.
Here is Another way to achieve this:
boolean isNumeric = true;
String test = "testing";
char second = test.charAt(1);
char third = test.charAt(2);
try {
Integer.parseInt(String.valueOf(second));
Integer.parseInt(String.valueOf(third));
} catch(NumberFormatException e) {
isNumeric = false;
}
System.out.println("Contains Number in 2nd and 3rd or both position: " + isNumeric);
You might make use of the String.IndexOf(String) method, like:
String digits = "0123456789";
String s2 = text.substring(2,3);
String s3 = text.substring(3,4);
boolean valid = (digits.indexOf(s2) > -1) && (digits.indexOf(s3) > -1);
resultString is the parameter I get from sqlite
resultString = result.getString(result.getColumnIndex(1));
I want to compare ignore case with user input , following is the code I have use. But it doesn't work.
For example, in database, I store a username "George"
When I login, I have to type exactly "George"."george" doesn't work.
Here is my code to compare.
if (userName.equalsIgnoreCase(resultString)) {
return true;
}
What might be the problem?
Please try following code,
if (userName.trim().equalsIgnoreCase(resultString.trim()))
{
return true;
}
Your code should work if the only difference is the case. I suspect you have leading or trailing spaces. Try the following:
if (userName.trim().equalsIgnoreCase(resultString.trim())) {
return true;
}
I was panic and didn't try the to print out the result.The problem was my query statement.
String whereClause = DatabaseHelper.USER_COL + " name =?";
The resultString is always empty unless input is the same as data.
To fix it I follow instruction in this post
sqlite LIKE problem in android
String whereClause = DatabaseHelper.USER_COL + " LIKE ?";
userName = userName+"%"
int Num;
String answer = et_q7.getText().toString();
String right_answer = "Air Pollution";
String right_answer2 = "Air";
if (answer.trim().equalsIgnoreCase(right_answer.trim()) || answer.trim().equalsIgnoreCase(right_answer2.trim())) {
Num = 1;
} else {
Num = 0;
}
I'm trying to remove all the spaces from a string derived from user input, but for some reason it isn't working for me. Here is my code.
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonGo:
String input = EditTextinput.getText().toString();
input = String.replace(" ", "");
url = ur + input + l;
Intent myIntent = new Intent(start.this, Main.class);
myIntent.putExtra("URL", url);
start.this.startActivity(myIntent);
break;
}
}
String input = EditTextinput.getText().toString();
input = input.replace(" ", "");
Sometimes you would want to remove only the spaces at the beginning or end of the String (not the ones in the middle). If that's the case you can use trim:
input = input.trim();
When I am reading numbers from contact book, then it doesn't worked
I used
number=number.replaceAll("\\s+", "");
It worked and for url you may use
url=url.replaceAll(" ", "%20");
I also had this problem. To sort out the problem of spaces in the middle of the string this line of code always works:
String field = field.replaceAll("\\s+", "");
Try this:
String urle = HOST + url + value;
Then return the values from:
urle.replace(" ", "%20").trim();
String res =" Application "
res=res.trim();
o/p: Application
Note: White space ,blank space are trim or removed
Using kotlin you can write like:
val resultStr = yourString.replace(" ", "")
Example:
val yourString = "Android Kotlin"
val resultStr = yourString.replace(" ", "")
Result: AndroidKotlin