Advertisement
savemefromcoding

Untitled

Sep 22nd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Scanner;
  6. import java.util.Map.Entry;
  7.  
  8. import sba.Item;
  9.  
  10. public class PleaseWork {
  11.  
  12.     public static void main(String[] args) throws FileNotFoundException {
  13.         Item myItem = new Item();
  14.         String itemName;
  15.         String itemDescription;
  16.         double itemPrice;
  17.         int quantity;
  18.         // TODO Auto-generated method stub
  19.         String line = "";
  20.         File myFile;
  21.         HashMap<String,Item> itemCollection = new HashMap<String,Item>();
  22.  
  23.            
  24.  
  25.                 myFile = new File("sample.txt");
  26.                 Scanner scan = new Scanner(myFile);
  27.                
  28.                 //int lineCount = 0;
  29.                 //while(scan.hasNext()) {  // counts the number of lines in sample.txt
  30.                 //  lineCount++;
  31.                 //}
  32.                 //String[] lines = new String[lineCount];//initializes an array of strings with the length of linecount
  33.                
  34.                 //int i = 0;
  35.                
  36.                
  37.                 //int lineCount = 0;
  38.                 //while(scan.hasNext()) {  // counts the number of lines in sample.txt
  39.                 //  lineCount++;
  40.                 //}
  41.                 //String[] lines = new String[lineCount];//initializes an array of strings with the length of linecount
  42.                
  43.                 //int i = 0;
  44.                
  45.                 while(scan.hasNext()) {//loads each line from samples.txt into a string and places it in the array at position i
  46.                     line = scan.nextLine();
  47.                     String[] lines = line.split("\\s");
  48.                     itemName = lines[0];
  49.                     itemDescription = lines[1];
  50.                     itemPrice = Double.parseDouble(lines[2]);
  51.                     quantity = Integer.parseInt(lines[3]);
  52.                     myItem.setItemName(itemName);
  53.                     myItem.setItemDesc(itemDescription);
  54.                     myItem.setItemPrice(itemPrice);
  55.                     myItem.setQuantity(quantity);
  56.                     itemCollection.put(itemName, myItem);
  57.                    
  58.                    
  59.                     }//end of while
  60.                
  61.                 for (Entry<String, Item> entry : itemCollection.entrySet()) {
  62.                     myItem = entry.getValue();
  63.                     System.out.println(myItem.getItemName());
  64.                     System.out.println(myItem.getItemDesc());
  65.                     System.out.println(myItem.getItemPrice());
  66.                     System.out.println(myItem.getAvailableQuantity());
  67.                 }
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement