Results 1 to 12 of 12
  1. #1
    127.0.0.1 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default Help with ugly code

    My friend offered to help me with my assignment for my beginning programming class. The assignment consists of parsing a string that contains a list of text, numbers, and semicolons into a arraylist of an object with a name and number field. However, I have no idea what is going on in the example he gave me.. primarily the crazy for loops. Could someone tell me why this works?

    Example of input string:
    text 1.1; text 1.1; text 1.1;
    Code:
    Java Code:
    import java.io.*;
    import java.util.ArrayList;
    public class format_string
    {
    	//Constants
    	public static char TOKEN_END  = ';';
    	//Classes
    	public class record
    	{
    		public String name   = "";
    		public double number = 0.0;
    		public record(String a,double b)
    		{
    			name = a;
    			number = b;
    		}
    	}
    	//Objects
    	public ArrayList<record> record_list = new ArrayList<record>();
    	//--------------
    	// Constructor
    	//--------------
    		public format_string ()
    		{
    			//null
    		}
    	//----------------
    	// print_records 
    	//----------------
    		public void print_records (String input_list)
    		{
    			int    index       = 0;
    			double temp_double = 0.0;
    			input_list = "!; " + input_list;
    			output_string_line("\nRecords in original sequence.");
    			for(int j,i = input_list.length() - 1;i > -1 && input_list.charAt(i - 1) != '!';i--)
    				if(input_list.charAt(i) == TOKEN_END)
    				{
    					for(j = --i;i < input_list.length() - 1 && input_list.charAt(i) != ' ' ;i--){}
    					try{
    						temp_double = Double.parseDouble(input_list.substring(i + 1 ,j + 1));
    					}catch (NumberFormatException error){
    						System.exit(-1);
    					}
    					for(j = --i;i < input_list.length() - 1 && input_list.charAt(i) != TOKEN_END;i--){}
    					if(input_list.charAt(i) == ';')
    						i++;
    						if(input_list.charAt(i) == ' ')
    							i++;
    					record_list.add(new record (input_list.substring(i--, j + 1),temp_double));
    				}
    				else
    					System.exit(-1);
    			for(int i = record_list.size() - 1;i > -1;i--)
    				output_string_line(record_list.get(i).name + " " + record_list.get(i).number + TOKEN_END);
    			output_string_line("");
    		}
    ...

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    796
    Rep Power
    6

    Default

    Post the code with code tag.we can give suggestions if you get problem.
    Noone will have time to scan your code.
    Put s.o.p and understand each and every step.
    Ramya:cool:

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default

    Leave the dirty work to a Properties object, i.e. after converting those semicolons to System.getProperty("line.separator") characters make a Properties object read the String (through a StringReader) and voila.

    kind regards,

    Jos

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,249
    Rep Power
    25

    Default

    @JosAH Knowing how to use tools is very important. However,I think, knowing how to program is more important. Students should write these kinds of programs to know how to do the tasks that are done by tools. There will be less "magic" about using a tool if they know what the tool is doing.
    This is a philosophical thing and I'm sure there will be "flames" shooting out here.\

    For example
    Could someone tell me why this works
    The OP asks why code works. If the author of the code had commented the code, then the explanation would be there.
    Last edited by Norm; 06-28-2010 at 01:08 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    @JosAH Knowing how to use tools is very important. However,I think, knowing how to program is more important. Students should write these kinds of programs to know how to do the tasks that are done by tools. There will be less "magic" about using a tool if they know what the tool is doing.
    This is a philosophical thing and I'm sure there will be "flames" shooting out here.
    No flames from me; but if one reads the API for the Properties class one knows what that tool is doing (I didn't do more than that). If this thread continues your way it is going to stay ugly code (not because of you but because of doing what already has been done before ;-).

    kind regards,

    Jos

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,249
    Rep Power
    25

    Default

    Your write about it being ugly code.
    Many poor techniques used.
    No {} for large if statement bodies
    Hardcoded values vs using final variables.
    Expressions NOT enclosed in ()

    It reminds me of contests I've seen with C programmers where they try to write code that no one can figure out how it works.

    I rewrote it with some debug println() that should show you what is happening with the index values i and j that are used to scan out the records:
    Java Code:
    /*My friend offered to help me with my assignment for my beginning programming class.
    The assignment consists of parsing a string that contains a list of text, numbers, 
    and semicolons into a arraylist of an object with a name and number field. However, 
    I have no idea what is going on in the example he gave me.. primarily the crazy for loops. 
    Could someone tell me why this works?
    
    Example of input string:
    text 1.1; text 1.1; text 1.1;
    */
    
    import java.io.*;
    import java.util.ArrayList;
    
    
    public class Format_String
    {
    	//Constants
    	public static char TOKEN_END  = ';';
       final static char FIRST_CHAR = '!';
    	//Classes
    	public class record    	{
    		public String name   = "";
    		public double number = 0.0;
    		public record(String a,double b) 		{
    			name = a;
    			number = b;
    		}
    	}
    	//Objects
    	public ArrayList<record> record_list = new ArrayList<record>();
    	//--------------
    	// Constructor
    	//--------------
    		public Format_String()   		{
    		}
    
    	//----------------
    	// print_records 
    	//----------------
    		public void print_records (String input_list)  		{
    			int    index       = 0;
    			double temp_double = 0.0;
    
    			input_list = FIRST_CHAR + "" + TOKEN_END + " " + input_list; // Prepend special characters  
             System.out.println("Scanning: >" + input_list + "<");
    
    			for(int j,i = input_list.length() - 1; i > -1 && (input_list.charAt(i - 1) != FIRST_CHAR); i--) {
    				if(input_list.charAt(i) == TOKEN_END){
                   // scan j to ...
    					for(j = --i; i < (input_list.length() - 1) && (input_list.charAt(i) != ' ') ; i--){}
                   System.out.println("1j=" + j);
    					try{
    						temp_double = Double.parseDouble(input_list.substring(i + 1 ,j + 1));
    					}catch (NumberFormatException error){
    						System.exit(-1);
    					}
    
                   // Scan j to ...
    					for(j = --i; i < (input_list.length() - 1) && (input_list.charAt(i) != TOKEN_END); i--){}
                   System.out.println("2j=" + j);
    
    					if(input_list.charAt(i) == TOKEN_END) {
    						i++;     // adj index past
                   }
    					if(input_list.charAt(i) == ' ') {
    							i++;
                   }
                   System.out.println("i=" + i + ", j=" + j);
    					record_list.add(new record (input_list.substring(i--, j + 1), temp_double));
    				}
    				else {
                   System.out.println("i at wrong loc? i=" + i + ", charAt[i]=" + input_list.charAt(i));
    					System.exit(-1);
                }
             } // end for(i)
    
             // Now report the results
    			System.out.println("\nRecords in original sequence.");
    			for(int i = record_list.size() - 1;i > -1;i--)
    				System.out.println(record_list.get(i).name + " " + record_list.get(i).number + TOKEN_END);
    //			System.out.println("");
    		}
          //-------------------------------------------------------------------------
          public static void main(String[] args) {
           	new Format_String().print_records("text1 1.1; text2 1.2; text3 1.3;");
          }
    }
    /* Output:
    Running: java.exe -classpath D:\JavaDevelopment;.  -Xmx512M Format_String
    
    Scanning: >!; text1 1.1; text2 1.2; text3 1.3;<
    1j=33
    2j=29
    i=25, j=29
    1j=22
    2j=18
    i=14, j=18
    1j=11
    2j=7
    i=3, j=7
    
    Records in original sequence.
    text1 1.1;
    text2 1.2;
    text3 1.3;
    
    0 error(s)
    */

  7. #7
    127.0.0.1 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Wow, I feel a lot better now understanding and looking back at the original code. Thanks for the help Norm, i should be able to write my own with no problem now. :)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by 127.0.0.1 View Post
    Wow, I feel a lot better now understanding and looking back at the original code. Thanks for the help Norm, i should be able to write my own with no problem now. :)
    Now this thread is more or less solved, have a look at how I would've done it:

    Java Code:
    import java.io.IOException;
    import java.io.StringReader;
    import java.util.Properties;
    
    
    public class T {
    
    	public static void main(String[] args) {
    		String t="text1 1.1; tex2 1.2; text3 1.3;";
    		
    		t= t.replaceAll(";", System.getProperty("line.separator"));
    		StringReader sr= new StringReader(t);
    		Properties p= new Properties();
    		try {
    			p.load(sr);
    		}
    		catch(IOException ioe) { /* can't happen here */ }
    		
    		System.out.println(p);
    	
    	}
    }
    kind regards,

    Jos

  9. #9
    127.0.0.1 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    JosAH, how would you then parse the double out of the string without indexing through it?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by 127.0.0.1 View Post
    JosAH, how would you then parse the double out of the string without indexing through it?
    You do know how to parse a double given a String do you? (hint:have a look at the Double class). All you have to do is look up that property (say "test1") in the Properties object and pass it to that method in the Double class.

    kind regards,

    Jos

  11. #11
    127.0.0.1 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    I think I understand what you mean. I am just not keen on all of the numerous libraries java has built in (i am used to pascal and strings being an array of characters). Thanks for the code though JosAH, it's nice to see how easy it can be if you start learning the different tools java has.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by 127.0.0.1 View Post
    I think I understand what you mean. I am just not keen on all of the numerous libraries java has built in (i am used to pascal and strings being an array of characters). Thanks for the code though JosAH, it's nice to see how easy it can be if you start learning the different tools java has.
    Yep, Pascal is more of an educational language, i.e. you have to do almost everything yourself. Java comes with an abundance of classes that can do almost everything for you. The language itself is still very simple but you have to know how to find your way around in that huge bunch of classes. If you know how to do that, those classes can be a big help, otherwise you keep on reinventing the wheel over and over again. You'll get used to it ;-)

    kind regards,

    Jos

Similar Threads

  1. Nested generics and ugly syntax
    By P3t3r in forum Advanced Java
    Replies: 2
    Last Post: 05-26-2010, 04:06 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Ugly XML to parse
    By JohnST in forum New To Java
    Replies: 2
    Last Post: 01-03-2010, 04:48 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •