IBM i Access Client Solutions

IBM i Access Client Solutions

Connect, learn, share, and engage with IBM Power.

 View Only
Expand all | Collapse all

How to read integers from a text file into an array and iterate through them using Java classes

  • 1.  How to read integers from a text file into an array and iterate through them using Java classes

    Posted Wed July 03, 2024 02:03 PM
      |   view attached

    Hello,
    I am new to HAScript (complete beginner) and new to the group. I have attempted to create a macro that will read integers (account numbers) from a text file into an array using Java classes. However I am getting errors when I try to loop and add to the array list. It does not accept the "while condition". My ultimate goal is to read the list of account numbers into a variable then iterate through the list and perform actions for each account. Would someone please help me correct my code? Any help is very much appreciated!

    Cal


    <HAScript name="open text file" description=" " timeout="60000" pausetime="300" promptall="true" blockinput="true" author="" creationdate="" supressclearevents="false" usevars="true" ignorepauseforenhancedtn="false" delayifnotenhancedtn="0" ignorepausetimeforenhancedtn="true" continueontimeout="false">
        <import>
            <!-- Importing Necessary Java classes -->
            <type class="java.io.File"/>
            <type class="java.io.FileReader"/>
            <type class="java.io.BufferedReader"/>
            <type class="java.io.IOException"/>
            <type class="java.util.ArrayList"/>
            <type class="java.util.Scanner"/>
        </import>
        <vars>
          <!-- Creating an ArrayList variable named $list$ -->
          <create name="$list$" type="java.util.ArrayList" value="$new java.util.ArrayList()$" />
        </vars>
        <screen name="Screen1" entryscreen="true" exitscreen="true" transient="false">
            <description >
                <!-- OIA settings -->
                <oia status="NOTINHIBITED" optional="false" invertmatch="false" />
            </description>
            <actions>
                <!-- Read integers from the text file into $list$ -->
                <perform value="$new java.io.File(C:\\mypath\\mylist.txt)$"  />
                <perform value="$new java.util.Scanner(file)$"  />
                <perform value="$list.add(java.util.Scannner.Int())$"/>
        <while condition="$scanner.hasNextInt()$">
    <perform value="$list.add(scannner.nextInt())$"/>
    </while>
        <perform value="$scanner.close()$"/>
                
    <!-- Displaying $list$ content (for debugging, remove later) -->
                <message title="" value="$list$" />
            </actions>
            <nextscreens timeout="0" >
            </nextscreens>
        </screen>
    </HAScript>


    ------------------------------
    Cal Moffitt
    ------------------------------

    Attachment(s)

    txt
    post.txt   1 KB 1 version


  • 2.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Mon September 09, 2024 12:28 PM

    Hi @Cal Moffitt did you ever find a solution to this question? I'm currently working on this issue and I have progressed somewhat but, the looping in this HA Script language is difficult to understand.  I have no coding experience and only started playing with Java 2 weeks ago so I understand the Scanner class that is needed in this macro.

    Thanks

    John



    ------------------------------
    John Pimentel
    ------------------------------



  • 3.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Mon September 09, 2024 04:40 PM

    Hi John
    No, I have not found a solution. I got busy at work and haven't looked at it in bit. It's a lower priority project. When I left off, I was just trying to read a file into a variable, but couldn't quite get it to work. Any suggestions?

    Thanks

    Cal



    ------------------------------
    Cal Moffitt
    ------------------------------



  • 4.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Tue September 10, 2024 08:07 AM

    Hi Cal, here is my solution to read text files. I don't have any previous coding experience and could not really figure the looping for HA Script.  My sample is only needed on one screen where it inputs the fields and I accept the changes and it goes back to field one on the screen.  I have not put any of the input actions on this script. This sample will only read the text file one line at a time and i have assigned a variable to each item on each line and then created variables for the remaining lines.

    This script has some short comings and hope someone with more experience can improve on it.  If you try to read a text file with more lines than what the variables are setup for it will not work. In this example I have a text file with 4 lines and 5 fields.  

    55578 TW G745 1125897 15
    66147 XX X778 4169980 48.97
    77799 VU K125 5168727 105-
    81784 HP Q982 6428940 54-

    If you had to read a large file it might not help as you would need to create lots of variables, but it is a start. For my needs I might adjust this script to read maybe 15 lines. It will take the time to setup the initial script, but once it is setup you just run the script. I hope this helps you and others looking for how to read a text file.

    Thanks

    John

    <HAScript name="ReadTextFile" description=" " timeout="60000" pausetime="300" promptall="true" blockinput="true" author="" creationdate="" supressclearevents="false" usevars="true" ignorepauseforenhancedtn="true" delayifnotenhancedtn="0" ignorepausetimeforenhancedtn="true" continueontimeout="true">
    
        <import>
            <type class="java.io.File" name="javaFile"/>
            <type class="java.util.Scanner" name="javaScanner"/>
        </import>
    
        <vars>
          <create name="$fileName$" type="javaFile" value="$new javaFile('C:\\Users\\MyName\\Documents\\share1.txt')$" />
          <create name="$lineScanner$" type="javaScanner" value="$new javaScanner($fileName$)$" />
          <create name="$lineScanner1$" type="string" value="$lineScanner.hasNextLine()$" />
          <create name="$A1$" type="string" value="$lineScanner.next()$" />
          <create name="$B1$" type="string" value="$lineScanner.next()$" />      
          <create name="$C1$" type="string" value="$lineScanner.next()$" />
          <create name="$D1$" type="string" value="$lineScanner.next()$" />      
          <create name="$E1$" type="string" value="$lineScanner.next()$" />    
          <create name="$A2$" type="string" value="$lineScanner.next()$" />
          <create name="$B2$" type="string" value="$lineScanner.next()$" />      
          <create name="$C2$" type="string" value="$lineScanner.next()$" />
          <create name="$D2$" type="string" value="$lineScanner.next()$" />      
          <create name="$E2$" type="string" value="$lineScanner.next()$" />
          <create name="$A3$" type="string" value="$lineScanner.next()$" />
          <create name="$B3$" type="string" value="$lineScanner.next()$" />      
          <create name="$C3$" type="string" value="$lineScanner.next()$" />
          <create name="$D3$" type="string" value="$lineScanner.next()$" />      
          <create name="$E3$" type="string" value="$lineScanner.next()$" />
          <create name="$A4$" type="string" value="$lineScanner.next()$" />
          <create name="$B4$" type="string" value="$lineScanner.next()$" />      
          <create name="$C4$" type="string" value="$lineScanner.next()$" />
          <create name="$D4$" type="string" value="$lineScanner.next()$" />      
          <create name="$E4$" type="string" value="$lineScanner.next()$" />
        </vars>
    
        <screen name="Screen1" entryscreen="true" exitscreen="true" transient="false">
            <description >
                <oia status="NOTINHIBITED" optional="false" invertmatch="false" />
            </description>
            <actions>           
                <message title="&apos;RESULTS1&apos;" value="&apos;A1: &apos;+$A1$+&apos; B1: &apos;+$B1$+&apos; C1: &apos;+$C1$+&apos; D1: &apos;+$D1$+&apos; E1: &apos;+$E1$" />
                <message title="&apos;RESULTS2&apos;" value="&apos;A2: &apos;+$A2$+&apos; B2: &apos;+$B2$+&apos; C2: &apos;+$C2$+&apos; D2: &apos;+$D2$+&apos; E2: &apos;+$E2$" />
                <message title="&apos;RESULTS3&apos;" value="&apos;A3: &apos;+$A3$+&apos; B3: &apos;+$B3$+&apos; C3: &apos;+$C3$+&apos; D3: &apos;+$D3$+&apos; E3: &apos;+$E3$" />
                <message title="&apos;RESULTS4&apos;" value="&apos;A4: &apos;+$A4$+&apos; B4: &apos;+$B4$+&apos; C4: &apos;+$C4$+&apos; D4: &apos;+$D4$+&apos; E4: &apos;+$E4$" />
            </actions>
            <nextscreens timeout="0" >
            </nextscreens>
            <recolimit value="10000" />
        </screen>
    </HAScript>
    


    ------------------------------
    John Pimentel
    ------------------------------



  • 5.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Tue September 10, 2024 06:23 PM

    Hi John,

    I an new to HAScript and Java. My background is in SQL, so bear with me please.
    I see what you did with the variables to get the scanner to read the file, nice!
    To simplify, I shortened the text file to 1 field (still 4 lines) like so


    55578
    66147
    77799
    81784


    Then updated your script to use only one variable "$myString$"  to return each line by using the <varupdate> action. Unfortunately, it still does not "loop" thru the file as long as the next line is not null.

    Here is the script:

    <HAScript name="ReadTextFile_Ver2" description=" " timeout="60000" pausetime="300" promptall="true" blockinput="true" author="" creationdate="" supressclearevents="false" usevars="true" ignorepauseforenhancedtn="true" delayifnotenhancedtn="0" ignorepausetimeforenhancedtn="true" continueontimeout="true">

        <import>
            <type class="java.io.File" name="javaFile"/>
            <type class="java.util.Scanner" name="javaScanner"/>
        </import>

        <vars>
          <create name="$fileName$" type="javaFile" value="$new javaFile('C:\\Users\\MyName\\Documents\\share1.txt')$" />
          <create name="$lineScanner$" type="javaScanner" value="$new javaScanner($fileName$)$" />
          <create name="$lineScanner1$" type="string" value="$lineScanner.hasNextLine()$" />
          <create name="$myString$" type="string" value="$lineScanner.next()$" />
        </vars>


        <screen name="Screen1" entryscreen="true" exitscreen="true" transient="false">
            <description >
                <oia status="NOTINHIBITED" optional="false" invertmatch="false" />
            </description>
            <actions>
                <message title="&apos;RESULTS1&apos;" value="&apos;A1: &apos;+$myString$" />
                <varupdate name="$myString$" value="$lineScanner.next()$" />
                <message title="&apos;RESULTS2&apos;" value="&apos;A2: &apos;+$myString$" />
                <varupdate name="$myString$" value="$lineScanner.next()$" />
                <message title="&apos;RESULTS3&apos;" value="&apos;A3: &apos;+$myString$" />
                <varupdate name="$myString$" value="$lineScanner.next()$" />
                <message title="&apos;RESULTS4&apos;" value="&apos;A4: &apos;+$myString$" />
            </actions>
            <nextscreens timeout="0" >
            </nextscreens>
            <recolimit value="10000" />
        </screen>

    </HAScript>

    l see in the Host On-Demand Macro Programming Guide there is a "condition action" with <if> and <else> elements. I'm hoping maybe it can be used to write an "if then else" statement to do the loop. I'll give it a try as soon as I can. Hope this helps!

    Thank you for your assistance!



    ------------------------------
    Cal Moffitt
    ------------------------------



  • 6.  RE: How to read integers from a text file into an array and iterate through them using Java classes
    Best Answer

    Posted Wed September 11, 2024 10:09 AM

    Hi Cal, thanks for the suggestion to use the varupdate option as this makes it cleaner than having to create new variables for each line.

    I have figured out the looping issue. I created a fictional Screen2 and put the varupdate in the Screen2 and then once screen 2 is done I put in the code that the next screen should be Screen 1. Thereby forcing the loop and once the records are done you get an error message and the macro stops.  Which is perfect for me as all the records have been entered into the system. 

    The final code will much smaller than what I had originally created.  Thanks again for your input.  I have included the updated script.

    I guess next will be to see if I can get the Apache POI to work with Excel.  Not sure how to get that started. 

    <HAScript name="ReadTextFile_Ver3" description=" " timeout="60000" pausetime="300" promptall="true" blockinput="true" author="" creationdate="" supressclearevents="false" usevars="true" ignorepauseforenhancedtn="true" delayifnotenhancedtn="0" ignorepausetimeforenhancedtn="true" continueontimeout="true">
     
        <import>
            <type class="java.io.File" name="javaFile"/>
            <type class="java.util.Scanner" name="javaScanner"/>
        </import>
     
        <vars>
          <create name="$fileName$" type="javaFile" value="$new javaFile('C:\\Users\\MyName\\Documents\\share1A.txt')$" />
          <create name="$lineScanner$" type="javaScanner" value="$new javaScanner($fileName$)$" />
          <create name="$lineScanner1$" type="string" value="$lineScanner.hasNextLine()$" />
          <create name="$myString$" type="string" value="$lineScanner.next()$" />
        </vars>
     
     
        <screen name="Screen1" entryscreen="true" exitscreen="false" transient="false">
            <description >
                <oia status="NOTINHIBITED" optional="false" invertmatch="false" />
            </description>
            <actions>
                <message title="&apos;RESULTS1&apos;" value="&apos;A1: &apos;+$myString$" />
            </actions>
            <nextscreens timeout="0" >
                <nextscreen name="Screen2" />
            </nextscreens>
            <recolimit value="10000" />
        </screen>
     
        <screen name="Screen2" entryscreen="true" exitscreen="false" transient="false">
            <description >
                <oia status="NOTINHIBITED" optional="false" invertmatch="false" />
           
            </description>
            <actions>
                <varupdate name="$myString$" value="$lineScanner.next()$" />
            </actions>
            <nextscreens timeout="0" >
                <nextscreen name="Screen1" />
            </nextscreens>
        </screen>
     
    </HAScript>

    Thanks

    John



    ------------------------------
    John Pimentel
    ------------------------------



  • 7.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Wed September 11, 2024 07:51 PM

    Hi John,
    Thanks for figuring out how to make it loop. So simple, I was way over thinking it. Yes, I would like to get the Apache POI working with Excel also, I'm still reading about it on their website.



    ------------------------------
    Cal Moffitt
    ------------------------------



  • 8.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Wed September 11, 2024 08:02 PM

    @Greg Cornett I think you might be interested in this.



    ------------------------------
    Cal Moffitt
    ------------------------------



  • 9.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Thu September 12, 2024 08:50 AM
      |   view attached

    Thanks, Cal!  I've been watching the thread.  I've also experienced the frustration of not being able to do a loop, so I like where you both are going with this.

    In the meantime, I did receive a message back from Juan Manuel Alcudia Peñas!  He gave me the English version of his CEC 2022 presentation, which I have attached. The last 15 or so pages give instructions on working with Excel files, so maybe you will find this useful.  I just haven't had the opportunity to dig into this yet.



    ------------------------------
    Greg Cornett
    ------------------------------

    Attachment(s)

    pdf
    IBM i ACS Macros.pdf   1.20 MB 1 version


  • 10.  RE: How to read integers from a text file into an array and iterate through them using Java classes

    Posted Thu September 12, 2024 01:47 PM

    Hi Greg, thanks for English PDF.  Since Apache POI is an add on that you need to download to get it to work is not going to work for me.  Work environment prevents any external downloads.  Hope others are able to get it up and running.  

    Thanks 

    John 



    ------------------------------
    John Pimentel
    ------------------------------