Results 1 to 5 of 5
  1. #1
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default VeryLong Integer.. help

    Hi guys,

    please, help me))) :confused:

    I am trying to create a VEreyLong Integer class.. I am using ArrayList..

    but I have some questions..
    this is my code (just the beginning)

    Java Code:
    package VeryLong;
    import java.util.*;
    public class VeryLong{
    	public VeryLong(ArrayList<Integer> a){
    		ArrayList<Integer> verylong = new ArrayList<Integer>();
    		System.out.println("number is created");
    		
    	};
    	public ArrayList<Integer> Readln(){
    		
    		int i=0, tempint=0;
    		ArrayList<Integer> verylong = new ArrayList<Integer>();
    		
    		
    		Scanner in = new Scanner(System.in);
    		String str;
    		str=in.next();
    		while (Character.isDigit(str.charAt(i)))
    		   {
    		   	tempint = Integer.parseInt( Character.toString( str.charAt(i) ) );
    			verylong.add(tempint);
    			i++; 	
    		   };	
    		
    			
    		return verylong;
    	}
    }
    so, in readln method, I am trying to read a string from console, then write each char (which is digit) to ArrayList, then I have to claim that that created ArrayList is my class VeryLong integer, right? But in this case, ArrayList functions will not be available for me.. So do I need to make my class VeryLong smth like
    VeryLong extends ArrayList???? But in this case, how do I block other ArrayList methods, which I don't need..
    I know, that it sounds really confusing.. but please, help me!!!))) :confused: :confused: :confused:

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I think what you want to do is make the ArrayList a field in your class.

    Java Code:
    package VeryLong;
    import java.util.*;
    public class VeryLong{
    
            private ArrayList<Integer> longInt;
    	public VeryLong(ArrayList<Integer> a){
    		this.longInt = a;
    		System.out.println("number is created");
    		
    	};
    	public ArrayList<Integer> Readln(){
    		
    		int i=0, tempint=0;
    		Scanner in = new Scanner(System.in);
    		String str;
    		str=in.next();
    		while (Character.isDigit(str.charAt(i)))
    		   {
    		   	tempint = Integer.parseInt( Character.toString( str.charAt(i) ) );
    			this.longInt.add(tempint);
    			i++; 	
    		   };	
    		
    			
    		return this.longInt;
    	}
    }
    this way the array just lives in the class. It is private so other classes cannot call ArrayList methods on it directly. If you need use of any of the ArrayLists just set up a public method in your class that calls them.

  3. #3
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    em.. So, in that way, longInt will be my very long integer itself.. right? Thank you so much.! I still don't understand it completely, but I think I am on the right way.)))

  4. #4
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    I think what you want to do is make the ArrayList a field in your class.

    Java Code:
    package VeryLong;
    import java.util.*;
    public class VeryLong{
    
            private ArrayList<Integer> longInt;
    	public VeryLong(ArrayList<Integer> a){
    		this.longInt = a;
    		System.out.println("number is created");
    		
    	};
    	public ArrayList<Integer> Readln(){
    		
    		int i=0, tempint=0;
    		Scanner in = new Scanner(System.in);
    		String str;
    		str=in.next();
    		while (Character.isDigit(str.charAt(i)))
    		   {
    		   	tempint = Integer.parseInt( Character.toString( str.charAt(i) ) );
    			this.longInt.add(tempint);
    			i++; 	
    		   };	
    		
    			
    		return this.longInt;
    	}
    }
    this way the array just lives in the class. It is private so other classes cannot call ArrayList methods on it directly. If you need use of any of the ArrayLists just set up a public method in your class that calls them.
    I am sorry for the stupid questions, but how can I test it?
    This is what I have:
    Java Code:
    import VeryLong.VeryLong;
    import java.util.*;
    
    class main{
    	public static void main(String[] args){
    		VeryLong.Readln();
    
    	}
    
    }
    :mad: :mad: :mad: but I cannot do this, because Readln is non-static, but I also cannot call constructor,because I need to read inforamtion from the console first.. any ideas?

    Thanks a lot!!!

  5. #5
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    You need to create a VeryLong object.

    Java Code:
    VeryLong vl = new VeryLong(**Some array list here**);
    You'll probably want to add another constructor to the very long class that looks like this.

    Java Code:
    pucbli VeryLong(){
          this.longInt = new ArrayList<Integer>();
    }
    This will create an empty VeryLong instead of copying a passed ArrayList. To use this constructor:

    Java Code:
    VeryLong vl = new VeryLong();
    After the object is created, you can call it methods.

    Java Code:
    vl.Readln()  //by the way, it is a good idea to start your method names with lower case letters

Similar Threads

  1. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 06:39 PM
  2. Replies: 2
    Last Post: 12-12-2007, 05:01 PM
  3. Short/Integer
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-06-2007, 09:28 PM
  4. Integer vs int
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-14-2007, 09:13 PM
  5. Help with Integer in java
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-14-2007, 05:25 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
  •