0

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
3
  • 1
    you must re-evaluate why are you using the while block after the message asking the number of the message to be displayed Commented Jun 17, 2016 at 16:51
  • What @Leo said. You don't have to loop through the entire array to get to the element you're interested in. Commented Jun 17, 2016 at 16:55
  • array [response -1] did the trick as you both mentioned essentially, thx! Commented Jun 20, 2016 at 15:28

2 Answers 2

1

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]);

}
Sign up to request clarification or add additional context in comments.

Comments

0

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();

        }   

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.