MY mENU


Friday, 19 April 2013

programm to reading values from keyboard into an arraylist untill 'z' keyword enterd, stops reading and than sorting them in ascending order.

import java.io.*;
import java.util.*;

public class ArraysOne {

public static void main(java.lang.String[] args) 
{
//int i;
ArrayList al=new ArrayList();
try{
Scanner input = new Scanner(System.in);

int check=0;
while(true){
check = input.nextInt();
if(check == 'z' || check=='Z') break;
al.add(check);
}

} catch(Exception e){
e.printStackTrace();}

Iterator itr=al.iterator();
while(itr.hasNext()){
Object i=itr.next();
System.out.println(i);
}
int j=al.size();
System.out.println("Size"+j);

Collections.sort(al);
System.out.println("ArrayList is sorted");

for(Integer temp: al){
System.out.println(temp);

}
/*for(int i=j;i>=i-1;i--){
for(int k=i-1;k>=;k--){
if(al.get(i)al.get(k)){
System.out.println(al.get(k));}
else{ System.out.println(al.get(i));}
}
}*/

}

}

No comments:

Post a Comment