Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: private method , loop , switch , help?

  1. #1
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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] )

    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;
    			}
    		}
    	}
    Programming: the art that fights back


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: private method , loop , switch , help?

    The reason is Java is hitting the return statements, so everything after them won't ever get executed.

    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).

  3. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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 )
    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
    Programming: the art that fights back

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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

  5. #5
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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
    Programming: the art that fights back

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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;
         }
    }

  7. #7
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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
    Programming: the art that fights back

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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)

    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;
         }
    }
    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;
         }
    }

  9. #9
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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 )
    Last edited by wolfgar; November 8th, 2009 at 11:06 PM.
    Programming: the art that fights back

  10. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: private method , loop , switch , help?

    why not replace the != with < and see if any error concerning that still remains

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 04:02 PM
  3. switch with Enums
    By chronoz13 in forum Loops & Control Statements
    Replies: 17
    Last Post: October 8th, 2009, 08:08 PM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM