Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.Map.Entry;
- import sba.Item;
- public class PleaseWork {
- public static void main(String[] args) throws FileNotFoundException {
- Item myItem = new Item();
- String itemName;
- String itemDescription;
- double itemPrice;
- int quantity;
- // TODO Auto-generated method stub
- String line = "";
- File myFile;
- HashMap<String,Item> itemCollection = new HashMap<String,Item>();
- myFile = new File("sample.txt");
- Scanner scan = new Scanner(myFile);
- //int lineCount = 0;
- //while(scan.hasNext()) { // counts the number of lines in sample.txt
- // lineCount++;
- //}
- //String[] lines = new String[lineCount];//initializes an array of strings with the length of linecount
- //int i = 0;
- //int lineCount = 0;
- //while(scan.hasNext()) { // counts the number of lines in sample.txt
- // lineCount++;
- //}
- //String[] lines = new String[lineCount];//initializes an array of strings with the length of linecount
- //int i = 0;
- while(scan.hasNext()) {//loads each line from samples.txt into a string and places it in the array at position i
- line = scan.nextLine();
- String[] lines = line.split("\\s");
- itemName = lines[0];
- itemDescription = lines[1];
- itemPrice = Double.parseDouble(lines[2]);
- quantity = Integer.parseInt(lines[3]);
- myItem.setItemName(itemName);
- myItem.setItemDesc(itemDescription);
- myItem.setItemPrice(itemPrice);
- myItem.setQuantity(quantity);
- itemCollection.put(itemName, myItem);
- }//end of while
- for (Entry<String, Item> entry : itemCollection.entrySet()) {
- myItem = entry.getValue();
- System.out.println(myItem.getItemName());
- System.out.println(myItem.getItemDesc());
- System.out.println(myItem.getItemPrice());
- System.out.println(myItem.getAvailableQuantity());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement