-
geting value from text
Hi all. My question is a hava a text file that include some datas like this
N1 G94 G90 T1 M6
N2 G0 G56 X0 Y0
N3 G43 H01 Z100
N4 S1500 M3
N5 G01 Z55 F5000 M8
N4 X208.96 Y-99.963
N5 G1 G94 Z14.052 F300.
N6 Y-100.396 Z13.63
N7 Y-100.853 Z13.235
N8 Y-101.333 Z12.869
N9 Y-101.835 Z12.532
N10 Y-102.355 Z12.226
N11 Y-102.894 Z11.952
N12 Y-103.448 Z11.712
N13 Y-104.015 Z11.505
N14 Y-104.594 Z11.332
N15 Y-105.183 Z11.195
here I must find X ,Y and Z and create a table with their values. It not nessesar to read from text I can only copy form text to console .Problem is how to get this values and create a table .and if line doesnt contain X or Y or Z it must take it from preveous argument..What do u prefer to use here?Thanks ..
-
Here's a question for you: what will be the X, Y and Z values for the first line? Also, I'd check the regular expression mechanism available in Java.
kind regards,
Jos
-
they will be discarded..My program must check all lines and if there X or Y or Z it must take values after them and prtint them in a table like this
0.0000 0.0000 0.0000
0.0000 0.0000 100.0000
0.0000 0.0000 55.0000
208.9600 -99.9630 55.0000
208.9600 -99.9630 14.0520
208.9600 -100.3960 13.6300
208.9600 -100.8530 13.2350
208.9600 -101.3330 12.8690
208.9600 -101.8350 12.5320
208.9600 -102.3550 12.2260
208.9600 -102.8940 11.9520
208.9600 -103.4480 11.7120
208.9600 -104.0150 11.5050
208.9600 -104.5940 11.3320
208.9600 -105.1830 11.1950
-
So basically you need to have variables:
and you have to read a line while you haven't hit the end of file condition; split the line (read the API documentation for the String class, how to split the line using the "\\s+" regular expression. It returns an array (String[]) of String parts. Check every part if it starts with an 'X', 'Y' or 'Z' character. If so update the corresponding variable.
If you have processed the entire line the (possibly new) values of variables X, Y and Z have the values you want for your table.
kind regards,
Jos
-
I looked up some books and I made it works like that
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CnsPro {
static int[] coordiante = {0,0,0};
static int[] tablo ;
public static void main(String[] args) throws IOException {
Pattern x = Pattern.compile("(?:X)+([+-](\\d(\\.(\\d+))|\\d+(\\.(\\d+))|\\d+)|\\d(\\.(\\d+))|\\d+(\\.(\\d+))|\\d+)");
Pattern y = Pattern.compile("(?:Y)+([+-](\\d(\\.(\\d+))|\\d+(\\.(\\d+))|\\d+)|\\d(\\.(\\d+))|\\d+(\\.(\\d+))|\\d+)");
Pattern z = Pattern.compile("(Z([+-]?[0-9.]*))");
String txt = "N1 G94 G90 T1 M6 N2 G0 G56 X0 Y0 N3 G43 H01 Z100 N4 S1500 M3N5 G01 Z55 F5000 M8 N4 X208.96 Y-99.963N5 G1 G94 Z14.052 F300. N6 Y-100.396 Z13.63 N7 Y-100.853 Z13.235 N8 Y-101.333 Z12.869N9 Y-101.835 Z12.532N10 Y-102.355 Z12.226N11 Y-102.894 Z11.95";
Matcher mX = x.matcher(txt);
Matcher mY = y.matcher(txt);
Matcher mZ = z.matcher(txt);
while ( mX.find()){
System.out.print(mX.group());
}
System.out.print("\n");
while (mY.find()){
System.out.print(mY.group());
}
System.out.print("\n");
while (mZ.find()){
System.out.print(mZ.group());
}
System.out.print("\n");
}
}
Here My question is for X and Y I wrote this long regular expresion about 5 hours and then i found this code which I used for Z and that is the exakctly make the same think..Why my code is so longer?:D And now I must take those value to some table but I couldent find any vay to get value from this string without x,y,z and make them table like previous reply..any idea?
-
-
Quote:
Originally Posted by
Mekonom
any other idea?
Better split the line on one or more spaces and check the resulting String[] for a first character X, Y or Z; you won't need those regular expression monsters like that.
kind regards,
Jos
-
I understood but I can find now like this
X0X208
Y-99.963Y-100.396Y-100.853Y-101.333Y-101.835Y-102.355Y-102.894
Z100Z55Z14.052Z13.63Z13.235Z12.869Z12.532Z12.226Z1 1.95
here I must again make regular expresisiaon to take all values? and if I found value it vill be String and I must convert it to integer to use in cerating graph.But when converting it gives error that there is a sign...I used for converting this
int aInt = Integer.parseInt(mX.group);
or I mst look signh and value seprately?
-
Hi all again ..I solved most of my problem .Now I can take value after X,y,z and converted to flow .But I mst reate a table wich contains x,y,z on one line..but for example when reading from text if line doesnt contains x or y or z it mustput its previous value ..In my project I added new class and it reads value from text file but in Main class when I am trying to use object of this calss to my string it doesnt work ..:(
Code:
public class CnsPro {
static float[] coordiante = {0, 0, 0};
static int[][] tablo;
static float xf, yf, zf;
static String x_str;
static String y_str;
static String z_str;
static FileInput file = new FileInput();
static void doit() {
Pattern x = Pattern.compile("X([+-]?[0-9.]*)");
Pattern y = Pattern.compile("Y([+-]?[0-9.]*)");
Pattern z = Pattern.compile("Z([+-]?[0-9.]*)");
String txt = file.str;
Matcher mX = x.matcher(txt);
Matcher mY = y.matcher(txt);
Matcher mZ = z.matcher(txt);
for (int i = 0; i < txt.length(); i++) {
if (mX.find()) {
coordiante[0] = Float.parseFloat(mX.group(1));
System.out.print(" " + coordiante[0]);
}
if (mY.find()) {
coordiante[1] = Float.parseFloat(mY.group(1));
System.out.print(" " + coordiante[1]);
}
if (mZ.find()) {
coordiante[2] = Float.parseFloat(mZ.group(1));
System.out.print(" " + coordiante[2]);
}
}
}
public static void main(String[] args) throws IOException {
new CnsPro().doit();
}
}
Code:
public class FileInput {
static String str;
public static void main(String[] args) {
File file = new File("/Users/meko16/NetBeansProjects/FileinputDeneme/src/fileinputdeneme/tablo.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
String str = dis.readLine();
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-
I solved problem and Here Codes
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CnsPro {
static float [][] tablo;
static float xf, yf, zf;
static String x_str;
static String y_str;
static String [] z_str;
static String txt;
static float GX,GY,GZ ;
// static FileInput file = new FileInput();
static void doit() {
Pattern x = Pattern.compile("X([+-]?[0-9.]*)");
Pattern y = Pattern.compile("Y([+-]?[0-9.]*)");
Pattern z = Pattern.compile("Z([+-]?[0-9.]*)");
txt="O1005"+
"N1 G94 G90 T1 M6"+
"N2 G0 G56 X0 Y0"+
"N4 S1500 M3"+
"N5 G01 Z55 F5000 M8"+
"N4 X208.96 Y-99.963"+
"N5 G1 G94 Z14.052 F300."+
"N6 Y-100.396 Z13.63"+
"N7 Y-100.853 Z13.235"+
"N8 Y-101.333 Z12.869"+
"N9 Y-101.835 Z12.532"+
"N10 Y-102.355 Z12.226"+
"N11 Y-102.894 Z11.952"+
"N12 Y-103.448 Z11.712"+
"N13 Y-104.015 Z11.505"+
"N14 Y-104.594 Z11.332"+
"N15 Y-105.183 Z11.195"+
"N16 Y-105.778 Z11.094"+
"N17 Y-106.379 Z11.029"+
"N18 Y-106.982 Z11."+
"N19 Y-150.078 F96."+
"N20 X208.935 Y-150.675"+
"N21 X208.869 Y-151.272"+
"N22 X208.763 Y-151.87"+
"N23 X208.625 Y-152.428"+
"N24 X208.42 Y-153.064"+
"N25 X208.178 Y-153.661"+
"N26 X207.884 Y-154.258"+
"N27 X207.53 Y-154.855"+
"N28 X207.11 Y-155.453"+
"N29 X206.609 Y-156.05"+
"N30 X206.237 Y-156.433"+
"N31 X205.639 Y-156.963"+
"N32 X205.042 Y-157.407"+
"N33 X204.445 Y-157.78"+
"N34 X203.848 Y-158.092"+
"N35 X203.25 Y-158.35"+
"N36 X202.653 Y-158.558"+
"N37 X202.056 Y-158.721"+
"N38 X201.459 Y-158.841"+
"N39 X200.862 Y-158.919"+
"N40 X200.265 Y-158.956";
z_str=txt.split("N");
GX = GY = GZ = 0;
tablo = new float[z_str.length - 1][3];
for(int j=1;j<z_str.length;j++){
Matcher mX = x.matcher(z_str[j]);
Matcher mY = y.matcher(z_str[j]);
Matcher mZ = z.matcher(z_str[j]);
if (mX.find())
GX = Float.parseFloat(mX.group(1));
tablo[j-1][0]= GX;
if (mY.find())
GY = Float.parseFloat(mY.group(1));
tablo[j-1][1]= GY;
if (mZ.find())
GZ = Float.parseFloat(mZ.group(1));
tablo[j-1][2]= GZ;
}
for(int i=0;i <tablo.length;i++) {
for(int j=0;j<3;j++){
System.out.print(" "+tablo[i][j]);
}
System.out.print("\n");
}
}
// }
public static void main(String[] args) throws IOException {
new CnsPro().doit();
}
}
It work. But I want to take this text from file and use filereader but I dont know howto if ony one will help will be great .if not forget it :)
-
OK I solved it too .Dont Attention on this post ..