I am trying to make a simple even or odd program. I want it to keep running until the user enters in 'q'. But I am having trouble accepting 'q' as a String.
import java.util.Scanner;
class EvenOrOdd {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Welcome to my program that checks if a number is even or odd.");
while (true) {
System.out.println();
System.out.print("Please type number in a number ['q' to quit]: ");
int number;
String quit;
try {
number = myScanner.nextInt();
} finally {
quit = myScanner.nextLine();
}
if (quit.equals("q")) {
break;
} else if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
}
}
}
The program works fine when I enter numbers, but when I enter 'q', the console throws an error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at EvenOrOdd.main(EvenOrOdd.java:19)
I know this may be easy for many of you, but I have just picked up a java book and am trying to finish the task. Any help would be greatly appreciated!
You can make something like this and i found that a boolean is better for a loop in this case instead of while(true) and break:
public class EvenOrOdd {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out
.println("Welcome to my program that checks if a number is even or odd.");
boolean enterLoop = true;
while (enterLoop) {
System.out.println();
System.out.print("Please type number in a number ['q' to quit]: ");
String scannerinput = myScanner.nextLine();
if (scannerinput.equals("q")) {
enterLoop = false;
} else {
checkNumber(scannerinput);
}
}
}
private static void checkNumber(String scannerinput) {
try {
int number = Integer.parseInt(scannerinput);
if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
} catch (Exception e) {
System.out.println("No Number!");
}
}
}
Take a String from Scanner, check if is 'q' and if not, convert it to int and then check even or odd.
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Welcome to my program that checks if a number is even or odd.");
while (true) {
System.out.println();
System.out.print("Please type number in a number ['q' to quit]: ");
String inText = myScanner.next();
if (inText.equals("q")){
break;
}
int number = Integer.valueOf(inText);
if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
}
}
import java.util.Scanner;
class EvenOrOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to my program that checks if a number is even or odd.");
while (true) {
System.out.print("\nPlease type number in a number ['q' to quit]: ");
String input = scanner.next();
if (input.equals("q")) {
break;
} else {
int number = Integer.parseInt(input);
System.out.print(number + " is ");
System.out.print(number%2 == 0 ? "Even." : "Odd.");
}
}
}
}
That'll do it. :)
Instead of using myScanner.nextInt(), just use myScanner.next() to get a String. Then if it is not "q", use Integer.valueOf(inputString) to get the int and check it for even/odd.
while (true) {
String input = myScanner.next();
if ("q".equals(input)) {
break;
} else {
int number = Integer.valueOf(input);
if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
}
}
Try this approach. Check code note for more details.
import java.util.Scanner;
class EvenOrOdd {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Welcome to my program that checks if a number is even or odd.");
String input=null;
int number;
boolean flag=true; // loop flag
do {
System.out.println();
System.out.print("Please type number in a number ['q' to quit]: ");
// Take user input as String
input=myScanner.nextLine();
try
{
// convert the string value to integer value
number = Integer.parseInt(input);
if (number % 2 == 0)
{
System.out.println(number + " is Even.");
}
else
{
System.out.println(number + " is Odd.");
}
}
catch (NumberFormatException nfe)
{
// control goes here if input is not integer value
if(input.equals("q")) // exist option
flag=false;
else // invalid input
System.out.println("Invalid input, Please enter integer value or (q) to exist");
}
} while (flag);
}
}
You should update your method flow, you are actually trying to pass and intger validation for a string so first take the input as String and check whether it is your quit character q then if it is not try to parse the string to and int primitive with Integer.parseInt(input) and test if it is Odd or Even.
If the process fails assuming it is neither a q nor a number (any other character), then a message will be prompted for user telling him to use a valid number or "q" to quit:
import java.util.Scanner;
class EvenOrOdd {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Welcome to my program that checks if a number is even or odd.");
while (true) {
System.out.println();
System.out.print("Please type number in a number ['q' to quit]: ");
int number;
String input = myScanner.next();
if (input.equals("q")) {
break;
} else {
try {
number = Integer.parseInt(input);
if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
} catch (NumberFormatException nfe) {
System.out.println("Enter valid number or \"q\" to quit!");
}
}
}
}
}
It is because the program is expecting a input of int-type, basically the program outputs: Please type number in a number ['q' to quit], and after that it will reach the myScanner.nextInt(); line and will be waiting for a input, and since "q" is not a integer it will throw an exception.
A quick solution would be to use myScanner.nextLine() and then convert the string into a integer unless it is equal to 'q'. Something like this:
import java.util.Scanner;
public class EvenOrOdd {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Welcome to my program that checks if a number is even or odd.");
while (true) {
System.out.println();
System.out.print("Please type number in a number ['q' to quit]: ");
String string = myScanner.nextLine();
int number = 0;
if (string.equals("q")) {
myScanner.close(); // Close the scanner.
break;
} else if ((number = toInteger(string)) == -1){ // Is the string a number, less than Integer.MAX_VALUE and greater than Integer.MIN_VALUE?
System.out.printf("%s is not a valid integer!%n",string);
} else if (number % 2 == 0) {
System.out.println(number + " is Even.");
} else {
System.out.println(number + " is Odd.");
}
}
}
private static int toInteger(String str){
try{
return Math.abs(Integer.parseInt(str));
}catch(NumberFormatException e){
return -1;
}
}
}
By the way, always close the scanner, otherwise a resource leak may occur.
Related
package react;
import java.util.Scanner;
public class Intputfromuser {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("enter a number to compare with number 5 ");
Scanner input= new Scanner(System.in);
int a=input.nextInt();
if(a==2)
{
System.out.println("U Have Entered The same value");
}
else if(a<2)
{
System.out.println("Ur number is Smaller than 2");
}
else if(a>2)
{
System.out.println("U Have Entered the number Greater than ");
}
else {
System.out.println("U Have Enterer Invalid Input");
}
}
}
how to get only integer from the user if the user enters any thing except integer then else statement should run
Another alternative. Be sure to read the comments in code:
public static void main(String[] args) {
/* Open a keyboard input stream. There is no need to close
this stream. The JVM will do that automatically when the
application closes. */
Scanner input = new Scanner(System.in);
String val = ""; // Used to store User input:
// User Prompt with 'quit' capability and entry validation:
while (val.isEmpty()) {
System.out.print("Enter a number to compare with number 5 (q to quit): -> ");
val = input.nextLine().trim(); // Trim in case just a whitespace(s) was entered.
// Was 'q' for quit supplied?
if (val.equalsIgnoreCase("q")) {
/* Yes...then quit. Returning out of main() effectively
closes this particular application: */
System.out.println("Quiting - Bye Bye");
return;
}
// Validate Entry:
/* Is entry a string representation of a signed or unsigned Integer
value and does the supplied value fall within the relm of an int? */
if (!val.matches("-?\\d+") || (Long.parseLong(val) < Integer.MIN_VALUE) ||
(Long.parseLong(val) > Integer.MAX_VALUE)) {
// No...Inform User and allow to try again:
System.out.println("Invalid Numerical Entry! {" + val + ") Try again..."
+ System.lineSeparator());
val = ""; // Empty variable to ensure re-loop:
}
}
// If you make it to this point in code, the User input was valid!
// Now parse the String numerical value to an int:
int a = Integer.parseInt(val);
/* At this point, there are only three usable conditions:
Equal To, Less Than, and Greater Than (validity has
already been handled within the `while` loop: */
// Equal To:
if (a == 5) {
System.out.println("You have entered The same value.");
}
// Less Than:
else if (a < 5) {
System.out.println("Your number is smaller than 5.");
}
// Greater Than:
else {
System.out.println("You have entered a number greater than 5.");
}
// DONE
}
You can also create method to collect input and make it inside loop like this:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("enter a number to compare with number 5: ");
int userInput = getInteger();
if (userInput == 2)
{
System.out.println("U Have Entered The same value");
}
else if (userInput < 2)
{
System.out.println("Ur number is Smaller than 2");
}
else {
System.out.println("U Have Entered the number Greater than 2");
}
}
static int getInteger() {
boolean correct = false;
Scanner input = new Scanner(System.in);
int userInput = 0;
do {
try {
userInput = input.nextInt();
correct = true;
} catch (Exception e) {
System.out.println("Incorrect input");
System.out.println("Please try again: ");
} finally {
input.nextLine();
}
}
while (!correct);
input.close();
return userInput;
}
}
Important note with scanner.nextInt() or scanner.nextDouble()
you need to call scanner.nextLine() after that to clear input. Otherwise you will end up with endless loop.
Use input.nextLine() instead and parse it to a String.
To avoid a ParseException, surround it by using a try { ... } catch() { ... } block.
In the catch block you can e.g. print a message informing the user of the wrong input.
public static void main(String[] args) {
System.out.println("enter a number to compare with number 5 ");
Scanner s = new Scanner(System.in);
String userInput = s.nextLine();
try {
int option = Integer.parseInt(userInput);
if (option == 2)
{
System.out.println("U Have Entered The same value");
}
else if (option < 2)
{
System.out.println("Ur number is Smaller than 2");
}
else if (option > 2)
{
System.out.println("U Have Entered the number Greater than 2");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
}
}
Hope this sort of helped!
import java.util.*;
public class Main {
static void factorFinder() {
Scanner sc = new Scanner(System.in);
boolean valid = false;
int number = 0;
while(! valid ){
System.out.println("Enter the number you want to find the factor of(Numbers only)");
try {
number = sc.nextInt();
valid = true;
} catch (InputMismatchException e) {
System.out.println("Not a number.");
sc.next();
}
}
sc.close();
System.out.print("Factors of " + number + " are: ");
for (int i = 1; i <= number; ++i) {
// if number is divided by i
// i is the factor
if (number % i == 0) {
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
Scanner choiceScanner = new Scanner(System.in);
System.out.println("Do you want to use the factor finder? (y/n)");
String answer = choiceScanner.nextLine();
choiceScanner.close();
if(answer.equals("y")){
factorFinder();
}else{
System.exit(0);
}
}
}
Here is the terminal output(Ran through replit)
sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java')
java -classpath .:target/dependency/* Main
Do you want to use the factor finder? (y/n)
y
Enter the number you want to find the factor of(Numbers only)
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Main.factorFinder(Main.java:13)
at Main.main(Main.java:38)
exit status 1
I am making a calculator that finds the factor of a given number. The calculator part is done and is working. However, the part where it asks to use the calculator isn't working. It gave a NoSuchElementException error. Can someone maybe help me on this?
Once you close a Scanner, you cannot read form another one.
So, you have two possible solution (as I can see):
1 (Recomended). Use only one Scanner. You can make it class member or pass it to the method as a parameter.
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static void factorFinder() {
boolean valid = false;
int number = 0;
while(! valid ){
System.out.println("Enter the number you want to find the factor of(Numbers only)");
try {
number = sc.nextInt();
valid = true;
} catch (InputMismatchException e) {
System.out.println("Not a number.");
sc.next();
}
}
System.out.print("Factors of " + number + " are: ");
for (int i = 1; i <= number; ++i) {
// if number is divided by i
// i is the factor
if (number % i == 0) {
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
System.out.println("Do you want to use the factor finder? (y/n)");
String answer = choiceScanner.nextLine();
if(answer.equals("y")){
factorFinder();
}else{
sc.close();
System.exit(0);
}
sc.close();
}
}
Close the choiceScanner only after you call your factorFinder() method.
public class Main2 {
static Scanner sc = new Scanner(System.in);
static void factorFinder() {
boolean valid = false;
int number = 0;
while(! valid ){
System.out.println("Enter the number you want to find the factor of(Numbers only)");
try {
number = sc.nextInt();
valid = true;
} catch (InputMismatchException e) {
System.out.println("Not a number.");
sc.next();
}
}
System.out.print("Factors of " + number + " are: ");
for (int i = 1; i <= number; ++i) {
// if number is divided by i
// i is the factor
if (number % i == 0) {
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
Scanner choiceScanner = new Scanner(System.in);
System.out.println("Do you want to use the factor finder? (y/n)");
String answer = choiceScanner.nextLine();
if(answer.equals("y")){
factorFinder();
}else{
sc.close();
System.exit(0);
}
sc.close();
}
}
so I am trying to do my homework, this being the question:
Write a program that prompts the user to read two integers and displays their sum. If anything but an integer is passed as input, your program should catch the InputMismatchException that is thrown and prompt the user to input another number by printing "Please enter an integer."
Below is the sample run and what I am supposed to test.
SAMPLE RUN #1: java InputMismatch
Enter an integer: 2.5↵
Please enter an integer↵
Enter an integer: 4.6↵
Please enter an integer↵
Enter an integer: hello!↵
Please enter an integer↵
Enter an integer:7↵
Enter an integer:5.6↵
Please enter an integer↵
Enter an integer:9.4↵
Please enter an integer ↵
Enter an integer:10↵
17↵
When I am testing my code and putting in the integers, it works as it is supposed to, however, I am stuck on getting the integers to add together when both inputs are entered correctly. What am I doing wrong?
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestInputMismatch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
boolean isValid = false;
while (!isValid) {
try {
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("The number entered is " + number);
boolean continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Try again. (" + "Incorrect input: an integer is required)");
input.nextLine();
}
}
System.out.println((num1 + num2));
}
}
try adding another condition to your while and putting the numbers in an array.
int[] numbers = new int[2];
and change this in your while loop:
int count = 0;
while (!isValid && count <2) {
try {
System.out.print("Enter an integer: ");
numbers[count] = input.nextInt();
count++;
System.out.println("The number entered is " + number);
boolean continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Try again. (" + "Incorrect input: an integer is required)");
input.nextLine();
}
}
Hope that helped.
Check with this approach:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numArray = new int[2];
int count = 0;
while (count <= 1) {
try {
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("The number entered is " + number);
numArray[count] = number;
count++;
} catch (InputMismatchException ex) {
System.out.println("Try again. (" + "Incorrect input: an integer is required)");
input.nextLine();
}
}
int num1 = numArray[0];
int num2 = numArray[1];
System.out.println((num1 + num2));
}
You need to stitch together the logic. As you are taking 2 numbers 2 flags will ensure you got correct input for both variables. Also both flags ensure you are taking input correctly for num1 or num2 when an exception occurs.
If you need to input n arbitrary integers, then you may want to use dynamic collections and add numbers to collection.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
boolean num1Valid = false;
boolean num2Valid = false;
while (num1Valid==false || num2Valid==false) {
try {
if(num1Valid == false) {
System.out.print("Enter an integer for num1: ");
num1 = input.nextInt();
System.out.println("The number entered for num1 is " + num1);
num1Valid = true;
}
if(num2Valid == false) {
System.out.print("Enter an integer for num2: ");
num2 = input.nextInt();
System.out.println("The number entered for num2 is " + num2);
num2Valid = true;
}
}
catch (InputMismatchException ex) {
System.out.println("Try again. (" + "Incorrect input: an integer is required)");
input.nextLine();
}
}
input.close()
System.out.println((num1 + num2));
}
You need capture the exception, in this case you can using e.getMessage()
public class TestInputMismatch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1=0, number=0;
boolean isValid = false;
while (!isValid) {
try {
System.out.print("Enter an integer: ");
number = input.nextInt();
if(number > 0) {
System.out.println("The number entered is " + number);
}
num1 += number;
System.out.println("are would you like continue the program? Y or
N ");
String condition = input.next();
if(condition.equalsIgnoreCase("Y")) {
isValid = false;
} else {
isValid = true;
}
}
catch (InputMismatchException ex) {
System.out.println(ex.getMessage());
System.out.println("You cannot type words");
isValid = true;
}
}
System.out.println("Result = " + num1);
input.close();
}
}
in another case do you can use the method matches using expression language
saw example below:
if(valor.matches("[0-9]*"))
You can add the two numbers inside the try block
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestInputMismatch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
boolean isValid = false;
while (!isValid) {
try {
System.out.print("Enter an integer: ");
num1 = input.nextInt();
System.out.print("Enter an integer: ");
num2 = input.nextInt();
System.out.println("The numbers you entered are " + num1 + ","+num2);
int sum = num1+num2;
System.out.println("The sum Of the numbers you entered is: "+ sum);
boolean continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Try again. (" + "Incorrect input: an integer is required)");
input.nextLine();
}
}
System.out.println((num1 + num2));
}
}
import java.util.Scanner;
import java.util.InputMismatchException;
public class InputMismatch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numArray = new int[2];
int count = 0;
while (count <= 1) {
try {
System.out.print("Enter an integer:");
int number = input.nextInt();
numArray[count] = number;
count++;
} catch (InputMismatchException e) {
System.out.println("Please enter an integer.");
input.nextLine();
}
}
System.out.println(numArray[0] + numArray[1]);
}
}
I already wrote a code that convert from decimal number to roman number, however, i want it to do opposite way so how can i make it possible? (This is my previous code: http://pastebin.com/QFKi0xJh ) thank you! and here is my code right now.
I am just a beginner so my code is look little bit basic, please apology for that.
public static void main(String[] args) {
// Fill in the body
Scanner in= new Scanner(System.in);
String user = promptUserForNumeral(in);
while (user.length()!=0) {
int numb= convertNumeralToNumber(user);
System.out.println("The numeral "+user+ " is the decimal number "+numb);
user = promptUserForNumeral(in);
}
}
private static String promptUserForNumeral(Scanner inScanner) {
// Fill in the body
System.out.println("Enter a roman numeral (Q to quit): ");
String i = inScanner.nextLine();
while (i.length()>=0) {
if (i.length()==0) {
System.out.println("ERROR! You must enter a non-empty line!");
System.out.println("Enter a roman numeral (Q to quit): ");
i = inScanner.nextLine();
}
else if ( i.length()==1 && i.charAt(0)=='q' || i.charAt(0)=='Q') {
System.out.println("Goodbye!");
System.exit(0);
}
}
return i;
}
private static int convertNumeralToNumber(String numeral) {
// Fill in the body
int n=0;
if (numeral.equalsIgnoreCase("m")) {
n-=1000;
}
else if (numeral.equalsIgnoreCase("d")) {
n=500;
}
else if (numeral.equalsIgnoreCase("c")) {
n=100;
}
else if (numeral.equalsIgnoreCase("l")) {
n=50;
}
else if (numeral.equalsIgnoreCase("x")) {
n=10;
}
else if (numeral.equalsIgnoreCase("v")) {
n=5;
}
else if (numeral.equalsIgnoreCase("i")) {
n=1;
}
return n;
}
I want to handle NumberFormatException in more specific way.
This exception occurs, when it tries assign anything but an integer, when the following is entered:
string
character
empty input
double number
Depending on what was entered I want to display a proper message, like
you've entered string, please enter an integer
or
value can't be null, please enter an integer value
The code below catches NumberFormatException in general way.
I wonder is there a way to include more catch clauses.
import java.util.Scanner;
public class TestException {
static int input;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter an integer number: ");
try {
input = Integer.parseInt(scan.next());
System.out.println("You've entered number: " + input);
} catch (NumberFormatException e) {
System.out.println("You've entered non-integer number");
System.out.println("This caused " + e);
}
}
}
First take the input from the user and after that try to convert it to integer.
static int input;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter an integer number: ");
String inputString = scan.next();
try {
input = Integer.parseInt();
System.out.println("You've entered number: " + input);
} catch (NumberFormatException e) {
if(inputString.equals("") || inputString == null) {
System.out.println("empty input");
} else if(inputString.length == 1) {
System.out.println("char input");
} else {
System.out.println("string input");
}
}
}
You've to use if-else construct to specify your scenerios within catch block.
See the code below:
String inString = null;
try
{
iString = scan.next().trim();
input = Integer.parseInt(inString);
System.out.println("You've entered number: " + input);
}
catch (NumberFormatException e)
{
if(inString.equals("")
{
System.out.println("You've entered empty string.");
}
else if(inString.length() == 1)
{
System.out.println("You've entered a single char");
}
else
{
System.out.println("You've entered non-intereger number");
}
System.out.println("This caused " + e);
}
You could do some more tests on the input if parsing the input as an integer value caused an exception, something like this:
String scanned = null
try {
scanned = scan.next();
input = Integer.parseInt(scanned);
System.out.println("You've entered number: " + input);
} catch (NumberFormatException e) {
if (scanned == null || scanned.isEmpty()) {
System.out.println("You didn't enter any value");
} else if (scanned.length() == 1)
System.out.println("You entered a single char which is not a number");
}
// and more tests, you can even try to parse as Double
}
String aString = null;
aString = scan.next().trim();
System.out.println("You've entered number: " + aString);
if("".equals(aString.trim())){
System.out.println("You have entered an Empty String");
}else if(!isNumber(aString) && aString.length()==1){
System.out.println("You have entered a Character");
}else if(!isNumber(aString) && aString.length()>1){
System.out.println("You have entered a String");
}else if(isNumber(aString)){
int input = Integer.parseInt(aString.replaceAll(",",""));
System.out.println("You have entered a correct Number"+input);
}
private boolean isNumber(String s){
return s.matches("[0-9]+(,[0-9]+)*,?");
}
public static int isInt(){
boolean ok=false;
int b=1;
do{
String next = sc.next();
int a=b;
try{
a = Integer.parseInt(next);
}catch(Exception e){
System.out.println("Invalid Option...");
continue;
}
if(a==1){b=a;ok=true;}
else if(a==2){b=a;ok=true;}
else if(a==3){b=a;ok=true;}
}while(!ok);
return b;
}