• Android-Java Logic Error (Can't Break For Loop Properly)
    12 replies, posted
The app writes numbers to a file via OutputStreamWriter, and the file comes out looking something like this: [code]5.0 12.42 3.12[/code] and will have a variable amount of lines (a new number is recorded every time a certain button is pushed) up to 2048 lines. So I have this code to then average all the numbers on lines: [code]public void getMile() { try{ fIn = openFileInput("gasRecord.txt"); //Stuff to read from file isr = new InputStreamReader(fIn); for (int i = 0; i < 2047; i++) //For loop to go through each line in gasRecord.txt { allMile[i] = 0.0; //The current null value of the allMile array is set to 0 char[] inputBuffer = new char[511]; isr.read(inputBuffer); //Read the value (mileage) written on the current line, hold it in the character array String readLine = new String(inputBuffer); //Convert it to a String allMile[i] = Double.valueOf(readLine); //convert it to a double if (readLine.equals(null)) { totalMile = totalMile/i; //average the total break;//break the for loop } totalMile += allMile[i]; //add it to totalMile (totalMile starts at 0) } } catch (Exception e) {} resultTF.setText(Double.toString(totalMile), TextView.BufferType.EDITABLE); //Set the EditText to the average Mileage }[/code] However, no matter what I do, it always skips the if statement. So far I've tried: allMile[i] == 0 allMile[i - 1] == allMile[i] readLine.equals("") (and others I can't remember). Since it skips the for loop, it just adds them together. For example, this: [code]2.0 4.0[/code] would result in 6.0
Doeesnt doing [cpp]String readLine = new String(inputBuffer);[/cpp] ensure readline wont be null, thus the if wont get triggered?
[QUOTE=Richy19;37530023]Doeesnt doing [cpp]String readLine = new String(inputBuffer);[/cpp] ensure readline wont be null, thus the if wont get triggered?[/QUOTE] I believe so. Changed it to: [code]char[] inputBuffer = new char[13]; isr.read(inputBuffer); //Read the value (mileage) written on the current line, hold it in character array String readLine = new String(inputBuffer); //convert it to a string allMile[i] = Double.valueOf(readLine); //convert it to a double if (readLine.equals(" ") && i > 0) { totalMile = totalMile/i; //average the total break;//break the for loop }[/code] However, this still does not work. What value does it result in if it is empty? [editline].[/editline] It results in the last value + how ever many characters are left. One sec, I should be able to fix this.
[QUOTE=Dr. Evilcop;37530125]I believe so. Changed it to: [code]char[] inputBuffer = new char[13]; isr.read(inputBuffer); //Read the value (mileage) written on the current line, hold it in character array String readLine = new String(inputBuffer); //convert it to a string allMile[i] = Double.valueOf(readLine); //convert it to a double if (readLine.equals(" ") && i > 0) { totalMile = totalMile/i; //average the total break;//break the for loop }[/code] However, this still does not work. What value does it result in if it is empty?[/QUOTE] I dont use java regularly but try readline.length() == 0
[QUOTE=Richy19;37530146]I dont use java regularly but try readline.length() == 0[/QUOTE] [code]package com.Venatus.mileagecalc; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.DecimalFormat; import java.text.DecimalFormat; public class CalcMain extends Activity { private EditText milesDrivenTF; private EditText gasUsedTF; private EditText resultTF; private Button CalcMileB; private double[] allMile = new double[2047]; //store up to 2048 saved mileage records private double totalMile = 0.0; private String milesDriven; private String gasUsed; private double mileage = 0.0; private double milesDrivenD = 0.0; private double gasUsedD = 0.0; private String avMile; private char[] inputBuffer = new char[13]; private String readLine; static DecimalFormat shortDec = new DecimalFormat("##########.00"); @Override public void onCreate(Bundle savedInstanceState) //This is Main { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calc_main); //Create the screen/GUI milesDrivenTF = (EditText)findViewById(R.id.milesDrivenTF); //Add elements to GUI gasUsedTF = (EditText)findViewById(R.id.gasUsedTF); resultTF = (EditText)findViewById(R.id.resultTF); CalcMileB = (Button)this.findViewById(R.id.CalcMileB); CalcMileB.setOnClickListener(CMBListener); //Add Listener for Button getMile(); //Call getMile func on startup } @Override public boolean onCreateOptionsMenu(Menu menu) //For options (none in this program) { getMenuInflater().inflate(R.menu.activity_calc_main, menu); return true; } private OnClickListener CMBListener = new OnClickListener() { public void onClick(View v) { //You can only get Strings from a EditText milesDriven = milesDrivenTF.getText().toString(); //Get Text from TF gasUsed = gasUsedTF.getText().toString(); //Get Text from TF milesDrivenD = Double.parseDouble(milesDriven); //Convert Strings to Doubles gasUsedD = Double.parseDouble(gasUsed); //Convert Strings to Doubles mileage = milesDrivenD/gasUsedD; //Find Mileage avMile = Double.toString(mileage); //Convert back to string, you can only write strings to files try{ FileOutputStream fOut = openFileOutput("gasRecord.txt", MODE_WORLD_READABLE); OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.write(avMile); osw.flush(); osw.close(); } catch (Exception e) {} //write to file "gasRecord.txt" getMile(); //call getMile Func to refresh mileage } }; public void getMile() { try{ FileInputStream fIn = openFileInput("gasRecord.txt"); //Stuff to read from file InputStreamReader isr = new InputStreamReader(fIn); for (int i = 0; i < 2047; i++) //For loop to go through each line in gasRecord.txt { allMile[i] = 0.0; //The current value of the allMile array is set to 0 isr.read(inputBuffer); //Read the value (mileage) written on the current line, hold it in character array readLine = new String(inputBuffer); //convert it to a string allMile[i] = Double.valueOf(readLine); //convert it to a double if (readLine.length() == 0 && i > 0) { totalMile = totalMile/i; //average the total break;//break the for loop } totalMile += allMile[i]; //add it to totalMile (totalMile starts at 0) } } catch (Exception e) {} resultTF.setText(Double.toString(totalMile), TextView.BufferType.EDITABLE); //Set the EditText to the average Mileage } }[/code] [img]https://dl.dropbox.com/u/22565769/fukkinwat.png[/img] Well that did...something. There's the full Source in case an error I can't see is somewhere else.
Unless I have done something very rong with java untill now, indexes in arrays and such should be of the full number you want, so when you do private double[] allMile = new double[2047]; it should be 2048 likewse here for (int i = 0; i < 2047; i++) that shoulldnt be your problem but i thought i should point it out, as for your problem maybe leave a link in WDYNHW see if others can figure it out
[QUOTE=Richy19;37530246]Unless I have done something very rong with java untill now, indexes in arrays and such should be of the full number you want, so when you do private double[] allMile = new double[2047]; it should be 2048 likewse here for (int i = 0; i < 2047; i++) that shoulldnt be your problem but i thought i should point it out, as for your problem maybe leave a link in WDYNHW see if others can figure it out[/QUOTE] Arrays start at 0, so [2047] would hold 2048 values :v: That's why i starts at 0 as well.
yes but say you want an array of 5 elements, you put [5] not 4 in a more basic example say you want 1 element, you put [1] not [0] and for (int i = 0; i < 2047; i++) i < 2047 means i is under, whereas it should be <= 2047 or < 2048 seeing as the last index is 2047 right now its setup correctly however only for 2047 elements not 2048 as you want it to be
"a = new int[3]" makes an array holding 3 values: a[0], a[1], and a[2]
[QUOTE=chimitos;37533634]"a = new int[3]" makes an array holding 3 values: a[0], a[1], and a[2][/QUOTE] Oh, my mistake.
It'd make more sense to store this list of numbers in an sqlite database, rather than a text file. That way you can store the integers as integers, so you don't have to convert to/from strings, and you can easily add additional columns, such as the date of each entry.
string.length() == 0 was deprecated a while ago, using string.isEmpty() is safer. EDIT: No, I'm full of crap, they're the same thing.
In principle, isEmpty() may be faster since it doesn't require the string to actually determine its exact length. In an implementation based on null-terminated strings, length() would have to count characters with something analogous to C's strlen() function, while isEmpty() could just check whether the first character is null. But Java strings can contain null characters, which makes it complicated (though still possible) to use C-style null termination, and a typical implementation will probably just store the length in a separate field. The [url=http://www.docjar.com/html/api/java/lang/String.java.html]implementation in OpenJDK 7[/url] is just: [cpp] public boolean isEmpty() { return count == 0; } [/cpp]
Sorry, you need to Log In to post a reply to this thread.