简体   繁体   中英

How to take inputs in Java8 for Coding Competitions?

I have been practicing competitive coding and to my utter surprise, some codes are getting Runtime Error though my code runs perfectly well in local IDE like Eclipse, BlueJ, or Netbeans. I used Scanner class or BufferedReader class with StringTokenizer to take multiple inputs from single line. But, the runtime error prevails. Actually, in Competitive Coding, I think the input my code is taking is null, not a value. That is why Integer.parseInt(br.readLine()) throws Format exception.

I want to know how to take inputs in java8: single line multiple inputs and single line one input.

Below I am sharing a code that runs fine on my local IDE but throws a runtime error in Google Kick-start IDE:

/*recordbreaking problem of kickstart group D*/
    import java.util.*;
    import java.io.*;
    class RecordBreaking
    {
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }
    public static void main(String args[])
    {
    FastReader s=new FastReader();
    int t=s.nextInt();
    int tans[] = new int[t];
    int n,c,sum=0;
    int v[] = new int[10000];
    for(int i=0;i<t;i++)
    {
        c=0;
        n=s.nextInt();
        for(int j=0;j<n;j++)
        {
            v[i]=s.nextInt();
        }
        for(int m=0;m<n;m++)
        {
            for(int q=0;q<m;q++)
                sum=sum+v[q];
            if((m==(n-1)||m==0)||(v[m+1]<v[m])&&(sum<v[m]))
                c++;
            sum=0;    
        }
        System.out.println(c);
    }
    
    
    }
}

Problem Isyana is given the number of visitors at her local theme park on N consecutive days. The number of visitors on the i-th day is Vi. A day is record breaking if it satisfies both of the following conditions: The number of visitors on the day is strictly larger than the number of visitors on each of the previous days. Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day. Note that the very first day could be a record breaking day!

Please help Isyana find out the number of record breaking days.

Input The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Vi.

Sample Inputs Sample

Input
 
 
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9

**Output**
  
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0

It throws Runtime error! Help!
There is no such message in Google Kick-Start regarding where the problem lies.

For online IDE: the following error message is displayed:

Exception in thread "main" java.lang.NullPointerException
    at java.util.StringTokenizer.<init>(StringTokenizer.java:199)
    at java.util.StringTokenizer.<init>(StringTokenizer.java:236)
    at RecordBreaking$FastReader.next(RecordBreaking.java:23)
    at RecordBreaking$FastReader.nextInt(RecordBreaking.java:35)
    at RecordBreaking.main(RecordBreaking.java:65)

The problem with your code is your class name. On Google KickStart use class name as public class Solution. Also I ran your code and gives WA so this is my code.

import java.util.*;
public class Solution {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int loop=1; loop<=T; loop++){
           int N = sc.nextInt();
           int arr[] = new int[N+7];
            for(int i=0; i<N; i++){
                arr[i] = sc.nextInt();
            }
            int max = -1;
            int ans = 0;
            for(int i=0; i<N-1; i++){
                //if(arr[i]>max) max = arr[i];
                if(arr[i]>max ) {
                    if(arr[i]>arr[i+1])ans++;
                    max =arr[i];
                }
            }
            if(arr[N-1]>max) ans++;
            System.out.println("Case #" + loop+": "+ans);
        }
    }
}

Hope it is helpfull.! Do upvote if you find it correct and accept it.

I can't say I am %100 sure of the problem, but I'll share what I usually do. Standard Scanner should work, maybe there is another problem in this case but I don't believe it is possible to say from this information.

Scanner scanner = new Scanner(System.in);
int t= scanner.nextInt();
for (int i = 0; i < t; i++) {
    int n = scanner.nextInt();
    int[] numbers = new int[n];
    for (int j = 0; j < n; j++) {
        numbers[j] = scanner.nextInt();
    }
}

This always works for me but again I can't know for certain. Sorry, won't be able to help more but then again I'm not sure if anyone else can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM