Copying Multiple Arrays into One Large Array

I'm currently trying to copy multiple character arrays into one larger character array in a method that prints data gathered from a database. Here is my code:
private static void printData(String[] data){
        try {
            int totalLengthOfAllStrings = 0;
            for(int i = 0; i < data.length; i++) {
                data[i] += '\n';
                totalLengthOfAllStrings += data.length();
int runningLength = 0;
char[] printArray = new char[totalLengthOfAllStrings];
for(int j = 0; j < data.length; j++) {
char[] tempArray = data[j].toCharArray();
runningLength += tempArray.length;
System.arraycopy(tempArray, 0, printArray, runningLength - 1, tempArray.length);
DocFlavor df = DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
Doc printDoc = new SimpleDoc(printArray, df, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
aset.add(MediaSize.NA.LETTER);
aset.add(Sides.DUPLEX);
PrintService[] services = PrintServiceLookup.lookupPrintServices(df, aset);
if (services.length > 0) {
DocPrintJob job = services[0].createPrintJob();
try {
job.print(printDoc, aset);
System.out.println("Print successful!");
catch (PrintException pe) {
catch(Exception e) {
e.printStackTrace();
I keep getting an ArrayIndexOutOfBoundsException and for the life of me I can't figure out why.
Can anyone help with this?
Thanks!
-Matt

I think your problem lies with the second for loop:
for(int j = 0; j < data.length; j++)  {
    char[] tempArray = data[j].toCharArray();
    runningLength += tempArray.length;
    System.arraycopy(tempArray, 0, printArray, runningLength - 1, tempArray.length);
}If I am not wrong, you should swap the sequence of the runningLength and System.arraycopy statements, and also move the minus 1 from arraycopy statement to the runningLength statement. The result would look like this:
for(int j = 0; j < data.length; j++)  {
    char[] tempArray = data[j].toCharArray();
    System.arraycopy(tempArray, 0, printArray, runningLength, tempArray.length);    // swapped and removed the -1
    runningLength += tempArray.length - 1;    // swapped and appended the -1
}The main reason for these changes is because when j == data.length - 1, the value of runningLength would have already exceeded the size of printArray. Therefore, swapping the position would be able to overcome this problem.

Similar Messages

  • How do i convert multiple tracks into one large mp3?

    how do i convert multiple tracks into one large mp3?

    Hi,
    You can use an app such as Audacity or Join Together to create one larger file. If AAC file used instead of mp3, you could use GarageBand.
    If you have the original CD, you can join the tracks whilst re importing disc.
    Jim

  • Combining multiple projects into one larger project?

    If I have multiple separate projects and now want to combine them, edits, titles and all into a larger project, is it possible? I expected that it might be possible to drag one project into another and place it intact onto the timeline for the new project. Since every combination of dragging a project around has evidently failed to accomplish what I'm expecting... is there a way to do this?

    welcome phiphika to the  board…
    in terms of convenience, I would indeed export/share all 6 movies.
    to tape, or as Quicktime/FullQuality…
    both methods create a lossless copy of your final movie(s).
    in step II, import all 6 project into a new iM project, as said above, you don't loose ay quality with that back'n forth copying... only some time ;-)… you can set then chapter markers etc....
    besides, your workflow is as mine on larger projects: not 678 clips into one monstrous project of some dozends Gigabytes, just segments…

  • Variable number of two dimensional arrays into one big array

    I have a variable number of two dimensional arrays.
    The first dimension is variable, the second dimension is always 7.
    i.e.:
    Object[][] array0 = {
    {"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"},
    {"alice", "smith", 42, 160.0, "la", "bub", "[email protected]"},
    Object[][] array1 = {
    {"john", "sdfs", 34, 15.5, "a", "bl", "[email protected]"},
    {"joe", "smith", 42, 16.0, "a", "bub", "[email protected]"},
    Object[][] arrayi = ...I'm generating these arrays with a for-loop:
         for (int i = 0; i < filter.length; i++) {
              MyClass c = new MyClass(filter);
              //data = c.getData();
    Where "filter" is another array which is filled with information that tells "MyClass" how to fill the arrays.
    "getData()" gives back one of the i number of arrays.
    Now I just need to have everything in one big two dimensional array.
    i.e.:Object[][] arrayComplete = {
    {"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"},
    {"alice", "smith", 42, 160.0, "la", "bub", "[email protected]"},
    {"john", "sdfs", 34, 15.5, "a", "bl", "[email protected]"},
    {"joe", "smith", 42, 16.0, "a", "bub", "[email protected]"},
    Any idea on how to accomplish this? It's blowing my mind right now.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Just brainstorming here:
    Why not put your actual data in a class and store that in a LinkedList (so you know the total number of elements for your multi-dimensional array). Then initalize your multi-dimensional array and populate it? Haven't tested the following, but thinking something along the lines of
    public class MyData {
         //data here
         public Object[] toArray() {
              //something similar to this
              return new Object[] = {"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"};
    LinkedList<MyData> data = new LinkedList<MyData>();
    data.add(mydata1);
    //however many times you need
    Object[][] arrayComplete = new Object[data.size()][7];
    for(int i = 0; i < data.size(); i++) {
         arrayComplete[i] = data.removeFirst().toArray();
    }Another option for knowing how many rows you would need might be using something like:
    int rows = 0;
    rows += array1.length;
    rows += array2.length;
    //etc...But is not entirely useful if you don't know how many variable arrays you have (although reflections might help if the name of the array variable is incremented systematically, someone told me earlier to avoid reflections since it could make debugging a LOT more difficult).
    Edited by: bogdana on Apr 1, 2010 10:38 AM

  • Is it possible to copy multiple classes into one class?

    Hi there,
    My environment is 2007 R2. We have two class: Class1 and Class2. Both are populated by a script discovery. Now I want to create another class: Class3 which should contain the object of BOTH Class1 and Class2. Of cause I can create another script discovery
    for class3. But since the objects are already discovered in class1 and class2, I just need to copy them into class3, is there any other way? I tried to avoid introducing more script discovery.

    Thanks for the reply. But the group one doesn't work well. The main reason for this is because I want to create ONE SINGLE state view and alert view for my application. There are few classes: web servers, portal servers, db server and etc. Using group works
    on alert view. However state view doesn't.
    Those class are NOT generating from the same seed class. However they all have similar properties: name, role, lob and etc.
    Any suggestion?

  • OS X Maverick Multi Display Support for grouping multiple monitors into one display?

    Will the new Mac Pro be capable of grouping multiple monitors into one large display?  Similar to AMD Eyefinity for the purpose of gaming.

    The current Mac Pro, or any Mac released since 1987 that can support multiple displays can ALREADY support combining all attached displays into one Extended Desktop, onto which you can open the gaming window and game across multiple displays. It is built into the System Software.

  • Combine Columns From Separate Arrays Into One Formatted Table

    What I'm trying to do is make two WMI queries with 2 different classes for a list of machines and then patch the columns together into one single array that is formatted as a table with columns and rows. I seem to keep banging my head against the wall and
    I can't help but feel that the answer is simple. I can certainly create an array that contains all 3 columns (such as in the commented out part) but no matter which angle I go at it, it always seems to end up as all the data in one single row in each column
    rather than a nicely formatted table. I've even tried constructing separate custom objects and adding the different objects to the array but that's obviously not working. Below is the code of the last thing I tried. I need someone to bash it to death and tell
    me the (most likely obvious) thing that I'm doing wrong. Thanks!
    $failedos = @()
    $failedcs = @()
    $ccs = get-adcomputer -property operatingsystem -filter {name -like "*-CC*"} | select name | sort name
    $cs = foreach ($cc in $ccs){$cc.name | % {if ($c=get-wmiobject -computername $cc.name -class win32_computersystem -ErrorAction SilentlyContinue){$c | select @{Name="Name";Expression={$_.Name}}, @{Name="Model";Expression={$_.Model}}} else {$failedcs += "$_"}}}
    $os = foreach ($cc in $ccs){$cc.name | % {if ($o=get-wmiobject -computername $cc.name -class win32_operatingsystem -ErrorAction SilentlyContinue){$o | select @{Name="OperatingSystem";Expression={$_.caption}}} else {$failedos += "$_"}}}
    #[array]$osprops = @{'Name'=$cs.Name;'Model'=$cs.Model;'OperatingSystem'=$os.OperatingSystem}
    $result = @()
    Foreach ($Line in $cs) {
    $MyCustomObject = New-Object -TypeName PSObject
    Add-Member -InputObject $MyCustomObject -MemberType NoteProperty -Name "Name" -Value $Line.name -Force
    Add-Member -InputObject $MyCustomObject -MemberType NoteProperty -Name "Model" -Value $Line.Model -Force
    $result += $MyCustomObject
    foreach ($Line2 in $os) {
    $MyCustomObject2 = New-Object -TypeName PSObject
    Add-Member -InputObject $MyCustomObject2 -MemberType NoteProperty -Name "OperatingSystem" -Value $Line2.OperatingSystem -Force
    $result += $MyCustomObject2

    Any help?
    $ccs = get-adcomputer -property operatingsystem -filter {name -like "*-CC*"} |
    select -ExpandProperty name | sort
    $Result =
    Foreach ($CC in $CCs)
    $Object =
    New-Object PSObject -Property @{ Name = $CC
    Model = 'Failed'
    OperatingSystem = 'Failed'
    Try {
    $Object.Model =
    get-wmiobject -computername $CC -class win32_computersystem -ErrorAction Stop |
    select -ExpandProperty Model
    $Object.OperatingSystem =
    get-wmiobject -computername $CC -class win32_operatingsystem -ErrorAction Stop |
    select -ExpandProperty Caption
    Catch{}
    Finally { $Object }
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • How do I combine two arrays into one?

    I am trying to combine two byte arrays into one. What function do I use that will put in the information in the same order just combined?

    battler. wrote:
    Julien,
    Used LV for years.  Never knew that (concatenate inputs).  Wouldn't believe the lengths ive used to get around it.  Thanks
    Same here! I actually learned about it taking a sample test for the CLAD.
    Richard

  • How do I add multiple images into one file?

    I'm sure this is something that's been covered in another post (or even in the help portal) but I think my wording in my search terms are not correct or... I don't know, because I just can't find what I'm looking for.
    I want to know how to add multiple images into one file/one image, both horizontally and/or vertically. To give you an idea of what I mean, check out :
    http://www.best10apps.com/apps/comic-story,531596060.html
    If you scroll down, you'll see a heading entitled : Screenshots of Comic Story. Notice how there's 3 pictures (divided by borders). 2 of those pictures are side by side, and 1 of them is below the first 2 pictures.
    I want to know how to add different pictures/images and put them into one picture.

    One way is to create template PSD files and populate them with your images using Photoshops scripts.
    Photo Collage Toolkit UPDATED June 12, added Picture Package Support via PasteImageRoll and BatchPicturePackage scripts.
    The package includes four simple rules to follow when making Photo Collage Template PSD files so they will be compatible with my Photoshop scripts.
    There are eleven scripts in this package they provide the following functions:
    TestCollageTemplate.jsx - Used to test a Photo Collage Template while you are making it with Photoshop.
    CollageTemplateBuilder.jsx - Can build Templates compatible with this toolkit's scripts.
    LayerToAlphaChan.jsx - Used to convert a Prototype Image Layer stack into a template document.
    InteractivePopulateCollage.jsx - Used to interactively populate Any Photo Collage template. Offers most user control inserting pictures and text.
    ReplaceCollageImage.jsx - use to replace a populated collage image Smart Object layer with an other image correctly resized and positioned.
    ChangeTextSize.jsx - This script can be used to change Image stamps text size when the size used by the populating did not work well.
    PopulateCollageTemplate.jsx - Used to Automatically populate a Photo Collage template and leave the populated copy open in Photoshop.
    BatchOneImageCollage.jsx - Used to Automatically Batch Populate Collage templates that only have one image inserted. The Collage or Image may be stamped with text.
    BatchMultiImageCollage.jsx - Used to Automatically Batch Populate Any Photo Collage template with images in a source image folder. Easier to use than the interactive script. Saved collages can be tweaked.
    BatchPicturePackage.jsx - Used to Automatically Batch Populate Any Photo Collage template with an image in a source image folder
    PasteImageRoll.jsx - Paste Images into a document to be print on roll paper.
    Documentation and Examples

  • Multiple delivery into One invoice

    Dear All
    Issue regarding multiple delivery into one invoice
    1 When i am creating multiple delivery doc into one invoice, its possible for domestic[local sales] but when i am creating multiple delivery doc into one invoice for export it is not possible.
    I have checked with the threads and tried it out with copy control with VBRK/VBRP [001/003/004]
    Is there any specific customization for exports?
    Regards
    Sandeep Bhowmick

    Dear Lakshmipati
    As per your instruction i have done the split analysis:
    split analysis:
    Split due to different header data
    Field Name                           :$00000001           $00000002
    Number of foreign trade data      : 0000000266         0000000267
    The invoice is generating against one delv document, it could not be combined. In copy control any changes i have to make
    Regards
    Sandeep Bhowmick
    Edited by: Sandeep Bhowmick on Jan 17, 2009 7:33 AM
    Edited by: Sandeep Bhowmick on Jan 17, 2009 7:34 AM

  • IDVD - multiple slideshows into one

    In IDVD how do I make multiple slideshows into one slideshow. I have never used this and I accidentally entered 300 pics as individual slideshows. How would I put all of them into one slideshow? I'm making a dvd for Christmas presents.

    If you have more than 99 slides in a slideshow you will run into a noticable pause at the 99 to 100th slide transition, and at the  198 to 199 slide transition. 
    To make a slideshow with more that 99 slides that will play without the pauses the slides must be joined together in iPhoto for one large slideshow with the transition and background music  of your choice. Then export it out of iPhoto as a Quicktime movie file and addes to the iDVD theme. 
    What version of iPhoto and iDVD are you using?  It matters in how to export the slideshow out of iPhoto.
    Happy Holidays

  • When I go back to edit the form why are my individual text boxes merging into one large text box?

    I am currently trying to create a form fillable pdf and have never had problems until yesterday.
    My individual text boxes are somehow conjoining into one large text box after I save my edits and go back to the form to make additional edits.  This has never happened before and it is messing up the entire format of the form.  Any guidance would be grealy appreciated.
    Thanks!

    Did you find a solution to your problem?  I am experiencing the same thing.  Only, the boxes will change.  One time when I go in a single word on a line will be in the same box as a word above it.  I created these text boxes in the pdf and did not import from another file.  The next time I go into the file, the word will be back in its original box and other words will be combined in boxes. The last time I went in two entire lines that were two separate boxes are not one box.  The boxes seem to merge completely randomly though the text they are enclosing are next to each other either side by side, or more frequently, above/below.  This is quite a nuisance as I am still creating my form and when I want to move words or lines, I have to copy the text box and then delete the words I do not want in each box just so I can manipulate the content into the positions I want them.

  • How do I scan multiple pages into one pdf document on a HP Envy 4501 Printer

    My HP Envy 4501 Printer won't let me scan multiple pages into one pdf document.
    I went to "Advanced Settings", clicked on "file" and made sure that the check box next to "Create a separate file for each scanner page" was unchecked.
    When I clicked the Save button it showed the page that I scanned and showed 1/1.  On the right side it had options for changing the brightness and Contrast, Rotate and Crop and "Back", "Save" and "Done" buttons, but nothing about scanning another page.
    Not to mention HP's Customer Support is useless!
    I'd appreciate any help I can get with this problem.
    Thanks,
    Mark

    Hi BH,
    I see your diagram.  I just scanned something.  The screen that I'm looking at right now shows the preview page and the scroll box to the left of that just like in your diagram.  The screen does not show the + button nor does it show the red x button under the scroll box as in your diagram.  I can't see the plus sign because it is not there.
    I tried re-installing the software and drivers as you suggested but that did not have any affect.
    Thank you for trying to help me with this problem but it hasn't worked, therefore I can't mark the reply "Accept as Solution".
    I've noticed that on HP's support forum web page there is only 1 solved request for assistance.  That doesn't give me much confidence in HP.
    Please reply back to this post to let me know if there is anything else that I can try or if you know someone else at HP that can help.
    If I don't here back from you, I'm returning this HP Envy 4501 Printer to get my money back to use it to buy a printer from one of HP's competitors.
    Thanks,
    Mark  

  • Scanning multiple pages into one document with an automatic document feeder

    when using automatic document feeder to scan in multiple pages - using the Image Capture app, this process does not work for more than 2 pages. However works perfectly in PC environment.
    Using a MacBook Pro with 10.8.4 and a Lexmark Interpret 405S All In One Scanner.
    Is there any other all in one device that can perform this function in a Mac environment?
    Please advise.

    Hi @FentyFly ,
    I see by your post that you would like to know how to scan multiple pages into one file. I would like to help you out today.
    From the HP Scan Software, click on the link for Advanced Settings, then uncheck Create a separate file for each scanned page.
    Here is a URL for how to scan and change the settings. Just select your operating system.
    Scan.
    What operating system are you using? How to Find the Windows Edition and Version on Your Computer.
    If you need further assistance, just let me know.
    Have a great day!
    Thank You.
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos Thumbs Up" on the right to say “Thanks” for helping!
    Gemini02
    I work on behalf of HP

  • How to concatenate multiple records into one

    Hi everybody:
    I want to know if exist some way to concat multiple records into one without using cursors. For example, I have a table named "Authors" like this:
    Lan|Author
    English|Ernest Hemingway
    Spanish|Octavio Paz
    Spanish|Mario Vargas Llosa
    English|Sinclair Lewis
    Spanish|Gabriel García Márquez
    And I want to get this:
    Author
    Octavio Paz, Mario Vargas Llosa, Gabriel García Márquez
    I have worked with SQL Server and I can do something like this:
    CREATE FUNCTION dbo.MyConcat (@lan varchar(10))
    RETURNS varchar(5000) AS
    BEGIN
    declare @retvalue varchar(5000)
    set @retvalue=''
    select @retvalue = @retvalue + Author +',' from Authors where lan = @lan
    return substring(@retvalue,1,len(@retvalue)-1)
    END
    ie, do not use cursors to concatenate records. However, with ORACLE, I have to do someting like this.
    FUNCTION MyConcat(P_Lan IN VARCHAR2) RETURN VARCHAR2 IS
    v_ret VARCHAR2(4000);
    v_element VARCHAR2(4000);
    v_cursor sys_refcursor;
    BEGIN
    OPEN v_cursor FOR SELECT Author FROM Authors where Lan = P_Lan
    LOOP
    FETCH v_cursor INTO v_elemento;
    EXIT WHEN v_cursor%NOTFOUND;
    IF v_ret IS NULL THEN
    v_ret := v_element;
    ELSE
    v_ret := v_ret || ', ' || v_element;
    END IF;
    END LOOP;
    RETURN v_ret;
    END;
    Exist some other way to do this?
    Best Regards
    Jack

    Tks both for answer... I forgot to mention that I am using Oracle 10g. I read about LISTAGG() but this function is available for Oracle 11g release 2.
    I wil read about the other techniques than Hoek mention
    Best Regards.
    Jack

Maybe you are looking for