0

I have a program which tells the user to input three ints and converts each one to a string. I.e 1 = one ; ...etc. The problem here is defining the ints for every user input. How can i get it with just one input and loop it to take another, considering to separate inputs so we can use it individually in every phrase down here.

public static void main(String[] args) {

    String[] i = { "zero" , "one" , "two" , "three" , "four" , 
                    "five", "six", "seven" , "eight" , "nine" , "ten" };

    Scanner in = new Scanner(System.in);

    System.out.println("I have ..... Networking books, ..... Database books, and ..... Programming books. ");

    String text = "" , text1 = "", text2 = "" ;    
    // loop i/p and using it in every phrase seperatellty 

    for (int a = 1; a <= 1; a++) {
        int word = in.nextInt();
        int word1 = in.nextInt();
        int word2 = in.nextInt();

        text = i[word];
        text1 = i[word1]; 
        text2 = i[word2]; 
    }

    System.out.println("I have " + text + " Networking books, "
                                        + text1 + "  Database books, and " 
                                        + text2 +" Programming books. ");

}} 
2
  • Why do you want to loop?? Commented Jul 21, 2015 at 3:50
  • it just an assignment tells me to do ! Commented Jul 21, 2015 at 4:13

1 Answer 1

2

Perhaps this is what you are trying to do

Note:

You have missed range check for the entered no. This will throw ArrayIndexOutOfBoundExceptionif you are trying to access the array i for indices above 10

import java.util.*;
class UserInputReader {

    private static final int EXPECTED_NUM_COUNT = 3;

    public static void main(String[] args) {
        String [] i = { "zero" , "one" , "two" , "three" , "four" , 
                    "five", "six", "seven" , "eight" , "nine" , "ten" };

        Scanner in = new Scanner (System.in);   
        String[] words = new String[EXPECTED_NUM_COUNT];

        int numCount = 0;
        while(numCount < 3) {
            System.out.println("Enter the number");
            int num = in.nextInt();
            if (num >= 0 && num <= 10) {
                words[numCount] = i[num];
                numCount++;
            } else {
                System.out.println("Enter no is invalid. Try again entering a number between 0 - 10");
            }
        }

        System.out.println("Entered nums " + Arrays.toString(words));

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

2 Comments

Hey, no problem and welcome. Glad that I could help.
this is what i wanted instead of define int for every input , just make it array and use it,s element to compare , thanks :D { System.out.println("I have "+ words[0]+ " Networking books," + words[1] +" Database books, and "+words[2]+ " Programming books.") ;

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.