Advertisement
YoFuzzy3

Here you go DaWolf

Oct 5th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.util.ArrayList; // Imports
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class Main{ // Class declaration
  7.  
  8.     public static void main(String args[]){ // Main method
  9.        
  10.         String[] inv = {"rock", "knife", "spoon", "sword"}; // Making array
  11.         List<String> cInv = new ArrayList<String>(Arrays.asList(inv)); // Converting array to ArrayList
  12.         Scanner scanner = new Scanner(System.in); // Creating scanner
  13.         System.out.println("Welcome to the game. Your inventory is: ");
  14.         for(String item : cInv){
  15.             System.out.println("- " + item); // Printing out all strings in the converted inventory
  16.         }
  17.         System.out.println("Press any key to continue.");
  18.         scanner.nextLine(); // Waiting for user input
  19.         System.out.println("Do you wish to add a stick, pebble or pokeball to your inventory?");
  20.         String input = scanner.nextLine(); // Read new user input
  21.         if(input.equalsIgnoreCase("stick") || input.equalsIgnoreCase("pebble") || input.equalsIgnoreCase("pokeball")){ // Check if item valid
  22.             cInv.add(input.toLowerCase()); // Add item to converted inventory
  23.             System.out.println("You added the item " + input + " to your inventory!");
  24.             System.out.println("Your new inventory:");
  25.             for(String item : cInv){
  26.                 System.out.println("- " + item); // Printing out all strings in the converted inventory (new item has been added)
  27.             }
  28.         }else{
  29.             System.out.println("Sorry, but that item does not exist."); // Invalid item
  30.         }
  31.         inv = cInv.toArray(new String[cInv.size()]); // Converting converted inventory back to array
  32.         scanner.close(); // Closing scanner to prevent resource leak
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement