Saving user input to text file, android studio
<p>Well, I'm trying to implement some basic java knowledge I gained by creating a simple app with Android Studio.</p>

<p>Basically my app would receive some information from user and save it to a text file, the app serves as kind of a template to receive information.</p>

<p>Elements are;</p>

<p>2 text fields, actually one field with option of entering Letters ( A,B,C etc...) or numbers, the Second TextField would accept only Numbers.</p>

<p>6 Radio Buttons, user should and only could choose one, i already placed these Buttons in a Radio Group.</p>

<p>4 Check boxes, user should tick the box or boxes suitable for the entry.</p>

<p>2 Buttons, one to save, other to edit.</p>

<p>as it is shown in the screen capture.</p>

<p>My code goes as this so far, and MY ISSUE is , that I can't figure out how to implement the write command into the java file, as i learned doing so in eclipse. I want to save the entry into a text file, but not sure what is the write syntax for this in android studio, and how to do that with the check boxes and radio buttons ?</p>

<p>I've tried to go over several question and tutorials but can't put it together, any guidance and tips would be really appreciated .</p>

<p>CODE:</p>

<pre><code>import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;

public class MainActivity extends AppCompatActivity {



EditText CodNum;
EditText PltNum;
RadioButton radButA;
RadioButton radButD;
RadioButton radButS;
RadioButton radButR;
RadioButton radButO;
RadioButton radButI;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    CodNum = (EditText) findViewById(R. id.CodNum);
    PltNum = (EditText) findViewById(R. id.PltNum);
    radButA = (RadioButton) findViewById(R. id.radButA);
    radButD = (RadioButton) findViewById(R. id.radButD);
    radButS = (RadioButton) findViewById(R. id.radButS);
    radButR = (RadioButton) findViewById(R. id.radButR);
    radButO = (RadioButton) findViewById(R. id.radButO);
    radButI = (RadioButton) findViewById(R. id.radButI);
    checkBoxMw = (CheckBox) findViewById(R. id.checkBoxMw);
    checkBoxG7 = (CheckBox) findViewById(R. id.checkBoxG7);
    checkBoxGw = (CheckBox) findViewById(R. id.checkBoxGw);
    checkBoxF = (CheckBox) findViewById(R. id.checkBoxF);
    final FileOutputStream outputStream;
    String fileName = "feederTxtFile";


    Button buttonS = (Button) findViewById(R. id.buttonS);
    buttonS.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                try{
                    outputStream = openFileOutput(fileName,CONTEXT.MOOD_PRIVATE);
                    outputStream.write(String.getBytes());
                    outputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }

                }
            });

    Button buttonE = (Button) findViewById(R. id.buttonE);
    buttonE.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Editing to text file.
        }
    });
</code></pre>

<p>All right so i corrected the code, and added the right syntax to save the Code and Number but i am still having a problem with it to save the right RadioButton and CheckBox, Actually iwas able to make it save the Radio button but with a weird message and part of it including the RadioButton id, and i wan it to just save the Radiobutton name ( A, D, R, etc .. ) next to the number and the same with the check box which i do not have any clue how to even deal with the check boxes !?</p>

<p>Could someone please guid me on what is wrong with my RadioButton code and HOW TO SAVE THE CHECK BOXES !?</p>

<p>This is the new code:</p>

<p>public class MainActivity extends AppCompatActivity {</p>

<pre><code>private EditText entryCode;
private EditText entryNum;

private RadioGroup radioBtnGr;
private RadioButton radioButtonA;
private RadioButton radioButtonD;
private RadioButton radioButtonS;
private RadioButton radioButtonR;
private RadioButton radioButtonO;
private RadioButton radioButtonI;

private CheckBox checkBoxGw;
private CheckBox checkBoxG7;
private CheckBox checkBoxKL;
private CheckBox checkBoxFW;


View.OnClickListener checkBoxListener;

private Button buttonE;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    entryAcceptance();
}

public void entryAcceptance(){

    entryCode = (EditText) findViewById(R. id.entryCode);
    entryNum = (EditText) findViewById(R. id.entryNum);

    radioBtnGr = (RadioGroup) findViewById(R. id.RadioBtnGr);
    radioButtonA = (RadioButton)findViewById(R.id.radioButtonA);
    radioButtonD = (RadioButton)findViewById(R.id.radioButtonD);
    radioButtonS = (RadioButton)findViewById(R.id.radioButtonS);
    radioButtonR = (RadioButton)findViewById(R.id.radioButtonR);
    radioButtonO = (RadioButton)findViewById(R.id.radioButtonO);
    radioButtonI = (RadioButton)findViewById(R.id.radioButtonI);

    checkBoxGw = (CheckBox) findViewById(R. id.checkBoxGw);
    checkBoxG7 =  (CheckBox) findViewById(R. id.checkBoxG7);
    checkBoxKL =  (CheckBox) findViewById(R. id.checkBoxKL);
    checkBoxFW =  (CheckBox) findViewById(R. id.checkBoxFW);


}

public void radioButtonClicked(View v)
{
    int selectedCode = radioBtnGr.getCheckedRadioButtonId();
    RadioButton selected = (RadioButton)findViewById(selectedCode);
    toasting(selected.getText().toString());
}

public void saveLogOnClick(View view){
    int selectedCodNum = radioBtnGr.getCheckedRadioButtonId();
    RadioButton selected = (RadioButton)findViewById(selectedCodNum);

    CheckBox selectedChBx = (CheckBox)findViewById();


    String FILENAME = "entry_log.csv";
    String entry = entryCode.getText().toString() + "-" +
            entryNum.getText().toString() +
            selected + "\n";
    try {
        FileOutputStream out = openFileOutput(FILENAME, Context.MODE_APPEND);
        out.write(entry.getBytes());
        out.close();
        toasting("Entry Saved");
    }catch (Exception e){
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void toasting(String msg){
    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();

}
</code></pre>

<p>}</p>

<p>and the a screen shot of the content of the CSV file<a href="https://i.stack.imgur.com/46o9h.png" rel="nofollow noreferrer">CSV content</a>
<a href="https://i.stack.imgur.com/UMcJw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UMcJw.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/VK7bj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VK7bj.png" alt="enter image description here"></a></p>

<p>OK, so i was able to fix the problem with the RadioButton correct Syntax, in order for it to save the Character it represent ( A, D, etc.. ).
 i had to change this part :
    String entry = entryCode.getText().toString() + "-" +
                entryNum.getText().toString() +
                selected + "\n";
adding .getText().toString() before " selected ". and it saved the right Character the chosen RadioButton selected in the CSV file.</p>

<p>But i am still not sure how to deal with the CheckBoxes ... how to save the name of the CheckBox checked in the CSV file !?</p>

<p>thought of adding an if else statement somewhere and save it in a way or another to the CSV file, but i am trying to figure out if will be able to do this and how, or IF THERE IS  A BETTER WAY !?</p>

<p>Will keep trying, hopefully someone could suggest something, and show me a good way to do it ...</p>

<p>I am still trying to get the right values and only the values of the checked boxes, which i was able only to list, True &amp; False for the check boxes states, with this code :</p>

<pre><code>    public void saveLogOnClick(View view){
    int selectedCodNum = radioBtnGr.getCheckedRadioButtonId();
    RadioButton selected = (RadioButton)findViewById(selectedCodNum);

   StringBuffer chkdbx = new StringBuffer();
    chkdbx.append(checkBoxGw.isChecked());
    chkdbx.append(checkBoxG7.isChecked());
    chkdbx.append(checkBoxKL.isChecked());
    chkdbx.append(checkBoxFW.isChecked());

    String FILENAME = "entry_log.csv";
    String entry = entryCode.getText().toString() + " - " +
            entryNum.getText().toString() + " - " +
            selected.getText().toString() +" - " + chkdbx.toString() +  "\n";
    try {
        FileOutputStream out = openFileOutput(FILENAME, Context.MODE_APPEND);
        out.write(entry.getBytes());
        out.close();
        toasting("Entry Saved");
    }catch (Exception e){
        e.printStackTrace();
    }
}
</code></pre>

<p>Not getting any responses on this post from previous participant, i am aware this is a straight forward issues, and it is simple to solve, but i am learning to implement new methods i am just learning by building this simple app.</p>

<p>Hope i get some hints on how to do this the right way.</p>

<p>Need to save the Value of the checked Boxes in the CSV file, not true and false;</p>

<p>Finally the right way to log the Value of the Checked boxes is this way :</p>

<pre><code>       List&lt;CheckBox&gt;checkBoxes=new ArrayList&lt;&gt;();
    checkBoxes.add(checkBoxGw);
    checkBoxes.add(checkBoxG7);
    checkBoxes.add(checkBoxKL);
    checkBoxes.add(checkBoxFW);
    String checkBoxText = "";

    for(CheckBox checkBox: checkBoxes){
        if(checkBox.isChecked()){
            checkBoxText = checkBoxText + " - " + checkBox.getText().toString();
        }
    }

    String FILENAME = "entry_log.csv";
    String entry = entryCode.getText().toString() + " - " +
            entryNum.getText().toString() + " - " +
            selected.getText().toString() +" - " + checkBoxText +  "\n";
</code></pre>

<p>but apparently it only saves the first check box that is checked and comes up in the list without going over the rest of the list or saving more than one checked box !?</p>

<p>So i fixed the part suggested by @NickIsaacs which is CheckBoxText + " , " + checBoxes.getText().toString(); and it should have saved the Checked Boxes, but instead i did not get any Values at all for the Check Boxes !?</p>

<p>I understand this is turning to a tutorial more than a question and answer, which should be considering how simple this is, but i am learning along the way, thank yall.</p>

<p>attached screen shot of the CSV file after the change in the code , no Values of Checked boxes ??</p>

<p>ALL RIGHT, at last, the minor issue was that in the code, that goes over the check boxes list, and list the checked boxes, separating each entry with " , " should be replaced with something else such as " - ", since the file the input is being saved on is CSV file.</p>

<p><a href="https://i.stack.imgur.com/eJUIW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eJUIW.png" alt="enter image description here"></a> </p>

<p><a href="https://i.stack.imgur.com/ReJp4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ReJp4.png" alt="enter image description here"></a> </p>