Trying a basic code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Trying a basic code

Hi, i want to ask if the below program is possible using basic codes. The program should only receive 3 integers, otherwise it puts off the example2. https://code.sololearn.com/ca215A1A110a Example1: Input: 1 24 7 Output: WOW! This is great! Example2: Input: 2 mall code 7 11 Output: Ummm.. This is awkward;

1st Jul 2021, 3:48 AM
Brianna
13 Answers
+ 3
Brianna it goes like this. I would encourage you to continue with Java course cause the exception handling technique is available there. // Using try...catch block import java.util.Scanner; public class Main { public static void main(String args[]) { try { Scanner input = new Scanner(System.in); int number1= input.nextInt(); int number2= input.nextInt(); int number3= input.nextInt(); if( number1 == 1 && number2 == 24 && number3 == 7 ) { System.out.println("WOW!"); System.out.println("This is great!"); } else { System.out.println("Ummm..."); System.out.println("This is awkward"); } } catch( Exception ex ) { System.out.println("Ummm..."); System.out.println("This is awkward"); } } }
1st Jul 2021, 7:52 AM
Ipang
+ 4
Brianna People can write comments in our codes, we can also write comments in others' codes. But those comments are only accessible when using SoloLearn app. I'm guessing you are accessing SoloLearn from the web?
1st Jul 2021, 7:47 AM
Ipang
+ 2
What should happen when: 1. No input was at all even provided. 2. When all input were non numeric, e.g. String. I notice an inconsistency here, in the post "WOW This is great" is displayed when input were (1, 24, 7). But in your code you used a condition that requires inputb(1, 14, 7) for getting such output. Which one is correct? I'd probably use exception handling (try...catch) block while reading the 3 integers to know whether `nextInt()` succeeded to get an `int` value (or not). This way we can count how many times we read input, and how many times we read numeric input (integral).
1st Jul 2021, 4:21 AM
Ipang
+ 2
I wrote a sample code in your code, check the comments.
1st Jul 2021, 7:32 AM
Ipang
+ 1
Okay 👌 So ... have you tried to use exception handling as I suggested? How did it go?
1st Jul 2021, 6:34 AM
Ipang
+ 1
Ipang Thank you so much!
1st Jul 2021, 8:28 AM
Brianna
+ 1
No problem 👌
1st Jul 2021, 8:49 AM
Ipang
0
Ipang Oh, I meant 24. The program should only receive these two inputs given in example1 and example2.
1st Jul 2021, 6:31 AM
Brianna
0
Ipang , I haven't tried that yet so I'm having a problem.
1st Jul 2021, 7:17 AM
Brianna
0
Ipang I may sound stupid, but where can I see the comments?
1st Jul 2021, 7:43 AM
Brianna
0
Yes, i dont have an app. But I think you can send me a link, just write comments on the code
1st Jul 2021, 7:49 AM
Brianna
0
Hi I want to know when ever I code it shows error in main() I don't know why can anyone help me I am a new user of this app
2nd Jul 2021, 5:14 PM
Aman Singh
Aman Singh - avatar
0
Aman Hi, You must make a new thread for your own. Don't post question in other people's thread. Follow the guides helow, and don't forget to include link to the troubling code, for analysis. https://www.sololearn.com/Discuss/333866/?ref=app https://www.sololearn.com/post/75089/?ref=app
2nd Jul 2021, 5:35 PM
Ipang