i have program tells user input 3 ints , converts each 1 string. i.e 1 = 1 ; ...etc. problem here defining ints every user input. how can 1 input , loop take another, considering separate inputs can use individually in every phrase down here.
public static void main(string[] args) { string[] = { "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, , ..... programming books. "); string text = "" , text1 = "", text2 = "" ; // loop i/p , using in every phrase seperatellty (int = 1; <= 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, , " + text2 +" programming books. "); }}
perhaps trying
note:
you have missed range check entered no. throw arrayindexoutofboundexceptionif trying access array i indices above 10
import java.util.*; class userinputreader { private static final int expected_num_count = 3; public static void main(string[] args) { string [] = { "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 number"); int num = in.nextint(); if (num >= 0 && num <= 10) { words[numcount] = i[num]; numcount++; } else { system.out.println("enter no invalid. try again entering number between 0 - 10"); } } system.out.println("entered nums " + arrays.tostring(words)); } }
Comments
Post a Comment