How to get index of element from edit text - java

How can I code the following:
For example, I have one EditText in which I insert a word (or words) like "abcdawa". I have another EditText in which I enter the search element. Let's say I want to search "a".
When I click a Button the indexes of the word in the first EditText which equals the search element should be shown in a TextView. Expected output: 1, 5, 7

Consider that you have 2 edittext with id edText_1 & edText_2, and a button
Now in on click of your button call following,
setOnClickListener{
String query = edText_2.getText().toString();
char[] dataArray = edText_1.getText().toString().toCharArray();
for (int i = 0; i < dataArray.length; i++) {
if (query.equalsIgnoreCase(String.valueOf(dataArray[i]))) {
textView.append(" " + i);
}
}
}

Related

How to split string array?

It worked but it took the last song settext for all
for (int i = 1; i < items.length; i++) {
String SongsName = items[i];
String [] abcd = SongsName.split("_");
if(abcd[0] != null)
{
txtTitle.setText(abcd[0]);
txt2.setText(abcd[1]);
}
else
{
txtTitle.setText("Zero");
}
}
How does it show each line? Thanks in advance
After splitting the string, you end up with an array of song names. That array has an undefined number of items inside so you cannot just reference abcd[0] and abcd[1] and expect to see all songs. You need another loop that will iterate through all your song names in variable abcd.
Maybe I did not understand properly though, if variable abcd contains just one song name and you are just splitting to get the different pices of information for that song, then here is what is happening: you are looping through all the songs but you are overwriting txtTitle and txt2 everytime, so in the end, you end up with only the last one showing.

show textview in next line

TextView textView =(TextView)findViewById(R.id.text);
for(int i=1;i<6;i++)
{
textView.setText(i);
textView.setText("\n");
}
This is not printing
1
2
3
4
5
Please help me how to print the above output
whenever you call setText on a TextView you change the entire text.
you should create your String first and then set that as your TextView's text.
String text = "";
for(int i=1;i<6;i++) {
text += i+"\n";
}
textView.setText(text)'
another solution is using append instead of setText
for(int i=1;i<6;i++) {
textView.append(i+"\n");
}
I want from you in one comment to please show what it is printing right now ? and what you want to print ?
if you have 6 textviews then just do this
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
tv1.setText(""+i+"");
try this
String value="";
for(int i=1;i<6;i++)
{
value = value + i + "\n";
}
textView.setText(value);

Android Eclipse: for each line of EditText

I want to know how I can get the count of lines of an EditText and then set a line to a string.
Something like that:
String current_line;
for (int i = 0; i < EditText1.LinesLength; ++i) {
current_line = EditText1.Lines(i);
}
I can't make this work..
you can get the edittext lines like this..
Try using String.split(). Code example:
String multiLines = streetEt.getText().toString();
String[] streets;
String delimiter = "\n";
streets = multiLines.split(delimiter);
Now you have an array of streets.
Let's say, for example, your EditText reads "1st St.\nHighway Rd.\nUniversity Ave." (or is those 3 streets separated by line breaks, instead of you actually seeing \n). Following the code example I provided you,
multiLines becomes "1st St.\nHighway Rd.\nUniversity Ave."
`streets = multiLines.split(delimiter);` fills the array streets with the street names, i.e.
streets[0] = "1st St."
streets[1] = "Highway Rd."
streets[2] = "University Ave."

Get entire text from EditText

When I enter "sa" into EditText it takes only s. How can I pass my entire text?
Suppose I want to compare the text with the string "sa", how to get entire text from edittext?
e = (EditText) findViewById(R.id.selection);
//edittext watcher
e.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//getting edittext
String s1 =s.toString();
System.out.println("edittext :" + s1);
// compare edittext string woth arraylist
if (s1.length() >=1) {
//taking stringbuilder to add list items StringBuilder sb = new StringBuilder();
for(inti=0;i<orginalarrayelements.length;i++)
{
//compare edittext with arraylist with region matches if(orginalarrayelements[i].regionMatches(true, 0, s1, 0, s1.length())){
sb.append(orginalarrayelements[i] +",");
System.out.println("modified array: " + sb);
}
}
I think you are trying to get the full text from your EditText, right?
OnTextChanged gets called every time your text changes, so it will always only contain either one letter or null if you deleted a letter.
If you want to get the full text, call e.getText().toString(); in your OnTextChanged, OnClick on a Button or anywhere else.
By the way: rethink you names: "e" is not a name you should use. Take something like "editText", "myEditText", "usernameEditText",...

How to compare strings to get missing word?

I have created a table and some buttons to remove/add rows.
Problem is, when I add a new row, I must insert a value in the field name which isn't already on that table. Let me explain.
Here is the default table:
Now imagine i remove Station 3:
If i add a new Station, I would want to add a new Station name Station 3 (which is missing on the list) but I am adding a new Station 5 (obviously as my code is not correct).
My code for the Add Button action event is as such:
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
String s2 = "";
String s1 = "Station 1 Station 2 Station 3 Station 4 Station 5";
int tb1rows = jTable1.getRowCount();
if (tb1rows == 5) {
// ERROR - MAXIMUM STATION NUMBER
}
else {
for (int i=0; i<tb1rows;i++) {
s2.concat(jTable1.getValueAt(i,1).toString());
s2.concat(" ");
}
String[] s3=s2.split(" ");
for (int i=0;i<s3.length;i++) {
if (s1.contains(s3[i])) {
System.err.println("contains");
System.out.println(s3[i]);
}
}
model.insertRow(jTable1.getRowCount(),new Object[] {jTable1.getRowCount() + 1,"Station " + (jTable1.getRowCount()+1),10,false,0,Color.BLACK});
}
}
What's wrong with my logic? Is there a better way to handle the problem so that I can get the "Station x" that is missing in that column so that I can re-add it?
Thanks in advance for your answers.
As there is a space in "Station 1" split on space won't do. Instead using another separator like ";", better use a Set<String> values = new HashSet<String>().
So long as you have
"Station " + (jTable1.getRowCount()+1)
the new station is always going to be named "Station N + 1".
Assuming you fix the problem of splitting on a space described by another answer, your code should be something like
for (int i=0;i<s3.length;i++) {
if (s1.contains(s3[i])) {
System.err.println("contains");
System.out.println(s3[i]);
}
else {
model.insertRow(jTable1.getRowCount(),new Object[] {jTable1.getRowCount() + 1,"Station " + (i + 1) ,10,false,0,Color.BLACK});
}
}
instead of all that string manipulation, you could use set mainpulations:
HashSet<String> all = new HashSet<String>();
// then populate all with your 5 station strings (loop)
HashSet<String> have = new HashSet<String>();
// then populate all with the contents of your table (loop)
all.removeAll(have);
// all now contains the ones that weren't in the table.
if its ordered you can just find the first gap and insert there. so iterate over the rows and if nextrow.numberInIt > thisrow+1 you insert thisrow+1
Code should be something like this:
int nextNrToInsert;
for(int=0; i < tb1rows; i++){
thisrowNr = jTable1.getValueAt(i,1).toString();
NextrowNr = jTable1.getValueAt(i+1,1).toString();
if(nextrowNr > thisrowNr+1){
//found the gap
nextNrToInsert = thisrowNr+1;
break;
}
}
//change this to use nextNrToInsert
model.insertRow(jTable1.getRowCount(),new Object[] {jTable1.getRowCount() + 1,"Station " + (jTable1.getRowCount()+1),10,false,0,Color.BLACK});
This line is the issue
model.insertRow(jTable1.getRowCount(),new Object[] {jTable1.getRowCount() + 1,"Station " + (jTable1.getRowCount()+1),10,false,0,Color.BLACK});
You're always adding in the row with the rowCount() + 1. So even if you remove Station 3, there are 4 rows, and you're adding in row + 1.
This code has no effect:
for (int i=0; i<tb1rows;i++) {
s2.concat(jTable1.getValueAt(i,1).toString());
s2.concat(" ");
}
At loop exit, s2 will still be an empty string, so s3 will be an empty array.
But, the approach to concatenate strings and then split them is misguided, anyway. If all you need is find the lowest integer that, appended to "Station" will produce a unique string, the most natural approach is to make your own TableModel that uses a list of your own objects for each row's data. In that data you'll keep the integer itself, and not the entire string "Station n". Then it's going to be a trivial matter to find a hole in the list of integers.

Categories

Resources