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;
Related
i have problem how to , remove the comma in the of my output. I use replaceall but it doesnt remove the comma , this my code
public void onClick(View v) {
String space = "";
String foo = " ";
String foo1 = ",";
String sentences = null;
//Splitting the sentence into words
sentences = multiple.getText().toString().toLowerCase();
String[] splitwords = sentences.trim().split("\\s+");
for (String biyak : splitwords) {
foo = (foo + "'" + biyak + "'" + foo1);
foo.replaceAll(",$", " ");//foo.replaceAll();
wordtv.setText(foo);
My codes Output : 'Hello','world',
My desire output: 'Hello','world'
String instances are immutable. As a result, methods like replaceAll() do not modify the original string but instead return a modified copy of the string. So replace foo.replaceAll(...) with foo = foo.replaceAll(...).
you can use substring method of String class. or You can use deleteCharAt() method of StringBuilder or StringBuffer classStringBuffer sb=new StringBuffer(your_string);sb.deleteCharAt(sb.length()-1);
U can also use a if statement and traverse the whole string .
If a comma(,) is found replace it with a space(" ").
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.
I got stuck in this regular expression as I am having a string as:
String str = "/abcde/samplename.xyz"
I want to replace this samplename.xyz from a new string, so how can I apply the regular expression in this ?
Try this out.....
String str = "/abcde/samplename.xyz";
String req=str.substring(str.lastIndexOf("/") + 1);
now you get the value of: req=samplename.xyz and you can replace with which ever string value you want
String rep=str.replace(req, "");
String startString=str.substring(0, str.lastIndexOf('/') + 1)
String str = "/abcde/samplename.xyz";
String str1 = str.substring(0, str.lastIndexOf('/') + 1);
Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();
Log.i("strArr[1]=", "" + str1);
check this.i hope its useful to you.
Try this::
String str = "/abcde/samplename.xyz";
String result=str.substring(0, str.lastIndexOf('/') + 1);
Log.d("Hello","Result="+result);
String[] result = str.split("/");
String LastItem = result[str.length -1];
LastItem.replace(newString);
Is there any way to access automatically any Log in Logcat by a double click ?
Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance
at com.myapp.mypackage$Class.function(File.java:117)
And by Double-clicking on this line, I am automatically redirected to the related line of my code.
But, when I try to generate the same line in another Log, example :
Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");
The Double-Click doesn't work anymore ...
Any ideas ?
If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:
Enjoy!
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message
Log.e("TAG", "Looky here see", new Exception());
I'm storing this data in a .dat file:
data = date + ": " + y + "L/100KM "+ " " + value1 + "dt "+ value2 + "KM\n";
Every line has different values of date,y,value1 and value2.
I want to retrieve variable value1 of every line. How to browse the file and extract this variable of all lines. I'm stucking in this problem in my project. Thanks for helping.
EDIT: Example:
I have this 3 datas stored in the file:
11/09: 5.8L/100KM 20dt 250KM
12/09: 6.4L/100KM 60dt 600KM
13/09: 7.5L/100KM 50dt 543KM
In that case, i want to retrieve 20dt, 60dt and 50dt.
Here's one suggestion using regular expressions:
String line = "12/09: 6.4L/100KM 60dt 600KM";
Pattern p = Pattern.compile("(\\d+)dt");
Matcher m = p.matcher(line);
if (m.find())
System.out.println(m.group(1)); // prints 60
If you have several lines to iterate over, you'd use for instance a new BufferedReader(new FileReader("youfile.dat")) and do something like
String line;
while ((line = br.nextLine()) != null) {
Matcher m = p.matcher(line);
if (m.find())
process(m.group(1));
}
You could also just use line.split(" ") and select the 3:rd element:
String line = "12/09: 6.4L/100KM 60dt 600KM";
String dtVal = line.split(" ")[2];
// Optional: Remove the "dt" part.
dtVal = dtVal.substring(0, dtVal.length() - 2);
System.out.println(dtVal);