简体   繁体   中英

Java Iterate array with a fixed length and take values with Scanner class

How can i get the value of a java array using Scanner class with a fixed length of 2, and which will iterate until its value is equal to a given value? For example; for the following inputs,

   A G 

   N H

   D F      

I wrote a for loop to take the values of fixed array road, in which the length is 2 using Scanner class.

 for(int i = 0; i<block.length; i++){
        System.out.println("enter number");
        block[i]=input2.next().charAt(0);
 }

I want to iterate this loop while the user input is {'C','C'}. THat means the array loop shpuld stop if the inputs are as follow;

AG

NH

DF

CC

How can i write the code to take user input values using Scanner class and to iterate the array? And the user input values should be copied without replacing them with newly entered values. Thank you!

assuming that your block and input2 variables are already set up and your loop as shown is working, put that loop inside a controller loop

 do {
    for(int i = 0; i<block.length; i++){
        System.out.println("enter number");
        block[i]=input2.next().charAt(0);
    }

 } while (block[0] != 'C" && block[1] != 'C' )

Try this way:

        Scanner input2 = new Scanner(System.in);
        char[] block = new char[2];
        ArrayList<String> arrayList = new ArrayList<String>();
        int i = 0;
        o:
        while (block[0] != 'C' && block[1] != 'C') {
            System.out.println("enter character");
            block[i % 2] = input2.next().charAt(0);
            i++;
            arrayList.add(input2.next());
            if(arrayList.size()>=2){
            if(arrayList.get(arrayList.size()-1).equals("C") && arrayList.get(arrayList.size()-2).equals("C"))
            {
                break o;
            }
            }
        }
   System.out.println(arrayList);

All you need is this

char[] block = new char[2];

while (block[0] != 'C' && block[1] != 'C') {
    System.out.println("enter number");
    block[0]=input2.next().charAt(0);
    System.out.println("enter number");
    block[1]=input2.next().charAt(0);
}

I assume the following from your question

  1. You have an array of fixed length into which you would like to read values using a Scanner
  2. Once you read values into the array,you would like to compare this array with values from another array and do something if the input array matches your array.

This is a simple program that does this:

    String[] scannedValues=new String[2];
    String[] matchValue={"X","Y"};
    boolean isMatched=false;
    Scanner s=new Scanner(System.in);
    while(!isMatched)
    {

        for(int i=0;i<scannedValues.length;i++)
        {
            scannedValues[i]=s.nextLine();
        }
        for(int i=0;i<scannedValues.length;i++)
        {
            if(matchValue[i].equals(scannedValues[i]))
                isMatched=true;
            else
                isMatched=false;
        }
            if(isMatched)
                s.close();
    }

You can use some of the Scanner methods such as nextInt() etc for finding various types of values.You can also pass regular expressions to the Scanner such as next("[AZ]{1}") However,in case you use a regular expression be aware that a mismatch between the input provided by the user and your expression will cause an InputMismatchException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM