+ 1

Java -How to Return in Something like Choices or a Block of code.

So Theres A Choices - A & B If the user inputs other letters,Characters,Numbers that Choices Dont Have its gonna something like return in Choices A & B and can Enter a Input Again.

25th Feb 2020, 6:56 PM
Booztay
Booztay - avatar
21 Answers
+ 1
Use swith statment
25th Feb 2020, 7:22 PM
Sumit Programmer๐Ÿ˜Ž๐Ÿ˜Ž
Sumit Programmer๐Ÿ˜Ž๐Ÿ˜Ž - avatar
+ 1
Hi, Booztay! In Java, every return statement is a primitive data type, an Object or null. If you need to return more than one value or Object, you should use Collections API (Set or a List of Objects) or Arrays (sequence of variables that can be of Objects or primitive data types).
26th Feb 2020, 4:27 PM
Rafael S Valle
Rafael S Valle - avatar
0
Description is not clear to me.. If you explain your doubt with a sample code, that will easy to understand.. But if you are taking about from function return, then you can return only one value. If you want ro return more than 1 value, then you have to use a data structure, a array, store those values in those and return that. Unpack in calinng function.. Only this is a possible way... If you are taking about main function implementation, then give a try and share your code if it incomplete with specific doubt.. Hope this helps you..
25th Feb 2020, 7:09 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
0
Jayakrishna theres a Variable called a & b And im gonna input like letter C, and my Code gonna Give me an Error but i can Still Input A and B to Continue
25th Feb 2020, 7:25 PM
Booztay
Booztay - avatar
0
Oh. So input you are taking is char type?
25th Feb 2020, 7:31 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
0
Jayakrishna CharAt() Method Thingy? Idontknow how to use that method Can u Post an Code or explain how to use it in my case
25th Feb 2020, 7:34 PM
Booztay
Booztay - avatar
0
Is it kind of a guessing game? You ask the user to input a number and if it is not the answer then you ask them to enter again.
25th Feb 2020, 7:36 PM
Avinesh
Avinesh - avatar
0
Try this... do{ System.out.println("Input options A or B"); try { char ch1=in.next(); char ch2=in.next(); } catch(Exception e) { System.out.println ("Invalid inputs"); } }while(ch1!='A' && ch2! ='B'); Edit: Before this, you need to read about Exception handling...
25th Feb 2020, 7:38 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
0
Avinesh Kind of
25th Feb 2020, 7:39 PM
Booztay
Booztay - avatar
0
ThankYou Jayakrishna Im So Gonna Save this Code :>
25th Feb 2020, 7:41 PM
Booztay
Booztay - avatar
0
Booztay If you are giving only proper input, but not correct input, means char type but not A or B, then you can neglect about try catch, only use do while... & Wel come..
25th Feb 2020, 7:44 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
0
while(true){ int a = sc.nextInt(); if(a>0 && a<10) { System.out.println("In range"); break; } else { System.out.println("Out of range: Try again"); } } // Just an example to check whether the number is in range from 0 to 10. If out of range, the loop runs again asking for a new input. If in range, the loop breaks.
25th Feb 2020, 7:44 PM
Avinesh
Avinesh - avatar
0
Booztay Are you taking 1 input or 2 ?.. Confused.. You said A and B..? If it is A or B, then use or(||) operator instead of &&. , take only one input....
25th Feb 2020, 7:49 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
25th Feb 2020, 7:50 PM
Booztay
Booztay - avatar
0
How many inputs you are taking..? OK. Change code snippet according to your requiments.. @Avinesh code works fine for single input.. Try that..
25th Feb 2020, 7:53 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
0
Jayakrishna im taking 8 inp
25th Feb 2020, 7:55 PM
Booztay
Booztay - avatar
0
Booztay Ok. Now you have 2 alternative solutions, try those..
25th Feb 2020, 7:58 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
26th Feb 2020, 3:10 PM
Sharofiddin
Sharofiddin - avatar
0
Booztay I hope this is what you want
26th Feb 2020, 3:11 PM
Sharofiddin
Sharofiddin - avatar
0
How it's possible
27th Feb 2020, 3:23 PM
Sumit Saw
Sumit Saw - avatar