Results 1 to 8 of 8
  1. #1
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default Using Throwable to store data in buffers and "throwing" data

    Hi. This is my first post. I am taking an undergraduate course in Java; Algorithms and Data structure. I am not sure if that is qualified as advanced material here. I assumed it was not. (Note to admin: Please do move to the correct topic where this post belongs - I was uncertain regarding the complexity of my question which I shall pose now)

    I wish to achieve the following code :
    Java Code:
    if (data instanceof Type1) {
        // put data in buffer1
    } else if (data instanceof Type2) {
        // putt data i buffer2

    That is the main goal I wish to achieve using class Throwable. Why ? Just for the sake of it I guess. No good reason I can up with :)

    Please be warned that the following code is just my attempt at solving the question I have posed, and in no way is correct :D It can be compiled though :p

    Here is the code I have written :
    Would really appreciate any tips regarding posting code. For instance a site which can maintain the structure of a text editor.

    EDIT : I found the site which can store your code after googling a little :) Here is the code : http://pastebin.com/VwhgdpBE


    Java Code:
    import java.util.ArrayList;
    
    class Test {
        public static void main(String [] args) {
    	Throw test = new Throw();
    	test.add();
    	    
    	try {
    	    test.fillBuffer1();
    	}
    	catch(Type2 t1) {
    	    
    	    try {
    		test.fillBuffer2();
    	    }	
    	    catch(Type1 t2) {
    		// no need to do anything
    	    } 
    	    
    	}
    	
        }
    }
    
    class Throw {
        public volatile static ArrayList <Type1> Buffer1 = new ArrayList<Type1> ();
        public volatile static ArrayList <Type2> Buffer2 = new ArrayList<Type2> ();
        public volatile static ArrayList <Object> MixedBuffer = new ArrayList<Object>() ;
    	
    
        public void add() {
    	MixedBuffer.add(new Type1("Jason"));
    	MixedBuffer.add(new Type2(96));
    	MixedBuffer.add(new Type1("Joe"));
    	MixedBuffer.add(new Type2(342316));
    	MixedBuffer.add(new Type2(13123));
    	MixedBuffer.add(new Type1("Jim"));
    	MixedBuffer.add(new Type1("Jake"));
    	MixedBuffer.add(new Type1("Jill"));
        }
        
        // throws the wrong type to the correct method which can insert it
        // in the correct buffer. how do I do that ?
        public synchronized void fillBuffer2() throws Type1 {
    	if(MixedBuffer.isEmpty()) {
    	    return;
    	}
    	
    	Buffer2.add((Type2)MixedBuffer.remove(0));
    	fillBuffer2();
        }
        
        // throws the wrong type to the correct method which can insert it
        // in the correct buffer. how do I do that ?
        public synchronized void fillBuffer1() throws Type2 {
    	if(MixedBuffer.isEmpty()) {
    	    return;
    	}
    	
    	Buffer1.add((Type1)MixedBuffer.remove(0));
    	fillBuffer1();
        }
    }
    
    
    class Type1 extends Throwable {
        public String data;
    
        public Type1(String data) {
    	this.data = data;
        }
    }
    class Type2 extends Throwable {
        public int data;
    
         Type2(int data) {
    	this.data = data;
        }
    }
    Moderator Edit: Code tags added
    Last edited by BruteforceFtw; 06-06-2010 at 02:28 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  3. #3
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks a lot :) Apologies for my laziness.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by BruteforceFtw View Post
    I wish to achieve the following code : [snip]
    Think of it this way: if a Type1 and a Type2 know so well in what buffer/array or whatever they belong why not let them add themselves to the correct collection? There is no need to throw and catch (expensive) exceptions for that.

    kind regards,

    Jos

  5. #5
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    @JosAH

    Hi! I am not catching an exception, but the data :D since the data it self extends Throwable.

    Obviously this is a silly thing to do :) Simply want to be able to implement for the sake of implementing it.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    The thing is you're trying to implement something that no one would bother implementing, for the reason Jos says. The first thing that came to mind as soon as I saw "if..instanceof..." was that you need to write a couple of related classes, implementing a common interface...as Jos implies.

    You don't use the Exception mechanism for that sort of thing.

  7. #7
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    @Tolls

    Hi! I am fully aware over the ludicrous implementation I have displayed. However I am simply interested in implementing it properly regardless of the convention. It is perhaps not that obvious, but I am simply trying to make this work with the full knowledge of its unpractical approach.

    If I wanted to do things right, I would simply do something as what has been suggested :

    Java Code:
    interface ForType1 { ... }
    interface ForType2 { ... }
    class Type1<T implements ForType1> {
        T data;
    }
    class Type2<T implements ForType2> {
        T data;
    }
    This would be one way of making sure that if data was to be saved in a buffer, data would implement a certain "type".

  8. #8
    BruteforceFtw is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Question

    Hi!

    Well it seems that my code may be setting people off :D

    How about this : Could you guys please give me a suggestion how I can create a class which handles throwable data, not exceptions, for storing data into their respective buffers. The task is to take data from a mixed buffer and seperate it by adding the data into the single type buffers.

    If anything here seemed unclear, please do raise an alarm.

    With kind regard,
    BruteForceFTW

Similar Threads

  1. Replies: 4
    Last Post: 06-08-2010, 01:35 AM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. Where is the "Create Data Source Dialog Box"
    By drieskn in forum NetBeans
    Replies: 0
    Last Post: 03-12-2009, 12:46 PM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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