Jo-Anne
Jo-Anne

Reputation: 139

Trying to display a certain String in an array after user input

I am super new to Java and having trouble with getting the output right. I am setting up an array and then asking the user to give me the number of the message they want displayed. So I see what it is doing. If I enter the number 5 expecting to have it display the 5th line, it will not do it but have me instead enter 5 numbers and then display the 5th line. I cannot figure out why it is doing this. I'm also supposed to wrap this whole thing in a method called shoutOutCannedMessage() and I'm not sure how to do this since I have already done it without that. I literally just started learning Java like two weeks ago. Thanks for any help. I'm going to continue testing and trying but thought I may get a bit of assistance.

import java.util.Scanner; //use java api scanner class

class MessageArray {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        //create an array of strings with 10 objects
        String[] array = new String[10];

        //create a counter to go through each message
        int counter = 0;
        int response;

        **shoutOutCannedMessage()**; {
        //start looping through each element in the array asking user for input 
        while (counter < array.length) {
            System.out.print((counter+1) + ": Please enter message: ");
            array[counter] = s.nextLine();
            counter++;
        }
        }


        System.out.print("Please enter the number of the message you would like displayed: ");
        counter=0;
        while (counter < array.length) {
                        response = s.nextInt();
            if (response == counter){
                System.out.println("Your selected value is " + array[counter]);
                counter = array.length+1;
            }
            counter++;
            }
    }

The output I am getting now is:

1: Please enter message: fdas
2: Please enter message: fgd
3: Please enter message: sdf
4: Please enter message: fh
5: Please enter message: af
6: Please enter message: dsf
7: Please enter message: h
8: Please enter message: fs
9: Please enter message: GDFS
10: Please enter message: FDAS
Please enter the number of the message you would like displayed: 
4
6
5
4
4
Your selected value is af

Upvotes: 0

Views: 76

Answers (2)

Jo-Anne
Jo-Anne

Reputation: 139

So I ended up doing the following per help above to solve my problem:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    //create an array of strings with 10 objects
    String[] array = new String[10];

    //create a counter to go through each message
    int counter = 0;
    int response;


    //start looping through each element in the array asking user for input 
    while (counter < array.length) {
        System.out.print((counter+1) + ": Please enter message: ");
        array[counter] = s.nextLine();
        counter++;
    }

    System.out.print("Please enter the number of the message you would like displayed: ");
    counter=0;
    response = s.nextInt();
    System.out.println("Your selected value is " + array[response-1]);
    s.close();

        }   

}

Upvotes: 0

pL4Gu33
pL4Gu33

Reputation: 2085

This works, you dont need another loop: Maybe some checks if it is higher 10 or under 1.

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    //create an array of strings with 10 objects
    String[] array = new String[10];

    //create a counter to go through each message
    int counter = 0;

    //start looping through each element in the array asking user for input 
    while (counter < array.length) {
        System.out.print((counter+1) + ": Please enter message: ");
        array[counter] = s.nextLine();
        counter++;
    }


    System.out.print("Please enter the number of the message you would like displayed: ");

    int response = s.nextInt();
    response-= 1;          
   System.out.println("Your selected value is " + array[response]);

}

Upvotes: 1

Related Questions