mardi 23 janvier 2018

If Statement for ArrayList to check if already entered

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package schmitm3_p1;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author mschmitt
 */
public class PegBoardGame {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    ArrayList<Integer> pegboard = create_pegboard();

    print_pegboard(pegboard);

    for (int i = 0; i < 10; i++) {
         peg_hole(pegboard);
         print_pegboard(pegboard);

    }
}


public static ArrayList<Integer> create_pegboard(){

    ArrayList<Integer> pegboard = new ArrayList<>();

    for (int n = 1; n <= 10; n ++ ) {

        pegboard.add(n);    
    }
     return pegboard;
}

public static void print_pegboard(ArrayList<Integer>pegboard){

    System.out.println("--------------------------------------------------");


    String print = "";

    for(int i= 0;i<pegboard.size(); i++ ){
        print = print + "(" + pegboard.get(i) + ")";
    }
    System.out.println(print);

    System.out.println("--------------------------------------------------");

}

public static void peg_hole(ArrayList<Integer>pegboard){
    Scanner in = new Scanner(System.in);

    //users must enter number to peg
    System.out.println("Enter a hole number to peg between 1-10:");

    //int input from user
    int pegnum = in.nextInt();
    //remove the value of the integer input or else would remove the index number
    int pegnumm = pegboard.indexOf(pegnum);
    pegboard.set(pegnumm, 0);



 }
 }

I am looking for a little help with a program I am writing right now. The program takes an integer input between 1-10 and removes that from the ArrayList and replaces it with a zero. I am trying to write an If Statement so that if they enter the same number twice or if they enter a number where there is already a zero it gives a message and states to enter another number. I have tried

      if(pegboard.indexOf(pegnum) == 0)
      {System.out.println("That number has already been entered");}

And a few other statements with no luck. Any help or suggestions would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire