Error in Java, Array, Break statement

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

int s[] = {0,10,20,30,40,50,60,70,80,90,100};
        int number, index = -1;  
        int i;
        Scanner scan = new Scanner(System.in);
        System.out.println ("Enter a value to explore").:");
            number = scan.nextInt();

        for(i=0; i<s.length; i++);{

            if(s[i]==number) {
                index = i;
            }       
        }
            if(index>=0) {
            System.out.println ("" +number + "value" + index + "in position")
              break;

            }else
             System.out.println ("" +number + "value does not exist");
            scan.close();
    }

}

Error in break (break cannot be used outside of a loop or a switch) and when you delete and turn the break, another error (java.lang.ArrayIndexOutOfBoundsException: Index 11 out of bound for 11>) occurs on the console.

java break array

2022-09-20 11:16

1 Answers

int s[] = {0,10,20,30,40,50,60,70,80,90,100};
int number, index = -1;  
int i;
Scanner scan = new Scanner(System.in);
System.out.println ("Enter a value to explore").:");
number = scan.nextInt();

for(i=0; i<s.length; i++){

    if(s[i]==number) {
        index = i;
    }       
//}
    if(index>=0) {
        System.out.println ("" +number + "value" + index + "in position")
        break

    }else{
        System.out.println ("" +number + "value does not exist");
        scan.close();
    }
}
//}


2022-09-20 11:16

If you have any answers or tips


© 2022 pinfo. All rights reserved.