0

Hey guys so I'm newer to Java and was running through some exercises and I wrote the following code

package Java;

import java.util.*;
public class datatypespractice {
    public static void main(String []args){   
            
        String[] dataTypes = {"* byte", "* short", "* int", "* long"};
            
        Scanner number = new Scanner(System.in);
        int w=number.nextInt(); 
        for(int t=0; t<w; t++){
        
            try{
            
            long x = number.nextLong();
            
            if (x <= 127 && x >= -128){
            
                 System.out.println(x + " can be fitted in:");
                 for(int i=0; i<=3; i++)
                 System.out.println(dataTypes[i]);}
            
            else if (x <= 32767 && x >= -32768){
            
                System.out.println(x + " can be fitted in:");
                for(int i=1; i<=3; i++)
                System.out.println(dataTypes[i]);}
            
            else if (x <= 2147483647 && x >= -2147483648){
            
                System.out.println(x + " can be fitted in:");
                for(int i=2; i<=3; i++)
                System.out.println(dataTypes[i]);}
            
            else if (x <=  9223372036854775807L  && x >=-9223372036854775808L ){
            
                System.out.println(x + " can be fitted in:");
                for(int i=3; i<=3; i++)
                System.out.println(dataTypes[i]);}
            
            } //closing try

            catch(Exception x){ 
                System.out.println(number.next() + " can't be fitted anywhere.");}
            
        } //closing the for-loop     
         
number.close();

    } //closing main
} //closing class


After the catch exception at the bottom if I use the code

System.out.println(x + "can't be fitted anywhere.");

it will repeat the "can't be fitted anymore" statement until it runs out of the "for" repetitions.

However by replacing x with number.next() it only repeats once and allows me to continue with further inputs. Can you explain why this is?

  • Because you're still in a for-loop? I think that your style of using braces makes this more difficult to understand, but the for-loop is outside the try/catch, so it will continue with the loop. – NomadMaker Oct 27 '20 at 00:30
  • NomadMaker is there a better way of doing braces that would make this easier for people to read? – frostyjones13 Oct 27 '20 at 01:48
  • I tend to use the same style I learned many years ago. It's pretty much the same style that Oracle uses in its java examples. The opening brace is at the end of the starting line, with a space before it. The closing brace is on a line all its own, in the same position as the opening statement. It may be that you're using something similar, but you need to edit your code above to use it consistently. Consistency in a style is useful to make it easier to read. – NomadMaker Oct 27 '20 at 03:22
  • Unrelated to your questions but please never use arbitrary names for your variables, especially if you intend other people to read your code. We have no idea to know what w, t or x mean. Down the line when you create longer code, you're also gonna hate yourself if you're down in line 1500 and see an error referencing "variable x" in line 30 or something and don't remember what x stood for. –  Oct 27 '20 at 04:07
  • Does this help? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Abra Oct 27 '20 at 04:34

1 Answers1

0

Your try-catch block is inside a for loop.

Upon handling the exception with your alternative code, it goes back to run another iteration of the loop. At this point, x is still in it's overflow value, because your alternative code does not reassign X to a new value, so it immediately throws a new exception.

As for your try-catch structure. Try-catch shouldn't go inside a for loop unless you REALLY want that to happen. You should put it outside of the for loop.

try{
    //whatever code you want
   } catch (Exception ex)
   { 
     //something to handle the exception
   }