简体   繁体   中英

while loop is prompting one more word than asked in

/*
 * 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 controller;

import java.util.Scanner;

/**
 *
 * @author Donovan
 */
public class Controller {

    public Controller() {

        sentenceArray();
}

    private void sentenceArray() {
        int size = 0;
        String word = "";
        int i = 0;


        Scanner input = new Scanner(System.in);
            System.out.println("How many words would you like to enter?");
                size = input.nextInt();
                String [] sentence = new String[size];
                System.out.println("please enter a word.");
                word = input.next();



        while( i < size){

            if( i < size){
                sentence[i] = word;
                i++;
                System.out.println("Please enter a word.");

                word = input.next();

           }//end if
        }//end while

        displayArray(sentence);
    } // end sentence array


    public static void displayArray(String []sentence){
                System.out.print( "Your sentence is: " );

        for (String x : sentence){                
            System.out.print( x  + "\t" );
        }

    }//end displayResults


}//end controller

The problem is that you ask for input after i >= size .

Specifically, these lines

i++;
System.out.println("Please enter a word.");

word = input.next();

And this isn't C, so you can declare you variables in-line with the method calls.

public void sentenceArray() {

    Scanner input = new Scanner(System.in);
    System.out.println("How many words would you like to enter?");
    int size = input.nextInt();
    String[] sentence = new String[size];

    int i = 0;
    while (i < size) {
        System.out.println("please enter a word.");
        String word = input.next();
        sentence[i] = word;
        i++;
    }

    displayArray(sentence);
}

do while loop

I would use a do while loop which tests the condition after the loop body, you don't need an if that matches your loop conition, and you also don't need the temporary String . Something like,

private void sentenceArray() {
    Scanner input = new Scanner(System.in);
    System.out.println("How many words would you like to enter?");
    int size = input.nextInt();
    String[] sentence = new String[size];
    int i = 0;
    do {
        System.out.println("Please enter a word.");
        sentence[i] = input.next();
        i++;
    } while (i < size);
    displayArray(sentence);
}

Finally, I think you want a println() after your loop in displayArray . Like,

public static void displayArray(String[] sentence) {
    System.out.print("Your sentence is: ");

    for (String word : sentence) {
        System.out.printf("%s\t", word);
    }
    System.out.println();
}

As my understanding that you are asking for a redundant "Please enter a word"? If yes, please remove these lines above the while:

System.out.println("please enter a word.");
word = input.next();

Please also check the whole code here:

   public class Controller {

    public Controller() {
        sentenceArray();
    }

    private void sentenceArray() {
        int size = 0;
        int i = 0;

        Scanner input = new Scanner(System.in);
        System.out.println("How many words would you like to enter?");
        size = input.nextInt();
        String [] sentence = new String[size];

        while( i < size){
            System.out.println("Please enter a word.");
            sentence[i] = input.next();
            i++;
            //end if
        }//end while

        displayArray(sentence);
    } // end sentence array


    public static void displayArray(String []sentence){
        if(sentence.length > 0) {
            System.out.print( "Your sentence is: " );

            for (String x : sentence){                
                System.out.print( x  + "\t" );
            }
        }

    }//end displayResults

    public static void main(String[] args) {
        Controller controller = new Controller();
    }
}//end controller

Hope this help.

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