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.
Original Message:
Sent: Thu September 12, 2024 08:50 AM
From: Greg Cornett
Subject: How to read integers from a text file into an array and iterate through them using Java classes
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
Original Message:
Sent: Wed September 11, 2024 08:01 PM
From: Cal Moffitt
Subject: How to read integers from a text file into an array and iterate through them using Java classes
@Greg Cornett I think you might be interested in this.
------------------------------
Cal Moffitt
Original Message:
Sent: Wed September 11, 2024 10:08 AM
From: John Pimentel
Subject: How to read integers from a text file into an array and iterate through them using Java classes
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="'RESULTS1'" value="'A1: '+$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
Original Message:
Sent: Tue September 10, 2024 06:22 PM
From: Cal Moffitt
Subject: How to read integers from a text file into an array and iterate through them using Java classes
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="'RESULTS1'" value="'A1: '+$myString$" />
<varupdate name="$myString$" value="$lineScanner.next()$" />
<message title="'RESULTS2'" value="'A2: '+$myString$" />
<varupdate name="$myString$" value="$lineScanner.next()$" />
<message title="'RESULTS3'" value="'A3: '+$myString$" />
<varupdate name="$myString$" value="$lineScanner.next()$" />
<message title="'RESULTS4'" value="'A4: '+$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
Original Message:
Sent: Tue September 10, 2024 08:06 AM
From: John Pimentel
Subject: How to read integers from a text file into an array and iterate through them using Java classes
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="'RESULTS1'" value="'A1: '+$A1$+' B1: '+$B1$+' C1: '+$C1$+' D1: '+$D1$+' E1: '+$E1$" /> <message title="'RESULTS2'" value="'A2: '+$A2$+' B2: '+$B2$+' C2: '+$C2$+' D2: '+$D2$+' E2: '+$E2$" /> <message title="'RESULTS3'" value="'A3: '+$A3$+' B3: '+$B3$+' C3: '+$C3$+' D3: '+$D3$+' E3: '+$E3$" /> <message title="'RESULTS4'" value="'A4: '+$A4$+' B4: '+$B4$+' C4: '+$C4$+' D4: '+$D4$+' E4: '+$E4$" /> </actions> <nextscreens timeout="0" > </nextscreens> <recolimit value="10000" /> </screen></HAScript>
------------------------------
John Pimentel
Original Message:
Sent: Mon September 09, 2024 04:39 PM
From: Cal Moffitt
Subject: How to read integers from a text file into an array and iterate through them using Java classes
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
Original Message:
Sent: Fri September 06, 2024 07:04 PM
From: John Pimentel
Subject: How to read integers from a text file into an array and iterate through them using Java classes
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
Original Message:
Sent: Tue July 02, 2024 06:20 PM
From: Cal Moffitt
Subject: How to read integers from a text file into an array and iterate through them using Java classes
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
------------------------------