Go to Top

Saturday 1 October 2011

Utilities 9 - Stylish way of array input

Just copy the following program.

Java Program :

import java.io.*;
class array_input
{
 static void input()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int[] dump,ret;
     String s,s1="";
     char k;
     int c=0,ac=0;
     System.out.print("Enter the array : ");
     s=br.readLine();
     dump=new int[s.length()];
     while(c<s.length())
     {
            while(c<s.length())
            {
                k=s.charAt(c);
                c+=1;
                if((int)k>=48 && (int)k<=57)
                {
                    s1=s1+k;
                    continue;
                }
                break;
            }
            if(s1!="")
            {
                dump[ac]=Integer.parseInt(s1);
                ac+=1;
            }
            s1="";
        }
     ret=new int[ac];
     System.arraycopy(dump,0,ret,0,ac);
     System.out.print("\nNumbers are : ");
     for(int i=0;i<ret.length;i++)
     System.out.print(ret[i]+"  ");
    }
}

To give the input, write the integers with spaces or commas or any String between it, and hit enter. You'll see the individual integers popping up. Paste this code wherever you want, and just corp the array "ret" into anywhere.

No comments:

Post a Comment

ShareThis