java catch indexoutofboundsexception - try-catch

i googled a lot, but I dont know why I am not able to catch the indexoutofboundsexception error. Can sombody tell me, whats my mistake?
try
{
if (amountOfPlayers <= 15 && amountOfPlayers > 0) {
int i = 0;
do {
i++;
System.out.println("Please enter your name: ");
String name = scanner.next();
nameList.add(name);
} while (i < amountOfPlayers);
}
}
catch (IndexOutOfBoundsException ex)
{
System.out.println("Wrong input. Theres a maximum of 15 players allowed");
}

Related

How to count 'on event' in CANoe Capl?

I'd like to count on event for 200ms.
I tried with this code in CANoe Capl but it's not working well.
I don't know what the problem is.
help me plz.
MainActivity.Capl
variables
{
int timerConditionChecker = 0;
int lockStatusMonitor = 0;
mstimer conutCheckTimer;
}
on timer conutCheckTimer
{
//do nothing
}
on sysvar_update sysvar::Frame2
{
if(timerConditionChecker == 0)
{
lockStatusMonitor++;
timerConditionChecker = 1;
setTimer(conutCheckTimer, 500);
}
else if(timerConditionChecker == 1)
{
if(timeToElapse(conutCheckTimer) > 200)
{
timerConditionChecker = 2;
}
else
{
lockStatusMonitor++;
}
}
else if(timerConditionChecker == 2)
{
timerConditionChecker = 3;
Write("lockStatusMonitorCount = %d",lockStatusMonitor);
}
else{}
}
What about this (I mostly used your variable names):
variables
{
int lockStatusMonitor = 0;
mstimer conutCheckTimer;
}
on timer conutCheckTimer
{
// Is called after 200ms and will output how often the sysvar was updated within these 200ms
Write("lockStatusMonitorCount = %d",lockStatusMonitor);
}
on sysvar_update sysvar::Frame2
{
if(isTimerActive(conutCheckTimer))
{
// In case the 200ms have already started, just count
lockStatusMonitor++;
}
else {
// Timer is not yet active, so start counting for the next 200ms now
lockStatusMonitor = 0; // or = 1 depending on whether your use case
setTimer(conutCheckTimer, 200);
}
}
Apart from that, using the CAPL debugger should help you to solve these kind of problems.

Countries Grouping in c

People in a group are sitting in a group numbered 1 to N. It is known that people of same countries are sitting together.
Output is a single integer denoting no of distinct countries.
Input
4 (no of test cases)
2 (no of people in group)
1 1 ( in this there are 2 people from diff country)
2
1 3
7
1 1 2 2 3 3 3
7
7 7 7 7 7 7 7
Output should be
2
Invalid Data
4
1
My program:please tell me where is the error
#include<stdio.h>
#include<string.h>
int main()
{
int tcaseno,nopgrp,flag=0;
int arr[1000];
int count=0,i=0,j=0,t=0;
scanf("%d", &tcaseno);
t=tcaseno;
while(t>0)
{
scanf("%d\n", &nopgrp);
for (i = 0; i < nopgrp;i++)
{
scanf("%d", &arr[i]);
}
for (j = 0; j < nopgrp;j++)
{
if(arr[j]==1)
{
count++;
}
else if(arr[j]==2)
{
if(arr[j+1]==2)
{
count++;
}
else
{
flag=1;
}
}
else if(arr[j]==3)
{
if((arr[j+1]==3)&&(arr[j+2]==3))
{
count++;
}
else
{
flag=2;
}
}
else if(arr[j]==4)
{
if((arr[j+1]==4)&&(arr[j+2]==4)&&(arr[j+3]==4))
{
count++;
}
else
{
flag=3;
}
}
else if(arr[j]==5)
{
if((arr[j+1]==5)&&(arr[j+2]==5)&&(arr[j+3]==5)&&(arr[j+4]==5))
{
count++;
}
else
{
flag=4;
}
}
else if(arr[j]==6)
{
if((arr[j+1]==6)&&(arr[j+2]==6)&&(arr[j+3]==6)&&(arr[j+4]==6)&&(arr[j+5]==6))
{
count++;
}
else
{
flag=5;
}
}
else if(arr[j]==7)
{
if((arr[j+1]==7)&&(arr[j+2]==7)&&(arr[j+3]==7)&&(arr[j+4]==7)&&(arr[j+5]==7)&&(arr[j+6]==7))
{
count++;
}
else
{
flag=6;
}
}
else if(arr[j]==8)
{
if((arr[j+1]==8)&&(arr[j+2]==8)&&(arr[j+3]==8)&&(arr[j+4]==8)&&(arr[j+5]==8)&&(arr[j+6]==8)&&(arr[j+7]==8))
{
count++;
}
else
{
flag=7;
}
}
else if(arr[j]==9)
{
if((arr[j+1]==9)&&(arr[j+2]==9)&&(arr[j+3]==9)&&(arr[j+4]==9)&&(arr[j+5]==9)&&(arr[j+6]==9)&&(arr[j+7]==9)&&(arr[j+8]==9))
{
count++;
}
else
{
flag=8;
}
}
else if(arr[j]==0)
{
flag=9;
}
}
if(flag!=0)
{
printf("Invalid Data");
flag=0;
}
else
{
printf("%d\n",count);
count=0;
}
t--;
}
return 0;
}
if((arr[j+1]==9)&&(arr[j+2]==9)&&(arr[j+3]==9) ...)
You can simplify the above code with another for loop. Evidently you just want to see how many different numbers there are in the array.
Note that one of the main reasons to use C, or to learn C, is for efficiency. Therefore int arr[1000] is somewhat out of place because it allocates 4000 bytes. You may want to streamline that with malloc/free.
You should use printf to tell the user what to input.
I took some guesses on what you are trying achieve.
int tcaseno, nopgrp, error;
int count, i;
int *arr;
printf("no_test_cases: ");
scanf("%d", &tcaseno);
while(tcaseno > 0)
{
error = 0;
count = 1;
printf("no_people_in_group: ");
scanf("%d", &nopgrp);
if(nopgrp > 0 && nopgrp < 1000)
{
arr = malloc(nopgrp * sizeof(int));
printf("Enter %d numbers: ", nopgrp);
for(i = 0; i < nopgrp; i++)
scanf("%d", &arr[i]);
for(i = 1; i < nopgrp; i++)
{
if(arr[i - 1] > arr[i])
error = 1;
else if(arr[i - 1] != arr[i])
count++;
}
free(arr);
}
else
error = 1;
if(error)
printf("Invalid Data\n");
else
printf("Result: %d\n", count);
tcaseno--;
}

What is wrong in this code? I'm getting time limit exceeded error.

#include<stdio.h>
#include<string.h>
int main() {
int test_case, count, i, j, check, count_1; char a[100005];
scanf("%d", &test_case);
while(test_case--) {
scanf("%s", a);
count=count_1=0;
for(i=0;i<strlen(a);i++) {
if(a[i]=='1') {
count_1++;
}
}
for(i=0;i<strlen(a);i++) {
if(a[i]=='1'&&a[i+1]!='1'&&a[i+1]!='\0') {
count++;
for(j=(i+1);j<strlen(a);j++) {
if(a[j]=='0') {
a[j]='1';
a[j-1]='0';
count++;
}
else {
break;
}
}
}
if(i==(strlen(a)-1)) {
check=0;
for(j=(strlen(a)-1);j>=1;j--) {
if(a[j]=='1'&&a[j-1]=='1') {
check++;
}
else {
break;
}
}
if(check==(count_1-1)) {
break;
}
else {
i=0;
}
}
}
printf("%d\n", count);
}
return 0;
}
This is the question.
I have carried out a few dry runs and I'm getting the results. I'm not sure why I'm getting time limit exceeded error. The code is working for every test case except #2 (that is 1100001).
Please help me I'm stuck on this one for quite a while.
I just needed to change i=0 to i=-1 to avoid infinite looping.

try catch blocks not able to figure out catch java

SORRY ABOUT FORMATTING. i am trying to determine if the grade is a passing, failing, or invalid grade. however i can't figure out how to catch the error.
EDIT: 70-100 OR "s" or "S" = pass; 0-69 OR u or U = retake; everything else = invalid.
This is my code:
import java.util.*;
public class Demo
{
public static void main(String [] args)
{
Scanner kb = new Scanner(System.in);
String total="";
System.out.println("enter grade");
total = kb.nextLine();
System.out.println(evaluateGrade(total));
}
public static String evaluateGrade(String expr)
{
String result ="";
boolean invalid = false;
int grade = Integer.parseInt(expr);
try{
if((grade <100 && grade >=70) || (expr.equalsIgnoreCase("s"))
{
result ="pass";
}
else if((grade <70 && grade >0)|| expr.equalsIgnoreCase("u"))
{
result ="retake";
}
else
{result="invalid";
}
} catch (Exception e)
{
}
return result;
}
}
This is my code:
import java.util.*;
class Demo{
public static void main(String [] args){
Scanner kb = new Scanner(System.in);
System.out.println("enter grade");
System.out.println(evaluateGrade(kb.next()));
kb.close(); // You need to ".close" your stuff
}
public static String evaluateGrade(String expr){
String result ="";
// boolean invalid = false; <--- This is not necessary
if(expr.equalsIgnoreCase("s")){
return "pass";
}
if(expr.equalsIgnoreCase("u")){
return "retake";
}
try{ // Try to get a Integer on "grade"
Integer grade = Integer.valueOf(expr);
if( (grade <= 100 && grade >=70)){ // 70 <= grade <= 100, and you forgot the ')'
result ="pass";
}
else if((grade <70 && grade >=0)){ // 0 <= grade < 70
result ="retake";
}
else{
result="invalid";
}
}
catch(NumberFormatException e){ // If "grade" is not a number, then you have this line
System.err.println("Your grade must be a NUMBER between 0 and 100.");
return "invalid"; // Return something to keep going with your code
}
return result;
}
}
If you're not understanding what is happening with your code, write it down on the paper.
Have a good day!

I want to write a custom filter which works as similar to Path Hierarchy Tokenizer

please find attached code I can't post image of analysis as my reputation is less than 10. the thing is I am getting empty space until last token and there after all those tokes which are added. the problem here is in analysis I query is not matching with the index content I am not getting whats going wrong
public final boolean incrementToken() throws IOException
{
if(Flag_Tokens_are_read)
{
String buffer="";
if((loop++)!=tokenCount-1)
{
for(int k=loop;k<tokenCount;)
{
buffer = buffer+tokensArray.get(k).toString();
k++;
if(k<tokenCount)
buffer=buffer+" ";
else
{
if(Flag_First_Time_total_length)
{ startOffset=0;
endOffset=buffer.length();
Flag_First_Time_total_length=false;
}
else
{
int length = buffer.length()-1;
startOffset=endOffset-length;
//endOffset=17;
}
char[] newBuffer = buffer.toCharArray();
termAtt.setEmpty();
this.termAtt.copyBuffer(newBuffer, 0, newBuffer.length);
offsetAtt.setOffset(startOffset, endOffset);
posIncAttr.setPositionIncrement(0);
return true;
}
}
}
return false;
}
if (input.incrementToken())
{
String token = termAtt.toString();
tokensArray.add(token);
//this.termAtt.copyBuffer(newBuffer, 0, newBuffer.length);
return incrementToken();
}
else
{ if(Flag_First_Time)
{
tokenCount=tokensArray.size();
Flag_Tokens_are_read=true;
loop=-1;
Flag_First_Time = false;
return incrementToken();
}
else
{
return false;
}
}
this is my code for incrementtoken(). its working but I don't know why it is not working for query searching

Resources