Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-14-2007, 05:06 AM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
VeryLong Integer.. help
Hi guys,

please, help me)))

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)

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!!!)))
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-14-2007, 05:16 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
I think what you want to do is make the ArrayList a field in your class.

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-14-2007, 05:30 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
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.)))
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-14-2007, 11:28 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Quote:
Originally Posted by ShoeNinja View Post
I think what you want to do is make the ArrayList a field in your class.

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:
Code:
import VeryLong.VeryLong; import java.util.*; class main{ public static void main(String[] args){ VeryLong.Readln(); } }
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!!!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-14-2007, 11:48 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
You need to create a VeryLong object.

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.

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

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

Code:
vl.Readln() //by the way, it is a good idea to start your method names with lower case letters
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Breaking down an integer Emily New To Java 1 03-06-2008 08:39 PM
Create a VeryLong class that will store an integer of arbitrary length. hey New To Java 2 12-12-2007 07:01 PM
Short/Integer mew New To Java 3 12-06-2007 11:28 PM
Integer vs int bugger New To Java 1 11-14-2007 11:13 PM
Help with Integer in java susan New To Java 1 07-14-2007 07:25 AM


All times are GMT +3. The time now is 03:19 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org