0

How to make a while loop with properties I been 2 days playing with Iterator, toString parse and a lot of another forms here is the format I want to display. Work perfect only for one set of properties so only display the last items from the query.

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    String line;
    while((line=in.readLine())!=null){
    System.out.println(line);
    }

Display 2 sets in a bad format

Now this is the perfect format but only print the last item:

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    String line;
   Properties prop= new Properties();
   while((line=in.readLine())!=null){
  prop.load(in);
   String amount = prop.getProperty("ssl_amount");
    String card = prop.getProperty("ssl_card_number");
    String approval = prop.getProperty("ssl_approval_code");
    String results = prop.getProperty("ssl_result_message");
    String time = prop.getProperty("ssl_txn_time");
    System.out.println(amount+" "+card+" "+approval+" "+results+" "+time+" "); 
  }

How I can make a loop to print all the entry's in the correct format? This is the Raw Input stream:

ssl_txn_count=2
ssl_txn_id=071214A15-B805A410-E9F4-4D6D-AA87-0E9923FBC7FA
ssl_user_id=webpage
ssl_trans_status=STL
ssl_card_type=CREDITCARD
ssl_transaction_type=SALE
ssl_txn_time=12/07/2014 11:28:00 PM
ssl_first_name=
ssl_last_name=
ssl_card_number=41**********1111
ssl_exp_date=1115
ssl_entry_mode=K
ssl_avs_response= 
ssl_cvv2_response=P
ssl_amount=10.00
ssl_invoice_number=
ssl_result_message=APPROVAL
ssl_approval_code=CVI064
ssl_txn_id=061214A15-8921B6B0-FF9E-4DA5-97D1-288C28272B10
ssl_user_id=webpage
ssl_trans_status=STL
ssl_card_type=CREDITCARD
ssl_transaction_type=SALE
ssl_txn_time=12/06/2014 01:25:18 AM
ssl_first_name=
ssl_last_name=
ssl_card_number=41**********1111
ssl_exp_date=1215
 ssl_entry_mode=K
 ssl_avs_response= 
 ssl_cvv2_response=P
 ssl_amount=12.00
 ssl_invoice_number=
 ssl_result_message=APPROVAL
 ssl_approval_code=CVI806

And here is the good format I want it : (like you can see only report 1 of them and not 2)

run:
12.00 41**********1111 CVI806 APPROVAL 12/06/2014 01:25:18 AM 
BUILD SUCCESSFUL (total time: 1 second)
passw
  • 1
  • 1

2 Answers2

1

Load the Reader once (outside the loop), and then you can iterate the keySet. Next, you could display the key and value. Using Format String Synatx that might look something like,

try (InputStreamReader is = new InputStreamReader(
        conn.getInputStream(), "UTF-8")) {
    Properties prop = new Properties();
    prop.load(is);
    for (Object key : prop.keySet()) {
        System.out.printf("%s=%s%n", key, prop.get(key));
    }
}

For hard coded keys (your specific case and if you want to ignore other properties), you don't need a loop

try (InputStreamReader is = new InputStreamReader(
        conn.getInputStream(), "UTF-8")) {
    Properties prop = new Properties();
    prop.load(is);
    String amount = prop.getProperty("ssl_amount");
    String card = prop.getProperty("ssl_card_number");
    String approval = prop.getProperty("ssl_approval_code");
    String results = prop.getProperty("ssl_result_message");
    String time = prop.getProperty("ssl_txn_time");
    System.out.println(amount + " " + card + " " + approval + " "
            + results + " " + time + " ");
}

The above are using try-with-resources to close the stream(s) when done.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Only came back with 1 respond. I need the loop to keep going printing the transactions. – passw Dec 09 '14 at 06:28
  • That's not how [`Properties`](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html) (or its' superclass [`Hashtable`](http://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html)) works. – Elliott Frisch Dec 09 '14 at 06:35
0

Use Enumuration to iterate property file and here is an example

Properties prop = new Properties();

InputStream input = nnew FileInputStream("<file-location>.properties");
prop.load(input);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        String value = prop.getProperty(key);
        System.out.println("Key : " + key + ", Value : " + value);
    }