removing words from a string in java - java

Could anyone tell me what is wrong in my code?
I am trying to pass a string to function removeWords() and this function removes some information from the String.
For example if I pass:
"I Have a Headach"
the function should return:
"Headach"
However, my function is not working:
public class WordChosen extends Activity {
private TextView wordsList;
private String symptom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_chosen);
//Getting String from VoiceRecognition Activity and displaying it
Intent intent = getIntent();
String wordChosen = intent.getExtras().getString("wordChosen");
//casting the string with TextView to display the result
wordsList = (TextView) findViewById(R.id.wordChosen);
Log.v("Word List:", "+++++"+wordChosen);
//Setting text to be displayed in the textView
removeWords(wordChosen);
Log.v("removewords:", "------- message is displayed");
}
public void removeWords(String wordList)
{
ArrayList<String> stopList = null;
stopList.add("i");
stopList.add("have");
stopList.add("a");
ArrayList<String> result = new ArrayList<String>(Arrays.asList(wordList.split(" ")));
for(int i=0; i<result.size();i++)
{
for(int j=0; j<stopList.size();j++)
{
if (result.get(i).equals(stopList.get(j))) {
break;
}
else {
if(j==stopList.size()-1)
{
wordsList.setText(result.get(i));
}
}
}
}
}
}

public static void main(String[] args) {
String word = "I Have a Headach";
String remove = "I Have a ";
System.out.println(removeWords(word, remove));
}
public static String removeWords(String word ,String remove) {
return word.replace(remove,"");
}
output : Headach

Related

Webview inside Asynctask

public class SecondActivity2 extends AppCompatActivity {
public static String main_html = "";
public static int second_action = 0;
WebView webView2;
public static taskResearch ndc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
webView2 = (WebView) findViewById(R.id.webView);
String userid = "12345";
try {
ndc = new TaskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");
ndc.execute("");
} catch (Exception e) {
e.printStackTrace();
}
Log.v("htmlg", "taskResearch finished");
}
class TaskResearch extends AsyncTask <String, String, String> {
String deviceId, s_id, keyword, city, s_title, s_text;
int count1, size1, online;
public TaskResearch(String deviceId, String s_id, String keyword, int count1, int size1, String city, int online, String s_title, String s_text){
this.deviceId = deviceId;
this.s_id = s_id;
this.keyword = keyword;
this.city = city;
this.s_title = s_title;
this.s_text = s_text;
this.count1 = count1;
this.size1 = size1;
this.online = online;
}
protected String doInBackground(String... strings) {
String sonuc = "";
runOnUiThread(new Runnable() {
public void run() {
}
});
return "a";
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
class html_reader {
#SuppressWarnings("unused")
#JavascriptInterface
public void processHTML(final String html) {
// Log.d("HTMLG", "html: " + html);
// Log.d("HTMLG", "html: " + html);
// html_reader jssil = new html_reader(deviceId, sId, swAfter, swcity);
// webView2.addJavascriptInterface(jssil, "HTMLOUT");
main_html = html;
}
#SuppressWarnings("unused")
#JavascriptInterface
public void ok() {
// Log.d("HTMLG", "html: " + html);
// Log.d("HTMLG", "html: " + html);
// html_reader jssil = new html_reader(deviceId, sId, swAfter, swcity);
// webView2.addJavascriptInterface(jssil, "HTMLOUT");
second_action = 1;
}
}
Hello Friends.
My codes are above. I want to run this command.
ndc = new taskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");
When this command finished. I want this command to run.
Log.v("htmlg", "taskResearch finished");
But 2 of them start together and second one finishes firstly. I want that first command should start and finish then second command should start. How can i do that ?
What is my mistake ?
By the way, If you are fecthing html code from a website, you can use different structures.
You can use onPostExecute to detect process end.
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
processFinished();
}
void processFinished(){
Log.v("htmlg", "task_research finished");
}
One way to do it is override the onPostExecute method, as mentioned in another post. Another way to do it is as follows:
After you call ndc.execute() you can wait for it to get a result like this:
String output = ndc.get(); // i believe this is the api
Once you are returned that output, you can do whatever you want to it and the second log will print after.

Android Studio saving data in database

I'm new here, and also new to Android Studio.
My first project is to create a diary to save a users workouts.
I'm having trouble saving for this data..
Here is my front page for the app. With the button "Styrkeboken" (Strengthbook) I'll be entering my sets, reps, weight(vikt) and excercise(övning). I want to save this data in the second button "Historik" (History), but I can't manage it to work as I want to..
Code from my class "Pass" (Workout):
package com.example.mama0086.styrkeboken;
public class Pass {
private int _set;
private int _reps;
private float _vikt;
private String _övning;
public Pass() {
}
public Pass(int set, int reps, float vikt, String övning) {
this._set = set;
this._reps = reps;
this._vikt = vikt; //Vikt = weight
this._övning = övning; //Övning = excercise
}
public void set_set(int _set) {
this._set = _set;
}
public void set_reps(int _reps) {
this._reps = _reps;
}
public void set_vikt(float _vikt) {
this._vikt = _vikt;
}
public void set_övning(String _övning) {
this._övning = _övning;
}
public int get_set() {
return _set;
}
public int get_reps() {
return _reps;
}
public float get_vikt() {
return _vikt;
}
public String get_övning() {
return _övning;
}
}
Code from my class "Historik" (History):
public class Historik extends AppCompatActivity {
EditText editSet;
TextView textSet;
EditText editReps;
TextView textReps;
EditText editVikt;
TextView textVikt;
EditText editÖvning;
TextView textÖvning;
MyDBHandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_historik);
editSet = (EditText) findViewById(R.id.editSet);
textSet = (TextView) findViewById(R.id.textSet);
editReps = (EditText) findViewById(R.id.editReps);
textReps = (TextView) findViewById(R.id.textReps);
editVikt = (EditText) findViewById(R.id.editVikt);
textVikt = (TextView) findViewById(R.id.textVikt);
editÖvning = (EditText) findViewById(R.id.editÖvning);
textÖvning = (TextView) findViewById(R.id.textÖvning);
dbHandler = new MyDBHandler(this, null, null, 1);
printDatabase();
}
//Add excercise
public void addBtn(View view){
Pass pass = new Pass(textSet.getText().toString()); //Pass is swedish for work-out
dbHandler.addPass(pass);
printDatabase();
}
public void removeBtn(){
String inputText = textSet.getText().toString();
dbHandler.deletePass(inputText);
printDatabase();
}
public void printDatabase(){
String dbString = dbHandler.historikToString();
textSet.setText(dbString);
editSet.setText("");
textReps.setText(dbString);
editReps.setText("");
textVikt.setText(dbString);
editVikt.setText("");
textÖvning.setText(dbString);
editÖvning.setText("");
}
}
In the "Historik" class I get this error message:
"Cannot resolve constructor 'Pass(java.lang.String)'
please let me know if you need the code for my database.
I appreciate the help!
You defined 2 different constructors for your Pass class :
public Pass() {
}
public Pass(int set, int reps, float vikt, String övning) {
this._set = set;
this._reps = reps;
this._vikt = vikt; //Vikt = weight
this._övning = övning; //Övning = excercise
}
However in your Historik activity, you're trying to instantiate a new Pass with a String only as a parameter Pass pass = new Pass(textSet.getText().toString());
You will need to change this line to match one of the 2 constructors you have, or define a 3rd one taking only a String as argument
EDIT : Depending on your Pass object, you can either :
declare a 3rd constructor taking only a String (I assume it's övning)
public Pass(String övning) {
this._övning = övning;
}
if the textSet.getText().toString() is not relevant, simply use the default constructor Pass pass = new Pass();

How to get child class objects from array list?

I am having trouble getting the earlier class objects of an array list in the 'onPostExecute' method of my asynch class. To simplify things for this post, I'm just trying to set the text of the 'name' to my textview in my onPostExecute method like so.
myTextView.setText(VideoDetail_List.get(0).getVideoName());
However the problem is that the String that is returning is the last instance of the videoname object in the string array, instead of the first instance of the video name, which should be the child class object of the VideoDetail_List at the 0 index.
AsyncTask Class
final JSONObject obj = new JSONObject(result);
final JSONArray videoData = obj.getJSONArray("data");
final int n = videoData.length();
for (int i = 0; i < n; ++i) {
final JSONObject video = videoData.getJSONObject(i);
videoDetail.setVideoName(video.getString("name"));
videoDetail.setVideoDuration(video.getString("duration"));
final JSONObject videoPictures = video.getJSONObject("pictures");
final JSONArray videoPictureSizes = videoPictures.getJSONArray("sizes");
final JSONObject videoPictureLink = videoPictureSizes.getJSONObject(1);
videoDetail.setPictureLink(videoPictureLink.getString("link"));
//pictureLink.add(videoPictureLink.getString("link"));
final JSONArray videoURLTypes = video.getJSONArray("download");
final JSONObject videoURLLink = videoURLTypes.getJSONObject(0);
videoDetail.setVideoLink(videoURLLink.getString("link"));
//videoLink.add(videoURLLink.getString("link"));
VideoDetail_List.add(i,videoDetail);
}
return VideoDetail_List;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(List<VideoDetail> VideoDetail_List) {
System.out.println("RESPONSE: " + VideoDetail_List);
final TextView myTextView = new TextView(mContext);
myTextView.setText(VideoDetail_List.get(0).getVideoName());
mLayout.addView(myTextView);
myTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sText = myTextView.getText().toString();
//Toast toast = Toast.makeText(this, sText, Toast.LENGTH_SHORT);
Toast toast = Toast.makeText(mContext, sText, Toast.LENGTH_LONG);
toast.show();
}
});
}
VideoDetail Class
public class VideoDetail {
private String videoName;
private String videoDuration;
private String pictureLink;
private String videoLink;
public String getVideoName() {
return this.videoName;
}
public void setVideoName(String videoName) {
this.videoName = videoName;
}
public String getVideoDuration() {
return videoDuration;
}
public void setVideoDuration(String videoDuration) {
this.videoDuration = videoDuration;
}
public String getPictureLink() {
return pictureLink;
}
public void setPictureLink(String pictureLink) {
this.pictureLink = pictureLink;
}
public void setVideoLink(String videoLink){
this.videoLink = videoLink;
}
public String getVideoLink() {
return videoLink;
}
}
Thank you in advance for your help!
However the problem is that the String that is returning is the last
instance of the videoname object in the string array
That's because you are overriding the content of the only instance of VideoDetails at every iteration of your for loop. You should instantiate it at every iteration
for (int i = 0; i < n; ++i) {
videoDetail = new VideoDetail();

How to display ArrayList Strings in TextView

I need to display current values in TextView (after removing String). I'm adding String when button is On and I need to remove it, when it's Off ( I don't know if I do it well), next I need to display Strings in TextView without deleted String. I need to display only Strings from On buttons.I don't know how to display ArrayList in TextView - this code display.setText(mActiveToggles.toString()); doesn't work. Here is my code:
public class Calc extends ActionBarActivity {
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
display = (TextView)findViewById(R.id.textView2);
display.setText("Add item");
}
static boolean isempty=true;
public void changeButton(View sender) {
ToggleButton btn = (ToggleButton) sender;
ArrayList<String> mActiveToggles = new ArrayList<String>();
String b = btn.getText().toString();
boolean on = ((ToggleButton) sender).isChecked();
if(on) {
if (isempty) {
if (b.equals("0")) return;
display.setText(btn.getText());
mActiveToggles.add(b);
isempty = false;
} else {
display.append(btn.getText());
mActiveToggles.add(b);
}
}
else
{
if (b.equals(btn.getText()))
{
mActiveToggles.remove(b);
display.setText(mActiveToggles.toString());
}
}
}
Use a loop. Iterate over each element in the ArrayList to add it to a String (or if you prefer you can use a StringBuilder instead). It would look something like the following:
String activeToggles = "";
for (String s : mActiveToggles) {
activeToggles += s + " ";
}
display.setText(activeToggles);

Android: compare string input (edittext) with arraylist

I want to compare my input string with an array list of another class, so I used the searchfield to compare them. As below it works fine.
Input Class
public class Suche extends Activity{
private Button zumergebnis;
final Intent zum_ergebnis = new Intent("android.intent.action.activity_ergebnis");
static String mystring;
static void setstring(String textView){
mystring = textView;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_suche);
zumergebnis = (Button) findViewById(R.id.zumergebnis);
zumergebnis.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
EditText searchinput = (EditText) findViewById(R.id.editText1);
String from = searchinput.getText().toString();
setstring(from);
startActivity(zum_ergebnis);
}
});}
}
Comparing Class
public class Ergebnis extends Suche
{
private TextView textView;
static String getstring(){
return mystring;
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ergebnis);
textView = (TextView) findViewById(R.id.textView2);
textView.setText(mystring);
class2 Object = new class2();
final ArrayList<String> word = Object.method2();
ListAdapter listenAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, word);
ListView mystringsView = (ListView) findViewById(R.id.listView1);
mystringsView.setAdapter(listenAdapter);
textView = (TextView) findViewById(R.id.textView2);
String searchField = (mystring);
for (String s : word)
{
if (searchField.toString().contains(s))
{
textView.setText("Your word"+mystring+"exists in the list");
}
else
{
textView.setText("Word"+mystring+"doesnt exist");
continue;
}
break;
}
}
}
list class
public ArrayList method2(){
ArrayList<String> word = new ArrayList<String>();
word.add("uno");
word.add("dos");
word.add("tres");
return word;
}
}
but when adding a second:
list class
public ArrayList method2(){
ArrayList<String> word = new ArrayList<String>();
word.add("uno; one");
word.add("dos; two");
word.add("tres; three");
return word;
}
}
'column' the app stops.
Anyone an idea how to even search for the second entry?
For everyone who is interested in the solution for that problem, the for- loop has to be changed to:
for (int i = 0; i < word.size(); i++) {
if (word.get(i).contains(searchField.trim())) {
textView.setText("Your input '" +mystring+ "' exists.");
}
else
adding some entries to the arraylist:
public ArrayList method2(){
ArrayList<String> word = new ArrayList<String>();
word.add("uno; one");
word.add("dos; two");
return word;
}
it works fine now.

Categories