how to get end of file(EOF) - android

I have this code..
String you = buf.readLine();
Log.d("STRING", ikaw);
StringTokenizer st = new StringTokenizer(you);
double lat = Double.parseDouble(st.nextToken());
double lng = Double.parseDouble(st.nextToken());
int type = Integer.parseInt(st.nextToken());
String text = st.nextToken();
Log.d("File Reading stuff", "success = " + lat);
Log.d("File Reading stuff", "success = " + lng);
Log.d("File Reading stuff", "success = " + type);
Log.d("File Reading stuff", "success = " + text);
How am i suppose to test if it is already the end of file?
I don't know how many lines there are in my file and I need to print all of them.
I know i should place my code inside a while loop, but I have no idea how to get the end of file.

I assume the code you have listed is inside a loop, and I assume buf is an instance of BufferedReader. You just need to check for null after you call readLine(). It returns null when you've hit the end of the file.

Related

Example pointclouds with ARCore

Can I get some examples about pointclouds with ARCore? I really search it for days.
Currently I am working on an application similar to this one:This app
Has the feature to view pcl and save files in .ply format
Thanks
The HelloARSample app renders pointcloud in a scene. You can get the coordinates for each point and save them manually in a .ply format.
To get a point cloud, you can create a float buffer and add the points from each frame.
FloatBuffer myPointCloud = FloatBuffer.allocate(capacity);
Session session = new Session(context);
Frame frame = session.update();
try (PointCloud ptcld = frame.acquirePointCloud()) {
myPointCloud.put(ptcld.getPoints());
}
The float buffer saves the points like [x1,x1,z1,confidence1, x2,x2,z2,confidence2, ...].
I havent looked at .ply file struckture, but if you want to save it to a .pcd file, you must create a header, then insert a point per line. Here is a detailed explanation on how to do it.
I did it like this
private boolean savePointCloudFile() {
String data = "";
String fileName = "pointCloud";
int points = 0;
String holder = "";
// Write the point cloud data by iterating over each point:
for (int i=0; i<pointCloud.position(); i+=4) {
data += pointCloud.get(i) + " " + // x
pointCloud.get(i + 1) + " " + // y
pointCloud.get(i + 2) + " " + // z
pointCloud.get(i + 3) + "\n"; // confidence
points = i;
}
points = points / 4 - 10; // Removed last 10 points to prevent errors in case that I lost points
// Write file header
data = "# .PCD v.7 - Point Cloud Data file format\n" +
"VERSION .7\n" +
"FIELDS x y z rgb\n" + // confidence represented by rgb
"SIZE 4 4 4 4\n" + // you only have 3 values xyz
"TYPE F F F F\n" + // all floats
"COUNT 1 1 1 1\n" +
"WIDTH " + points + "\n" +
"HEIGHT 1\n" +
"VIEWPOINT 0 0 0 1 0 0 0\n" +
"POINTS " + points + "\n" +
"DATA ascii \n" + data;
//BufferedWriter out = new BufferedWriter(new FileWriter(new File(new File(Environment.getExternalStoragePublicDirectory(
// Environment.DIRECTORY_DOCUMENTS), fileName + ".pcd"));
try {
File file = new File(context.getExternalFilesDir(null), fileName +".pcd");
FileOutputStream stream = new FileOutputStream(file);
file.createNewFile();
//FileOutputStream out = new FileOutputStream(file);
stream.write(data.getBytes());
stream.close();
Log.i("SUCCESS", "File saved successfully in " + file.getAbsolutePath());
return true;
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
return false;
}
}
You should save the file from within a separate thread as it may cause a timeout error, because it takes too long to save so many points to a file.
You should get a file similar to this
# .PCD v.7 - Point Cloud Data file format
VERSION .7
FIELDS x y z rgb
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
WIDTH 3784
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 3784
DATA ascii
0.068493545 -0.18897545 -0.6662081 0.007968704
0.26833203 -0.18425867 -1.5039357 0.02365576
0.19286658 -0.2141684 -1.58289 0.038087178
0.070703566 -0.17931458 -0.69418937 0.016636848
0.044586033 -0.18726173 -0.6926071 0.024707714
0.04002113 -0.20350328 -0.68689686 0.018577512
0.029185327 -0.18594348 -0.73340106 0.12292312
0.0027626567 -0.20299685 -1.5578543 0.15424652
-0.031320766 -0.20478198 -0.70128816 0.13745676
-0.06351853 -0.20185146 -0.61755043 0.15234329
-0.08655308 -0.19128543 -0.6776818 0.170851
1.0159657 -0.41043654 -6.8713074 0.05946503
-0.031778865 -0.20536968 -1.5218562 0.15976532
-0.09223208 -0.19543779 -0.61643535 0.12331226
0.02384475 -0.20319816 -1.7497014 0.15273231
-0.10013421 -0.19931296 -0.5924832 0.16186734
0.49137634 -0.09052197 -5.7263794 0.16080469
To viaualize the point cloud you can use pcl_viewer or Matlab. In Matlab I just typed
ptCloud = pcread('pointCloud.pcd');
pcshow(ptCloud);

How to replace the word in the string by using replace in Android?

I want to replace one word in the String by using substring. But it seen didn't work.
for example: The string is 0000 , and I want to replace the first word from 0 to 1.
It should be 1000. but it doesn't.
The code is like the following
String WorkStatus = "0000";
if(WorkStatus.substring(0, 1).matches("0"))
{
WorkStatus.substring(0, 1).replace("0", "1");
Log.d(TAG, "WorkStatus.substring(0, 1) = " + WorkStatus.substring(0, 1) + "\n");
Log.d(TAG, "WorkStatus = " + WorkStatus + "\n");
}
It didn't work , the string always show 0000. And what I want is "1000"
Do I missing something ?
use this
String WorkStatus = "0000";
//You use matches, while you might as well use equals
if (WorkStatus.substring(0, 1).equals("0")) {
//reassign workstatus to workstatus where the first entry is a '1' + the last three chars "000"
WorkStatus = WorkStatus.substring(0, 1).replace("0", "1") + WorkStatus.substring(1, WorkStatus.length());
Log.d(TAG, "WorkStatus.substring(0, 1) = " + WorkStatus.substring(0, 1) + "\n");
Log.d(TAG, "WorkStatus = " + WorkStatus + "\n");
}
You didnt assign the modified string to WorkStatus
Another possibility is converting the string to a char[] and replacing the index, instead of working with substrings.
String WorkStatus = "0000";
char[] chars = WorkStatus.toCharArray();
if (chars[0] == '0') {
chars[0] = '1';
WorkStatus = new String(chars);
}
If you want other chars to become 1 instead of zero, alter the chars[0] into chars[index], where index is the index you want to change from 0 to 1
Or, even easier, use a StringBuilder:
int yourIndex = 2; //your index which you want to check for 0 and change to 1
StringBuilder sb = new StringBuilder("0000");
if (sb.charAt(yourIndex) == '0')
sb.setCharAt(yourIndex, '1');
WorkStatus = sb.toString();
method replace has a return value of the string after replaced
you shuold resign the result to the String
WorkStatus=WorkStatus.substring(0, 1).replace("0", "1")+ WorkStatus.substring(1, WorkStatus.length();
if you asign it to a new variable like the below code, you can get what you needed.
String newWorkStatus=WorkStatus.substring(0, 1).replace("0", "1")+WorkStatus.substring(1);
Log.d("LOG", "WorkStatus.substring(0, 1) = " + WorkStatus.substring(0, 1) + "\n");
Log.d("LOG", "WorkStatus = " + WorkStatus + "\n");
Log.d("LOG", "New WorkStatus = " + newWorkStatus + "\n");
WorkStatus.substring(0, 1).replace("0", "1"); returns 1, you should use StringBuilder instead of String.
My solution:
StringBuilder WorkStatus = new StringBuilder("0000");
int pos = WorkStatus.indexOf("0", 0);
if (pos != -1) {
WorkStatus.replace(pos, pos + 1, "1");
}
System.out.print(WorkStatus);

How to format how values are saved to a text file?

I am currently saving values to a text file in my application. The values are read from an EEG headset every second and are then stored within the text file.
The values are read using a handler e.g.:
final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
// msg.what determines the type of each message
switch (msg.what) {
case TGDevice.MSG_EEG_POWER:
eegPower = (TGEegPower) msg.obj;
//trace code
Log.d("LSD", "highAlpha: " + eegPower.highAlpha);
Log.d("LSD", "lowAlpha: " + eegPower.lowAlpha);
Log.d("LSD", "highBeta: " + eegPower.highBeta);
Log.d("LSD", "lowBeta: " + eegPower.lowBeta);
Log.d("LSD", "lowGamma: " + eegPower.lowGamma);
Log.d("LSD", "midGamma: " + eegPower.midGamma);
Log.d("LSD", "delta: " + eegPower.delta);
Log.d("LSD", "theta: " + eegPower.theta);
//adding all the EEGpowers to an arraylist to help add them to file
ArrayList<String> EEGPowers= new ArrayList<String>();
EEGPowers.add("highAlpha: " + eegPower.highAlpha);
EEGPowers.add("lowAlpha: " + eegPower.lowAlpha);
EEGPowers.add("highBeta: " + eegPower.highBeta);
EEGPowers.add("lowBeta: " + eegPower.lowBeta);
EEGPowers.add("lowGamma: " + eegPower.lowGamma);
EEGPowers.add("midGamma: " + eegPower.midGamma);
EEGPowers.add("delta: " + eegPower.delta);
EEGPowers.add("theta: " + eegPower.theta);
for(String s: EEGPowers){
writeToFileEEGPower(s);
}
//rest of handler...
The following method is the method used to save the values to file:
public void writeToFileEEGPower(String data){
//creating time for the file
Time t= new Time();
int timeFileSecond= t.second;
int timeFileDate= t.yearDay;
int timeFileYear= t.year;
//creating file name
String fileName= "MathsGame" + timeFileSecond + timeFileDate + timeFileYear + android.os.Build.SERIAL;
//creating the file where the contents will be written to
File file= new File(dir, fileName + ".txt");
FileOutputStream os;
try{
boolean append= true;
os= new FileOutputStream(file, append);
String writeMe =data + "\n";
os.write(writeMe.getBytes());
os.close();
} catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
The method and handler work as is, however my issue is that the values are not formatted as I would like when they are saved and are very messy as you can see:
E.g. of current text file:
I would like my text file to be formatted like this:
How can I implement this formatting in my code?
EDIT (Attempt at formatting):
//Declared globally outside handler
final int maxWordLength = 15;
String spaces[] = new String[maxWordLength];
//Within handler:
//setting up the array of maxlength etc
spaces[0] = "";
for(int i=1; i<maxWordLength ;i++){
spaces[i] = spaces[i-1]+" ";
}
int seconds=0;
//CREATING THE HEADER IN THE TEXT FILE
writeToFileEEGPower(order("Seconds")+order("highAlpha")+order("lowAlpha")+order("highBeta")+order("LowBeta")+
order("lowGamma")+order("midGamma")+order("Delta")+order("Theta")+ "\n");
//creating the string to be written to file
String line = order(seconds+"")+order(eegPower.highAlpha+"")+order(eegPower.lowAlpha+"")+order(eegPower.highBeta+"")+
order(eegPower.lowBeta+"")+order(eegPower.midGamma+"")+order(eegPower.delta+"")+order(eegPower.theta+"")+ "\n";
//write the string to file
writeToFileEEGPower(line);
Current sample output in text file:
You can define your max 'word' length, and complete every 'word' with empty spaces.
for example:
STEP 1
Define maxWordLength. this value describe the width of each column. Should this value will be slightly larger than the longest word (In this example length of 'highAlpha:'=10, choose number>10).
final int maxWordLength = 15;
STEP 2
Create array of empty spaces.
String spaces[] = {""," "," "," "," ", /*....*/ " "};// Complete the missing words up to the last word with 15 spaces.
Or create it dynamically:
String spaces[] = new String[maxWordLength];
spaces[0] = "";
for(int i=1; i<maxWordLength ;i++){
spaces[i] = spaces[i-1]+" ";
}
STEP 3
Define the 'Seconds' var, create the order function at your class and write the header row to the file:
int seconds=0;
//table header row
writeToFileEEGPower(order("Seconds")+order("highAlpha")+order("lowAlpha")+order("highBeta")/+.../);
private String order(String value){
return (value + spaces[maxWordLength-value.length()]);
}
STEP 4
For each handleMessage create a line and save it to the file:
String line = order(seconds+"")+order(eegPower.highAlpha+"")+order(eegPower.lowAlpha+"")+order(eegPower.highBeta+"")//+...;
writeToFileEEGPower(line);

How can I create Android logcat entries that provide a link to source code in Eclipse? [duplicate]

Is there any way to access automatically any Log in Logcat by a double click ?
Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance
at com.myapp.mypackage$Class.function(File.java:117)
And by Double-clicking on this line, I am automatically redirected to the related line of my code.
But, when I try to generate the same line in another Log, example :
Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");
The Double-Click doesn't work anymore ...
Any ideas ?
If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:
Enjoy!
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message
Log.e("TAG", "Looky here see", new Exception());

Is there any way to access automatically any Log in Logcat by a double click?

Is there any way to access automatically any Log in Logcat by a double click ?
Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance
at com.myapp.mypackage$Class.function(File.java:117)
And by Double-clicking on this line, I am automatically redirected to the related line of my code.
But, when I try to generate the same line in another Log, example :
Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");
The Double-Click doesn't work anymore ...
Any ideas ?
If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:
Enjoy!
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message
Log.e("TAG", "Looky here see", new Exception());

Categories

Resources