How do I insert an object in an array?

How can I insert an object in an array of variable size?
I input two numbers from keyboard
1
2that form the following object (Pair)
(1,2)
how can I add such objects to an array
private Pair s[];
e.g.
public PairList(int x){
top = -1;
s = new Pair[x];
}//constructor
public void getPairs()throws FullException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try{
a=input.readLine();b=input.readLine();
}//try
catch(IOException e){System.out.println("Can't read input");}//catch
int x=0;
int y=0;
try{
x=Integer.parseInt(a);}//try
catch(NumberFormatException e){}
try{
y=Integer.parseInt(b);}//try
catch(NumberFormatException e){}
Pair k=new Pair(x,y);
System.out.println(k);
s[++top]=k; --here is my problem
}//getPairs

I tried making as few changes to your code as possible, but your idea should basically work. Potential problems:
1. Doesn't allow for more input than 1 pair.
2. By storing pair in an array, you must know the exact size of the array you will need beforehand. You might be better off storing the Pair(s) in a Vector and then converting that to an array.
3.Your exception handlers need some work. Basically, your exception handlers put out a message or do nothing and then you continue processing. Not good.
4. No cleanup of file resources after you have completed
import java.io.*;
public class PairList{
     private Pair s[];
     private int top;
     public PairList(int x){
          top = -1;
          s = new Pair[x];
     }//constructor
     public void getPairs()throws Exception{
          BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
          String a = null, b = null;
          try{
               a=input.readLine();
               b=input.readLine();
          }//try
          catch(IOException e){
               System.out.println("Can't read input");
          }//catch
          int x=0;
          int y=0;
          try{
               x=Integer.parseInt(a);
          }//try
          catch(NumberFormatException e){
          try{
               y=Integer.parseInt(b);}//try
          catch(NumberFormatException e){
          Pair k=new Pair(x,y);
          System.out.println(k.toString());
          s[++top]=k; //here is my problem
     }//getPairs
class Pair {
     private int xx;
     private int yy;
     public Pair(int x, int y) {
          xx = x;
          yy = y;
     public String toString() {
          return("X = " + xx + "  Y = " + yy);
}

Similar Messages

  • How do I convert an Object into an Array?

    If I have an instance of type object that I know is really a DataGrid row that was cast into an object when passed to a function, how can I cast that Object to an array, such that each index of this array would correspond to the information in a given column of the row?

    @Sathyamoorthi
    Can you please explain what this function does? Essentially, what should be contained in the String?
    I printed out the string and it seems like I am getting a random row in the Object, as opposed to first or last. Why is that?
    UPDATE: I printed out the value of dataGrid0.columns[5].dataField but it just turned out to be the id of the column.

  • How to convert a Image object to byte Array?

    Who can tell me, how to convert Image object to byte Array?
    Example:
    public byte[] convertImage(Image img) {
    byte[] b = new byte...........
    return b;

    Hello,
    any way would suit me, I just need a way to convert Image or BufferedImage to a byte[], so that I can save them. Actually I need to save both jpg and gif files without using any 3rd party classes. Please help me out.
    thanx and best wishes

  • Insert multiple objects into variable (array)

    Hello,
     I need to create PS script to which gets logged on users(get-rdusersession | select UserName), puts them into variable $users=@(user1,user2,...,userN) and runs foreach {something} - something like send message and logoff user. But I cannot get the
    output from command get-rdusersession to the variable. Can someone tell, how to do that? Thank you.
    Pete
    sfs

    Hi Pete,
    try this:
    $Users = Get-RDUserSession | Select -ExpandProperty UserName
    The "-ExpandProperty" Parameter means the select command returns a String with the value that the UserName property on the output of Get-RDUserSession had. Otherwise you get a PSObject that has a UserName property itself, containing that string
    (try Piping both versions to the cmdlet Get-Member and see for yourself, both properties and type name).
    Cheers,
    Fred
    Ps.: Try running Get-RDUserSession in your Powershell Console and check whether you actually get any output, in case that's the problem.
    There's no place like 127.0.0.1

  • How to insert an object into an object array

    I have an array of objects and im trying to insert an object into the array at position zero. Does anyone know what method I can use for this?

    Hey,
    Are u using a normal array or an array list ?
    For instance if you are using a normal array and you wnat to store an object of classX it should be something like this:
      myArray[0] = new classX(vlaue1, Value2, Value3);this will only work if you have a default constructer for the objects.
    I am posting under correction here but this is the basic idea.
    Hope it helps :-)

  • How can I insert values from table object into a regular table

    I have a table named "ITEM", an object "T_ITEM_OBJ", a table object "ITEM_TBL" and a stored procedure as below.
    CREATE TABLE ITEM
    ITEMID VARCHAR2(10) NOT NULL,
    PRODUCTID VARCHAR2(10) NOT NULL,
    LISTPRICE NUMBER(10,2),
    UNITCOST NUMBER(10,2),
    SUPPLIER INTEGER,
    STATUS VARCHAR2(2),
    ATTR1 VARCHAR2(80),
    ATTR2 VARCHAR2(80),
    ATTR3 VARCHAR2(80),
    ATTR4 VARCHAR2(80),
    ATTR5 VARCHAR2(80)
    TYPE T_ITEM_OBJ AS OBJECT
    ITEMID VARCHAR2(10),
    PRODUCTID VARCHAR2(10),
    LISTPRICE NUMBER(10,2),
    UNITCOST NUMBER(10,2),
    SUPPLIER INTEGER,
    STATUS VARCHAR2(2),
    ATTR1 VARCHAR2(80),
    ATTR2 VARCHAR2(80),
    ATTR3 VARCHAR2(80),
    ATTR4 VARCHAR2(80),
    ATTR5 VARCHAR2(80)
    TYPE ITEM_TBL AS TABLE OF T_ITEM_OBJ;
    PROCEDURE InsertItemByObj(p_item_tbl IN ITEM_TBL, p_Count OUT PLS_INTEGER);
    When I pass values from my java code through JDBC to this store procedure, how can I insert values from the "p_item_tbl" table object into ITEM table?
    In the stored procedure, I wrote the code as below but it doesn't work at all even I can see values if I use something like p_item_tbl(1).itemid. How can I fix the problem?
    INSERT INTO ITEM
    ITEMID,
    PRODUCTID,
    LISTPRICE,
    UNITCOST,
    STATUS,
    SUPPLIER,
    ATTR1
    ) SELECT ITEMID, PRODUCTID, LISTPRICE,
    UNITCOST, STATUS, SUPPLIER, ATTR1
    FROM TABLE( CAST(p_item_tbl AS ITEM_TBL) ) it
    WHERE it.ITEMID != NULL;
    COMMIT;
    Also, how can I count the number of objects in the table object p_item_tbl? and how can I use whole-loop or for-loop to retrieve values from the table object?
    Thanks.

    Sigh. I answered this in your other How can I convert table object into table record format?.
    Please do not open multiple threads. It just confuses people and makes the trreads hard to follow. Also, please remember we are not Oracle employees, we are all volunteers here. We answer questions if we can, when we can. There is no SLA so please be patient.
    Thank you for your future co-operation.
    Cheers, APC

  • How to insert Serialised Object(XML DOM) into Oracle Table(as BLOB or CLOB)

    we need a urgent help. How can we insert and retrieve the XML Document DOM Object into Oracle Table.Actually we used BLOB for insert the DOM Object,its inserted finely but we have a problem in retrieving that object, we got error when v're retrieving. so could you anyone tell us what's this exact problem and how can we reslove this problem If anyone knows or used this kind operation, pls let us know immediately.
    Thanks in advance.

    Please repost your question in the appropriate XML forum, http://forums.oracle.com/forums/index.jsp?cat=51

  • How To Insert A object into Conext

    Hi Techies,
    I have 8 tables in my designer
    and I have two contexts at designer level
    And how to insert a object into a context that is not referencing any table.
    For Example I have an object like "rownum" (Which should must be added at Universe level only not at report level). Here this object is not referencing any table and I want this to be used in a query where the remaining objects are coming from the context..
    Thanks in Advance..

          Double-click the Objects and click the TABLES button and select the table you want to associate the object to for the context to work properly (see below).
    Regards,
    Ajay

  • Any ideas how I can insert a pdf into word, using the insert object option. However the pdf i want to insert has text and lines annotated, but once inserted the comments don't appear????  any help would be greatly appreciated.

    Any ideas how I can insert a pdf into word, using the insert object option. However the pdf i want to insert has text and lines annotated, but once inserted the comments don't appear????  any help would be greatly appreciated.

    You will need to find a forum for MS Word since that is the software that you are trying to manipulate in this.  If you think the processing/creation of the PDF plays a role then you should ask in the forum for the software that you are using to create the PDF.
    This forum is for issue regarding downloading and installing Adobe trial products, so in any circumstance, your issue does not fit in this forum.

  • When using Windows MS Word, you can insert an object such as a video into your document - can anyone tell me how to do that with Pages on an iPad please?  Thanks!

    Hi
    When I was a PC user, I could insert an object such as a video into my MS Word document - and it would appear as an icon, which when clicked, would begin to play the video.
    I find that I cannot insert such videos/movies into my Pages document on my iPad, but maybe I am missing something, being new to the Apple way of doing things?:)  Would appreciate any help, thanks!

    "maybe I am missing something"
    Maybe you are.
    This forum/community is named "AppleWorks" because it is intended to discuss issues and techniques involving the now discontinued Apple productivity application AppleWorks.
    Your question concerns Pages, and specifically, Pages for iOS, the version that runs on iPad (and other iOS devices).
    Admittedly, the Pages for iOS community is difficult to locate. It's rolled in with the other iWork applications in a community named iWork for iOS, where you'll find several people willing and able to answer your question. The link will take you there.
    Regards,
    Barry

  • How to insert Transition object into html document

    Hello Friends,
    I am trying to insert Transition object ( downloaded from
    Extension) into HTML document.
    I tried Insert -> HTML -> Head Tags -> Transitions,
    but after doing all this I am not able to insert
    this extension in my document.
    As I am beginner to Dreamweaver, Can anyone pls. help me out?
    I am using Dreamweaver 8.
    Do I need to select <img> tag before inserting
    Transition Object?
    Pls. pls. help me out.....
    Waiting for reply.
    Thanks in advance for your help.

    When I used to do tabs I would set up a page that was as wide as the tab sheet including the tab, then add text frames in each tab position.

  • How to create Insert & Update on master-detail form JPA/EJB 3.0

    Is there any demonstration or tips how to Insert record on master-details form for JPA/EJB 3.0 with ADF binding?

    I have master-detail forms (dept-emp). I drag the dept->operations->create method to JSF page. But when I click create button, only dept form is clear and ready for insert. But emp form is not clear. How can I add create method for this?
    Can you give some example how to pass the right object to the persist or merge method so that it can save both the two objects (master-detail tables)
    Thanks
    Edited by: user560557 on Oct 9, 2009 8:58 AM

  • How to divide big serialized object in parts to be able to save as Blob typ

    Hi,
    Actually i have serialize a big XML document object and want to save in a field of type Blob. As Blob size is 64k. So if the serialized object is greater than 64k. It gives me error of size.Can anybody tell how to break large serialized objects.
    I am using this code
    File f = new File("out.ser");
    FileInputStream fin = new FileInputStream(f);
    p = farCon.prepareStatement("insert into TeeColor values('3',?)");
    p.setBinaryStream(1, fin, f.length());
    p.executeUpdate();
    This code works fine if the size of out.ser file is less than 64k. But does not work for bigger sizes
    Please help me out
    Thanks

    Read your data from the file into a byte array, then extract 64K at a time into a smaller byte array, and use a ByteArrayInputStream for updating the database.

  • How can I insert a code to switch between channels?

    I’ve got a HP4263A LCR meter which is being used with an ER-18 device to get multiple channel data acquisition, when I go to getting started to trigger the device, but this VI do not have an icon to change the channel how can I insert a code to switch between channels?

    I am just trying to use the drivers developed by NI for this device, but I don't know how to work on them to obtain data from three points, or three channels which I am using from an ER-16 device. So all what I have is the driver VI for the HP4263A.the only VI I can use is getting started, because whe I try to run any other one, this message appears:
    Error -1073807346 occurred at VISA Write in HP4263A Self-Test.vi.
    Possible reasons:
    VISA: (Hex 0xBFFF000E) The given session or object reference is invalid.
    your help would be very helpful
    thanks

  • How to access "Insert Bar" by keyboard in Dreamweaver CS5.5

    Hi All,
    Now I need to support Section 508 compliance, but I don't find how to access "Insert Bar" by keyboard.
    For Examlpe: The focus be in "INSERT", but it doesn't move to other operations(red color) by keyboard. 
    Could you help me to resolve the problem or give any information?
    Thanks,
    Dennis

    Hy Dennis, here are some shortcuts for DW
    Keyboard Commands
    PC Shortcut
    Mac Shortcut
    Create a new document
    Control+N
    Command+N
    Open an existing document
    Control+O
    Command+O
    Save an open document
    Control+S
    Command+S
    Close an open document
    Control+W
    Command+W
    Close all open documents
    Control+Shift+W
    Command+Shift +W
    Exit/Quit Dreamweaver
    Control+Q or Alt+F4
    Command+Q or Opt+F4
    Undo
    Control+Z or Alt+Backspace
    Option+Delete
    Redo
    Control+Y or
    Control +Shift+Z
    Command+Y or Command+Shift+Z
    Cut
    Control+X or
    Shift+Delete
    Command+X or
    Shift+Delete
    Copy
    Control+C
    Command+C
    Paste
    Control+V
    Command+V
    Paste special
    Control+Shift+V
    Command+Shift+V
    Select all
    Control+A
    Command+A
    Find and replace
    Control+F
    Command+F
    Open the Preferences panel
    Control+U
    Command+U
    Show/hide rulers
    Control+Alt+R
    Command+Option+R
    Show/hide guides
    Control+;
    Command+;
    Show/hide visual aids
    Control+Shift+I
    Command+Shift+I
    Show/hide grid
    Control+Alt+G
    Command+Option+G
    Edit page properties
    Control+J
    Command+J
    Refresh Design view
    F5
    F5
    Make selected text bold
    Control+B
    Command+B
    Make selected text italic
    Control+I
    Command+I
    Apply paragraph formatting to selected text
    Control+Shift+P
    Command+Shift+P
    Apply heading formatting (H1–H6) to selected text
    Control+1 through 6
    Command+1 through 6
    Add new paragraph
    Return
    Return
    Add a line break <BR>
    Shift+Return
    Shift+Return
    Insert a nonbreaking space
    Command+Shift+Spacebar
    Command+Shift+Spacebar
    Move object or text
    Drag selection to new location
    Drag selection to new location
    Copy object or text
    Control-drag selection to new location
    Option-drag selection to new location
    Select a word
    Double-click
    Double-click
    Select a row or text block
    Triple-click
    Triple-click
    Insert image
    Control+Alt+I
    Command+Option+I
    Insert table
    Control+Alt+T
    Command+Option+T
    Run a spell check
    Shift+F7
    Shift+F7
    Open the Help window
    F1
    F1
    Zoom in
    Control+=
    Command+=
    Zoom out
    Control+-
    Command+-
    Preview in primary browser
    F12
    Option+F12
    Preview in secondary browser
    Shift+F12 or Control+F12
    Command+F12
    Live View
    Alt+F11
    Option+F11
    Freeze JavaScript
    F11
    F11
    Inspect
    Alt+Shift+F11
    Option+Shift+F11
    Code Navigator
    Control+Alt+N
    Command+Option+N
    Get
    Control+Shift+D
    Command+Shift+D
    Put
    Control+Shift+U
    Command+Shift+U
    Check in
    Control+Alt+Shift+U
    Command+ Option+Shift+U
    Check out
    Control+Alt+Shift+D
    Command+ Option+Shift+D
    Hope it helps
    brezplačna izdelava spletnih strani

Maybe you are looking for

  • Help with opening a fb file in Adobe reader

    HI i am trying to open a PDF file from facebook in adobe  reader. I had done this many times on my old iPhone 5 (I'm now using iOS 8 on a iphone 6plus) it does not give me the option to do this jiwrver . There no box with the arrow to select open in.

  • The last iTunes wiped out my audiobooks.  How do I get them back in my library without having to import them again?

    I just went to iTunes and realized that my audiobooks have been wiped out.  I just did an update for iTunes.  The audiobooks were the last time I went to look at them, but now they are gone.  I could use some help.

  • 5530 - some incoming ringtones don't work but othe...

    Hi, my wife's just bought a 5530 and is pretty impressed... but... Having set up incoming ringtones for the rest of the family, our home phone doesn't trigger the correct ringtone (and the incoming call message just has the number, not the name).  Ca

  • SUBMIT report using selection-table issue

    Hi Experts, Actually I am calling report2 from report1. In report1 i have  select-options field WERKS. In report2 I have parameters field WERKS. Please let m know how to call report2 using SUBMIT button selection-table concept. How to insert records

  • 3G is blazing for me!

    Just took the phone off of Wifi and tried to surf some webs on 3G after driving away from the house... Super fast! God I love this phone!