private method , loop , switch , help?
why wont this work
i'm checking if any of the char in the string are letters . easy as that , though its not so easy
( every break, the method name are shown as errors , and the i = i + 1 is said to be dead code , wtf?!? [in eclipse] )
Code :
private boolean isWord(String check){
int loop = check.length();
for ( int i = 0 ; i != loop+1 ; i = i + 1){
switch (check.toLowerCase().charAt(i)){
case 'a':
return true;
break;
case 'b':
return true;
break;
// ........
case 'y':
return true;
break;
case 'z':
return true;
break;
default:
return false;
break;
}
}
}
Re: private method , loop , switch , help?
The reason is Java is hitting the return statements, so everything after them won't ever get executed.
Code :
case 'a':
return true; // method is done
break; // won't ever get executed
The java compiler is also smart enough to recognize that all the possible paths through the switch statement return on the first pass, so the for-loop incrementation also will never get executed.
What you should do is return true in all the cases (leave off the breaks) and you can remove the default statement completely.
Then, after your for loop, have the method return false (since it can only get there if none of the cases in the switch statement were executed).
Re: private method , loop , switch , help?
the for loop is less to check through the loop multiple times , but to check each letter in the string
cause that piece is to check if each line that's read from a file has letters or not , if it doesn't have letters and isn't blank (already checked when reading it ) then it will convert it to 3 sets of 3 doubles ( vertex placement for each polygon ) that the app is reading
shortened example ( only 2 of the 12 polygons for each of the 6 sides of a cube )
Code :
Box01
-12.000000 10.000000 37.000000 -12.000000 0.000000 37.000000 12.000000 10.000000 37.000000
-12.000000 0.000000 37.000000 12.000000 0.000000 37.000000 12.000000 10.000000 37.000000
Re: private method , loop , switch , help?
You'll still run into the same issues I pointed out above. It's better to read in one "word" (stuff separated by white-spaces), then parse those. That is, if it's safe to say you won't get any input like this:
(stuff with spaces in-between)
- 500.0
5 .3
5. 3
Re: private method , loop , switch , help?
idk how that would work , but here's how i have it set up right now
readFile(filename)
checkFileType(compleateFile , file name)
readFileIntoFile3DObject(compleateFile)
this is where i'm at . and i borke this down to where it will read a line .
if it comes back with a letter its the name of the object ( it is posable to have
a name with a space in it though) thenit checks the next , if it doesn't have a
letter its the polygon , witch is sent to the next object down the hierarchy to
divide that up and send each group of doubles to the vertex object ( witch is
just 3 doubles . essentially
File3D.addObjects(file[])
object.creatObject(name,Polygons[])
polygon.creatPolygon(lineXofPolygons)
vertex.creatVerticies(coordinates[])
then once its all done readFileInto3DObject returns a File3D that has all of the
stuff in it for later use
its probably really messy , but its the easiest way for me to understand how to break it all down
i just need help with the finding out how to check all the char in each line so i know where to send them
Re: private method , loop , switch , help?
I'm guessing that the files are arranged as such:
object name
vertices
object name
vertices
... etc
right?
In that case, here's some code that'll parse things for you:
Code :
public class Parse
{
ArrayList<String> objectNames;
ArrayList<ArrayList<Double>> vertices;
Parse(File filename) throws IOException
{
if (!validFile(filename)) // I'm not sure what is a valid file, so you'll have to implement this method
{
throw new IOException();
}
objectNames = new ArrayList<String>();
vertices = new ArrayList<ArrayList<Double>>();
Scanner reader = new Scanner(filename);
boolean lastReadWasName = true;
String partialName = "";
ArrayList<Double> partialVertices = new ArrayList<Double>();
while (reader.hasNext())
{
String parsing = reader.next();
if (hasLetter(parsing))
{
if (!lastReadWasName)
{
vertices.add(partialVertices);
partialVertices = new ArrayList<Double>();
lastReadWasName = true;
}
partialName += parsing;
}
else
{
if (lastReadWasName)
{
objectNames.add(partialName);
partialName = "";
lastReadWasName = false;
}
partialVertices.add(Double.parseDouble(parsing));
}
}
}
public static boolean hasLetter(String word)
{
for (int i = 0; i < word.length(); i++)
{
if (Character.isLetter(word.charAt(i)))
{
return true;
}
}
return false;
}
}
Re: private method , loop , switch , help?
its more like
3d file only 1 of these
3d object -can be hundreds per file
polygons -can be thousands in each object
verticals -3 per polygon
Re: private method , loop , switch , help?
yes, but the file containing all the 3D objects/polygons were formatted as such, yes?
If you want, it's possible to further process the list of vertices/3d object into polygon objects that have 3 vertices each.
(I'm assuming that since you're doing 3d objects, each vertice has an x, y, and z value)
Code :
public class Vertex
{
public double x, y , z;
public Vertex(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
Code :
public class Polygon
{
private Vertex v1,v2,v3;
public Polygon(Vertex v1, Vertex v2, Vertex v3)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
public static ArrayList<Polygon> parseVertices(ArrayList<Vertex> vertices)
{
if (vertices.length() % 3 != 0)
{
throw new RuntimeException("Malformed data: must have 3 vertices/polygon");
}
ArrayList<Polygon> object = new ArrayList<Polygon>();
for (int i = 0; i < vertices.size(); i+=3)
{
object.add(new Polygon(vertices.get(i) , vertices.get(i+1), vertices.get(i+2));
}
return object;
}
}
Re: private method , loop , switch , help?
thats more or less what i got , though its a bit longer and less pretty , lol ( mines the longer uglier 1 XP )
Re: private method , loop , switch , help?
why not replace the != with < and see if any error concerning that still remains