P
P
pitimirov_a2015-10-28 06:41:28
Java
pitimirov_a, 2015-10-28 06:41:28

(java errors) Why does try catch interfere with further code execution?

sith[s] = new Sith();
System.out.print("Enter sith's name: ");
sith[s].name = in.next();
System.out.print("Enter the count of health points: ");
try{sith[s].health = in.nextInt();}catch(Exception e){System.out.println("He will have 100hp");}
System.out.print("Enter the value of power: ");
try{sith[s].power = in.nextByte();}catch(Exception e){System.out.println("He will have base power (1)");}

I have such a (unpleasant shit-) code and, alas, on the first trycatch error i.e. when I specifically allow it, all subsequent blocks of code inside the try are automatically ignored.
That is, if we sith[s].health = in.nextInt();send the "#" character to the input here, for example, the following outputs to the console:
He will have 100hp
Enter the value of power: He will have base power (1)
How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey S., 2015-10-28
@pitimirov_a

try { 
    if (in.hasNextByte()) { sith[s].power = in.nextByte();}
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question