Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList; // Imports
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- public class Main{ // Class declaration
- public static void main(String args[]){ // Main method
- String[] inv = {"rock", "knife", "spoon", "sword"}; // Making array
- List<String> cInv = new ArrayList<String>(Arrays.asList(inv)); // Converting array to ArrayList
- Scanner scanner = new Scanner(System.in); // Creating scanner
- System.out.println("Welcome to the game. Your inventory is: ");
- for(String item : cInv){
- System.out.println("- " + item); // Printing out all strings in the converted inventory
- }
- System.out.println("Press any key to continue.");
- scanner.nextLine(); // Waiting for user input
- System.out.println("Do you wish to add a stick, pebble or pokeball to your inventory?");
- String input = scanner.nextLine(); // Read new user input
- if(input.equalsIgnoreCase("stick") || input.equalsIgnoreCase("pebble") || input.equalsIgnoreCase("pokeball")){ // Check if item valid
- cInv.add(input.toLowerCase()); // Add item to converted inventory
- System.out.println("You added the item " + input + " to your inventory!");
- System.out.println("Your new inventory:");
- for(String item : cInv){
- System.out.println("- " + item); // Printing out all strings in the converted inventory (new item has been added)
- }
- }else{
- System.out.println("Sorry, but that item does not exist."); // Invalid item
- }
- inv = cInv.toArray(new String[cInv.size()]); // Converting converted inventory back to array
- scanner.close(); // Closing scanner to prevent resource leak
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement