String replace in Android - android

To try string replace in Android, I wrote a small snippet:
public class cs{
public static void main(String[] args){
String a,c;
int b;
b=1;
c="12345";
a="12345,54321";
a.replace(c,String.valueOf(b));
System.out.println(a);
}
}
Expected Output: 12345,54321 changes to 1,54321
Actual Output: 12345,54321
。Please help。

Is the . in c.String.valueOf(b) a typo for a comma, separating two parameters? Because it doesn't make much sense as stated. replace accepts two parameters, and furthermore, it doesn't change the string it's executed on, it merely returns a new one, so you need to pick up that return value and reassign it to the variable:
a = a.replace(c, String.valueOf(b));

If you expect output 1,54321 you need to write
a.replace("12345", c.String.valueOf(b));

Related

How can I sort array of numbers higher to lower (reverse/decreasing order)?

How can I sort array by numbers higher to lower.
Array
ArrayList<TestClass> array1 = new ArrayList<>();
Class
public class TestClass{
public boolean type;
public int counter;
public TestClass(boolean type, int counter) {
this.type = type;
this.counter = counter;
}
}
I tried do this
Collections.sort(array1);
But I got error
reason: no instance(s) of type variable(s) T exist so that TestClass conforms to Comparable
Assuming you don't have any accessory methods, you can use
array1.sort(Comparator.comparing(a -> a.counter));
The sorting order you asked for is reverse order, there are couple of ways to achieve this.
You can simple do a reverse of the previous sort like
array1.sort(Comparator.comparing(a -> a.counter));
array1.sort(Collections.reverseOrder());
If you can't user Comparator.comparing, you can do as follows
Collections.sort(array1, (item1, item2) -> Integer.compare(item2.counter, item1.counter));
The above statement can be explained as below.
Collections.sort() is provided from Java collections framework.
First argument specifies which collection needs to be sorted.
Second argument depicts on how each object in the collection should
be evaluated with other object in comparison. So for every pair of objects, in your case integers here, the condition returns true if the second element is greater than the first one. Which will pull the entire list to appear from higher to lower
arrayList.sort(new Comparator<TestClass>() {
#Override
public int compare(TestClass testClass, TestClass t1) {
return Integer.compare(t1.counter, testClass.counter);
}
});

SharedPreferences - Android (different data types)

I have a problem with with sharing data between two different activities. I have data like :
int number
String name
int number_2
int time
int total
I'm trying to make something like order list with this set of data . So it will take one set of data , then back to previous activity , move forward and again add data to it .
I have an idea of making it in array of object - but data inside was cleared after changing activity.
How can I make it ?
I don't know if and how to add Array of object to SharedPreferences , and get value of one element from there.
You should have a look at the documentation of the Intent(s) if you want to do that on the fly associating a key to the value(s) that you want to pass to your second activity.
Anyway, you can think any(sharedpref, database,...) way to pass your parameters but for those kind of things it's a convention and a good practice to follow that.
Don't used share preferences for this...Use the singleton pattern, extend Application, or just make a class with static variables and update them...
You can use .putExtra but since you are communicating with more than one activity the above suggestions are probably the best.
public class ShareData {
private String s;
private int s;
private static ShareData shareData = new ShareData();
private ShareData(){}
public static ShareData getInstance(){ return shareData}
//create getters and setters;
}
Why not to use Intents
Intent intent = new Intent(FirstActivity.this, (destination activity)SecondActivity.class);
intent.putExtra("some_key", value);
intent.putExtra("some_other_key", "a value");
startActivity(intent);
in the second activity
Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("some_key");
String value2 = bundle.getString("some_other_key");
EDIT if you want to read more about adding array to shared preferences check this
Is it possible to add an array or object to SharedPreferences on Android
also this
http://www.sherif.mobi/2012/05/string-arrays-and-object-arrays-in.html

is there anyway i can have listarray<string,int,int> in android?

is there anyway i can have multi dimensional array ex: listarray string,int,int in android??
it is quite possible with Map & Set in Java/C++.
Make your own container with fields good explaining its purpose:
class PersonData {
public int age;
public int id;
public String name }
And make list of it:
List<PersonData> dataList = new ArrayList<PersonData>();
Acces your fields by:
dataList.get(5).age = 11;
As Egor said in comment, a good practice will be set those field as protected and create setters and getters, if you don't need extreme performance in this specific case.

Divide two values obtained from EditText ad divide them by each other

Is it possible in Java code to divide two obtained values from two EditText boxes in an android app, and then divide them by each other to create a result?
Pretty much, the consumer of my application will be asked for two DIFFERENT, numerical values to be inputted into each box.
What I want to do with each value is produce a mathematical sum.
E.g.
If the first EditText box contains "22"
and the second EditText contains "11"
I want to be able to take those two values and divide them by each other to produce a value which I can further use.
In this case, that value produced would be 2.
22 / 11 = 2
I've already imported these two values into another Class (labeled DataHelper) through className.insert(stringName).
public class DataHelper extends Activity{
public static void insert(String firstFB, String firstRL) {
// TODO Auto-generated method stub
}
}
That's the Class for DataHelper with the two String values.
I have tried using IEEEremainder(x, y), but I don't know exactly how to use it since I am a novice at this language.
Can anyone give me some help with this? Any help at all will be appreciated.
Regards,
Mike.
if under standing rigtig you cut due
int one = int value1 / int vlaue2;
but then when you get the value from a edittext you get a string so you need to convert it to a int first int a = new Integer("value from edittext").intValue();
I believe you want something like this:
public class DataHelper extends Activity{
public static void insert(String firstFB, String firstRL) {
int fb = Integer.parseInt(firstFB);
int rl = Integer.parseInt(firstRL);
int division = fb / rl; // Or use double if needed
}
}

How to use a single intent with multiple buttons

I have an android context menu with various selections and depending on the user selection I want to start an intent. The intent starts the same activity for all the buttons but will contain different String variables depending on the selection. I am currently using a switch, case methodology for my click listener but keep running into 'duplicate local variable' problems as I try to eliminate code repetition! If anyone could provide a-bit of pseudo-code that would be even better!
It's hard to tell without seeing some code, but "duplicate local variables" together with "switch case" makes me think you're declaring a variable in one of the cases with the same name as a variable from another case.
Code within different cases of the same switch is all in the same scope, unless you surround the code within a case with brackets, like this:
switch(VALUE) {
case A: {
String string = "";
}
case B: {
//Same variable name, possible since it's in a different scope now.
String string = "";
}
}
So either use brackets, or simply make sure you're using different variable names across the cases.
you can use intent.putExtra(String name, String value) and push it to the other activity.
Pseudo code:
Button1.value = "X" ;
Button2.value = "Y" ;
onClickListner(View v) {
Intent intent = new Intent() ;
intent.putExtra("ButtonValue",
v.value() ) ;
// extra code goes here...
}
Hope this is what you were looking for..
VInay
I like to use set/getTag(Object), as you can put any type you like into it (as long as you're careful about getting it out again):
button1.setTag(MyClass.STATIC_INT_1);
button2.setTag(MyClass.STATIC_INT_2);
button1.setOnClickListener(Click);
button2.setOnClickListener(Click);
private OnClickListener Click(View v) {
Intent intent = new Intent() ;
intent.putExtra("Value", Integer.parseInt(v.getTag().toString()) ) ;
···
}

Categories

Resources