Assigning and displaying at the same time | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Assigning and displaying at the same time

I have tried to user the scanner to give values to 3 variables. And the Variable value stored using the Scanner. lets see here : Scanner Sc = new Scanner(System.in); System.out.println("Enter the Hour, Minute and Seconds in the Military Format", H=Sc.nextInt(),M=Sc.nextInt(), S=Sc.nextInt() ); This will not be accepted. I have tried Printf, where I can see that there are no errors but nothing happens : Scanner Sc = new Scanner(System.in); System.out.printf("Enter the Hour, Minute and Seconds in the Military Format", H=Sc.nextInt(),M=Sc.nextInt(), S=Sc.nextInt() ); Can any one explain this ?

15th Aug 2017, 12:54 PM
Vivek Zacharia Muricken
Vivek Zacharia Muricken - avatar
4 Answers
0
don't use code playground. do it on your computer with an actual Java IDE like eclipse. you can easily see what go wrong.
15th Aug 2017, 12:57 PM
Gao Xiangshuai
Gao Xiangshuai - avatar
0
are you doing a separate line for each input in scanner?
15th Aug 2017, 1:08 PM
D_Stark
D_Stark - avatar
0
sorry, my English is bad. is this code ok... public static void main(String[] args) { int H = 0, M = 0, S = 0; Scanner Sc = new Scanner(System.in); for (int i = 0; i < 3; i++) { switch (i) { case 0: H = Sc.nextInt(); break; case 1: M = Sc.nextInt(); break; case 2: S = Sc.nextInt(); break; } } System.out.println("Enter the Hour, Minute and Seconds in the Military Format" + H + "," + M + "," + S); }
15th Aug 2017, 1:41 PM
Chathura Buddhika Liyanapathirana
Chathura Buddhika Liyanapathirana - avatar
0
the scanner needs importing and initialized to int i,h,m,s to work propley, what exactly are you trying to achieve as you have a for loop in there as well? get back to me and ill fix your code.
17th Aug 2017, 5:58 AM
D_Stark
D_Stark - avatar