SlideShare a Scribd company logo
1
Java Programming
Course Code: IT 201
MODULE – I
Dr. Sheenu Rizvi
Asstt. Professor
Dept Of CSE/IT ASET
AUUP Lucknow.
2
Array
An array is a collection of similar data types. Array is a container object that hold values of homogeneous type. It is
also known as static data structure because size of an array must be specified at the time of its declaration.
Array starts from zero index and goes to n-1 where n is length of the array. In Java, array is treated as an object
and stores into heap memory. It allows to store primitive values or reference values.
Array can be single dimensional or multidimensional in Java.
Single Dimensional Array
Single dimensional array use single index to store elements. You can get all the elements of array by just increment its index by
one.
Array Declaration
Syntax :
datatype[] arrayName;
or
datatype arrayName[];
3
Java allows to declare array by using both declaration syntax, both are valid.
The arrayName can be any valid array name and datatype can be any like: int, float, byte etc.
Example :
int[ ] arr;
char[ ] arr;
int[ ][ ] arr; // two dimensional array.
Initialization of Array
Initialization is a process of allocating memory to an array. At the time of initialization, we specify the size of array to
reserve memory area.
Initialization Syntax
1) arrayName = new datatype[size]
new operator is used to initialize an array.
The arrayName is the name of array, new is a keyword used to allocate memory and size is length of array.
4
Ex.
int size = 5, a[];
a= new int [size];
The size of an array using a variable array can be dynamically created at runtime.
1) We can combine both declaration and initialization in a single statement.
Datatype[] arrayName = new datatype[size]
Ex
Int a[ ] = new int [3];
5
Create An Array
Lets create a single dimensional array.
class Demo
{
public static void main(String[] args)
{
int[] arr = new int[5];
for(int x : arr)
{
System.out.println(x);
} }
}
Output 0 0 0 0 0
6
In the above example, we created an array arr of int type and can store 5 elements. We iterate the array to access
its elements and it prints five times zero to the console. It prints zero because we did not set values to array, so all
the elements of the array initialized to 0 by default.
Set Array Elements
We can set array elements either at the time of initialization or by assigning direct to its index.
int[] arr = {1,2,3,4,5};
Here, we are assigning values at the time of array creation. It is useful when we want to store static data into the array.
or
arr[1] = 105
Here, we are assigning a value to array’s 1 index. It is useful when we want to store dynamic data into the array.
7
Array Example
Here, we are assigning values to array by using both the way discussed above.
class Demo
{
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5};
for(int x : arr)
{
System.out.println(x);
}
8
// assigning a value
arr[1] = 10;
System.out.println("element at first index: " +arr[1]);
}
}
Output
1
2
3
4
5
element at first index: 10
9
Accessing array element
we can access array elements by its index value. Either by using loop or direct index value. We can use loop like: for, for-
each or while to traverse the array elements.
Example to access elements
class Demo
{
public static void main(String[] args)
{
int[] arr = {10,20,30,40,50};
10
for(int i=0;i<arr.length;i++)
{ //length give number of element stored in array
//length is not a method of the array object //rather length is an instance variable
System.out.println(arr[i]);
}
System.out.println("element at first index: " +arr[1]);
} }
10
20
30
40
50
element at first index: 20
11
public class arr {
public static void main(String args[]) {
int [] a= { 3, 34, 7, 9};
int len = a.length; //len = 4
System.out.println("length =" + len);
}
}
12
// The following code stores the numbers from 1 to 5 in a element in int array.
public class arr {
public static void main(String args[]) {
int[] a =new int[5];
for ( int i=0; i<a.length; i++) {
a[i]= i+1;
System.out.println("value =" +a[i]); //output is value=1
//2 3 4 5
}
}
}
13
Creation of table
public class arr {
public static void main(String args[]) {
int[] a =new int[11];
int b=2;
for ( int i=0; i<a.length; i++) {
a[i] = (i) * b;
System.out.println(i + "*"+ b+ "=" +a[i]);
} } }
14
The following for each iteration of the for loop
Syntax :
for(<data type> <variable name>:<array name>)
{
//code
}
Ex:
public class arr {
public static void main(String args[]) {
int[] a ={3,4,7,9};
for ( int x: a ) {
System.out.println(x);
}
}}
15
// WAP enter 5 values and display them.
import java.util.Scanner;
class array1
{
public static void main(String args[])
{
int i;
//int a[];
//a=new int[5]; // initialize array with name of array i:e a in int a[]
String []a=new String [5] ; // initialize at time of declaration
Scanner ob= new Scanner(System.in);
System.out.println("Enter values");
for(i=0;i<a.length;i++) //a.length give length of array
16
{
a[i]=ob.nextLine(); // enter name from user
//a[i]=ob.nextInt();
}
for(i=0;i<a.length;i++)
{
System.out.println("values are "+a[i]) ;
}
}
}
17
// Program for Linear Search
import java.util.Scanner; // from user input include
public class arr1 {
public static void main(String args[]) {
//int[] a = { 2, 4, 20,11, 40}; // an array not containing duplicates
//int target = 20; // the element to be searched
//from user input
Scanner in = new Scanner(System.in);
System.out.println("enter array");
int[]a=new int[8];
for(int b=0;b<a.length;b++)
{
18
a[b]=in.nextInt();
}
System.out.println("enter search element from keyboard");
int target= in.nextInt();
for( int i=0; i<a.length; i++) {
if(a[i]==target)
{
System.out.println ( "Element found at index "+i); //output element found at index 2
break; // break should be omitted if the array contains duplicates
}
}
} }
19
Program for Binary Search
public class arr1 {
public static void main(String args[]) {
int[] a = {2, 7, 20, 35, 45, 50, 85}; // a sorted array not containing duplicates
int target = 35; // the element to be searched
int left = 0;
int middle;
int right = a.length - 1;
while (left <= right) {
middle = (left + right) / 2;
if (a[middle] == target) {
System.out.println("Element found at index " + middle); //output element found at index 3
break;
} else if (a[middle] < target) {
20
left = middle + 1;
} else if (a[middle] > target) {
right = middle - 1;
}
}
} }
21
Multi-Dimensional Array
A multi-dimensional array is very much similar to a single dimensional array. It can have multiple rows and multiple
columns unlike single dimensional array, which can have only one row index.
It represents data into tabular form in which data is stored into row and columns.
Multi-Dimensional Array Declaration
datatype[ ][ ] arrayName;
Ex
int[][] multipliers = new int[10][]; // second dimension is optional
String[][] names = new String[5][];
22
Initialization of Array
datatype[ ][ ] arrayName = new int[no_of_rows][no_of_columns];
The arrayName is the name of array, new is a keyword used to allocate memory and no_of_rows and no_of_columns both
are used to set size of rows and columns elements.
Like single dimensional array, we can statically initialize multi-dimensional array as well.
int[ ][ ] arr = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
23
Example:
class Demo
{
public static void main(String[] args)
{
int arr[ ][ ] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
for(int i=0;i<3;i++)
{
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
// assigning a value
System.out.println("element at first row and second column: " + arr[0][1]);
}
}
24
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
element at first row and second column: 2
25
Q1. Addition of two matrix
Q2. Subtraction of two matrix
Q3 Multiplication of two matrix
Wap enter numbers in 2*2 matrix and show their sum
import java.io.*;
import java.util.*;
public class twod
{
public static void main(String[] args)
{
int a[][] = new int[2][2];
int sum=0;
Scanner in = new Scanner(System.in);
System.out.println("enter values from keyboard");
26
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
a[i][j] = in.nextInt();
sum = sum+ a[i][j];
}
System.out.println(" ");
System.out.println("n");
}
System.out.println(sum);
}
}
27
WAP to implement array of objects
import java.lang.*;
class emp
{
private String name;
private double salary;
public emp(String n, double s)
{
name= n;
salary=s;
}
public void print()
{
System.out.println(name+” “+ salary);
28
}
}
public class emptest
{
public static void main(String args[])
emp staff[]=new emp[3];
staff[0]=new emp (“Ajay”,3000);
staff[1]=new emp (“Ray”,2000);
staff[2]=new emp (“Jay”,5000);
for(int i=0;i<3;i++)
staff[i].print();
}
}
Output
Ajay 3000
Ray 2000
Jay 6000
29
WAP enter name of 2 student and display them.
import java.io.*;
import java.util.*;
public class string
{
public static void main(String[] args)
{
System.out.println("nn");
String a[][] = new String[2][2];
Scanner in = new Scanner(System.in);
System.out.println("enter 2 names from keyboard");
for(int i = 0; i < 2; i++)
30
{
for(int j = 0; j < 2; j++)
{
a[i][j] = in.nextLine();
}
}
for(int i = 0; i <2; i++)
{
for(int j = 0; j < 2; j++)
{
System.out.println("n"+a[i][j]);
}
System.out.println("n");
}
} }
31
Java Program Example - Concatenate String
import java.util.Scanner;
public class concat
{
public static void main(String args[])
{
String str1, str2;
Scanner scan = new Scanner(System.in);
System.out.print("Enter First String : ");
str1 = scan.nextLine();
System.out.print("Enter Second String : ");
str2 = scan.nextLine();
System.out.print("Concatenating String 2 into String 1...n");
32
str1 = str1.concat(" "+str2);
System.out.print("String 1 after Concatenation is " +str1);
}
}
//output Ajay Singh
33
/* Java Program Example - Find Length of String */
import java.util.Scanner;
public class length
{
public static void main(String args[])
{
String str;
int len;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Your Name : ");
str = scan.nextLine();
len = str.length();
System.out.print("Length of Entered String is " + len);
}
}
34
/* Java Program Example - Count Occurrence of Word in Sentence */
import java.util.Scanner;
public class count
{
public static int countWords(String str)
{
int count = 1;
for(int i=0; i<=str.length()-1; i++)
{
if(str.charAt(i) == ' ' && str.charAt(i+1)!=' ')
{
count++;
}
}
return count;
}
public static void main(String args[])
{
String sentence;
35
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Sentence : ");
sentence = scan.nextLine();
System.out.print("Total Number of Words in Entered Sentence is " + countWords(sentence)); // sentence pass to function
}
}
Example
Print Odd and Even Numbers from an Array
public class oddeven{
public static void main(String args[]){
int a[]={2,3,6,7,4,5};
System.out.println("Odd Numbers:");
for(int i=0;i<a.length;i++){
36
if(a[i]%2!=0){
System.out.println(a[i]);
}
}
System.out.println("Even Numbers:");
for(int i=0;i<a.length;i++){
if(a[i]%2==0){
System.out.println(a[i]);
}
}
}}
//odd 3 7 5 even 2 6 4
37
Example to multiply two matrices of 3 rows and 3 columns.
*/
public class multiply{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
38
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
}
}}
/*Output:
6 6 6
12 12 12
18 18 18
*/
39
WAP enter 5 number in array and find largest number.
import java.io.*;
class largest1
{
public static void main(String args[]) throws IOException
{InputStreamReader reader =new InputStreamReader(System.in);
BufferedReader in =new BufferedReader (reader);
int i,large;
int mark[]= new int[5];
String text;
large=0;
for(i=0;i<5;i++)
{System.out.println("enter a number");
40
text=in.readLine();
mark[i]=Integer.parseInt(text);
if(mark[i]>large)
large=mark[i];
}
System.out.println("largest value =" +large);
}
}
41
WAP to keep entering the strings till “STOP” is entered
import java.util.Scanner;
class string1
{
public static void main(String args[])
{
Scanner b= new Scanner(System.in);
String s[]=new String[10];
int i=0;
do
{s[i]=b.nextLine();
i++;
}while(!(s[i-1].equalsIgnoreCase("STOP")));
for(int y=0;y<i;y++)
System.out.println(s[y]);
}}
42

More Related Content

6_Array.pptx

  • 1. 1 Java Programming Course Code: IT 201 MODULE – I Dr. Sheenu Rizvi Asstt. Professor Dept Of CSE/IT ASET AUUP Lucknow.
  • 2. 2 Array An array is a collection of similar data types. Array is a container object that hold values of homogeneous type. It is also known as static data structure because size of an array must be specified at the time of its declaration. Array starts from zero index and goes to n-1 where n is length of the array. In Java, array is treated as an object and stores into heap memory. It allows to store primitive values or reference values. Array can be single dimensional or multidimensional in Java. Single Dimensional Array Single dimensional array use single index to store elements. You can get all the elements of array by just increment its index by one. Array Declaration Syntax : datatype[] arrayName; or datatype arrayName[];
  • 3. 3 Java allows to declare array by using both declaration syntax, both are valid. The arrayName can be any valid array name and datatype can be any like: int, float, byte etc. Example : int[ ] arr; char[ ] arr; int[ ][ ] arr; // two dimensional array. Initialization of Array Initialization is a process of allocating memory to an array. At the time of initialization, we specify the size of array to reserve memory area. Initialization Syntax 1) arrayName = new datatype[size] new operator is used to initialize an array. The arrayName is the name of array, new is a keyword used to allocate memory and size is length of array.
  • 4. 4 Ex. int size = 5, a[]; a= new int [size]; The size of an array using a variable array can be dynamically created at runtime. 1) We can combine both declaration and initialization in a single statement. Datatype[] arrayName = new datatype[size] Ex Int a[ ] = new int [3];
  • 5. 5 Create An Array Lets create a single dimensional array. class Demo { public static void main(String[] args) { int[] arr = new int[5]; for(int x : arr) { System.out.println(x); } } } Output 0 0 0 0 0
  • 6. 6 In the above example, we created an array arr of int type and can store 5 elements. We iterate the array to access its elements and it prints five times zero to the console. It prints zero because we did not set values to array, so all the elements of the array initialized to 0 by default. Set Array Elements We can set array elements either at the time of initialization or by assigning direct to its index. int[] arr = {1,2,3,4,5}; Here, we are assigning values at the time of array creation. It is useful when we want to store static data into the array. or arr[1] = 105 Here, we are assigning a value to array’s 1 index. It is useful when we want to store dynamic data into the array.
  • 7. 7 Array Example Here, we are assigning values to array by using both the way discussed above. class Demo { public static void main(String[] args) { int[] arr = {1,2,3,4,5}; for(int x : arr) { System.out.println(x); }
  • 8. 8 // assigning a value arr[1] = 10; System.out.println("element at first index: " +arr[1]); } } Output 1 2 3 4 5 element at first index: 10
  • 9. 9 Accessing array element we can access array elements by its index value. Either by using loop or direct index value. We can use loop like: for, for- each or while to traverse the array elements. Example to access elements class Demo { public static void main(String[] args) { int[] arr = {10,20,30,40,50};
  • 10. 10 for(int i=0;i<arr.length;i++) { //length give number of element stored in array //length is not a method of the array object //rather length is an instance variable System.out.println(arr[i]); } System.out.println("element at first index: " +arr[1]); } } 10 20 30 40 50 element at first index: 20
  • 11. 11 public class arr { public static void main(String args[]) { int [] a= { 3, 34, 7, 9}; int len = a.length; //len = 4 System.out.println("length =" + len); } }
  • 12. 12 // The following code stores the numbers from 1 to 5 in a element in int array. public class arr { public static void main(String args[]) { int[] a =new int[5]; for ( int i=0; i<a.length; i++) { a[i]= i+1; System.out.println("value =" +a[i]); //output is value=1 //2 3 4 5 } } }
  • 13. 13 Creation of table public class arr { public static void main(String args[]) { int[] a =new int[11]; int b=2; for ( int i=0; i<a.length; i++) { a[i] = (i) * b; System.out.println(i + "*"+ b+ "=" +a[i]); } } }
  • 14. 14 The following for each iteration of the for loop Syntax : for(<data type> <variable name>:<array name>) { //code } Ex: public class arr { public static void main(String args[]) { int[] a ={3,4,7,9}; for ( int x: a ) { System.out.println(x); } }}
  • 15. 15 // WAP enter 5 values and display them. import java.util.Scanner; class array1 { public static void main(String args[]) { int i; //int a[]; //a=new int[5]; // initialize array with name of array i:e a in int a[] String []a=new String [5] ; // initialize at time of declaration Scanner ob= new Scanner(System.in); System.out.println("Enter values"); for(i=0;i<a.length;i++) //a.length give length of array
  • 16. 16 { a[i]=ob.nextLine(); // enter name from user //a[i]=ob.nextInt(); } for(i=0;i<a.length;i++) { System.out.println("values are "+a[i]) ; } } }
  • 17. 17 // Program for Linear Search import java.util.Scanner; // from user input include public class arr1 { public static void main(String args[]) { //int[] a = { 2, 4, 20,11, 40}; // an array not containing duplicates //int target = 20; // the element to be searched //from user input Scanner in = new Scanner(System.in); System.out.println("enter array"); int[]a=new int[8]; for(int b=0;b<a.length;b++) {
  • 18. 18 a[b]=in.nextInt(); } System.out.println("enter search element from keyboard"); int target= in.nextInt(); for( int i=0; i<a.length; i++) { if(a[i]==target) { System.out.println ( "Element found at index "+i); //output element found at index 2 break; // break should be omitted if the array contains duplicates } } } }
  • 19. 19 Program for Binary Search public class arr1 { public static void main(String args[]) { int[] a = {2, 7, 20, 35, 45, 50, 85}; // a sorted array not containing duplicates int target = 35; // the element to be searched int left = 0; int middle; int right = a.length - 1; while (left <= right) { middle = (left + right) / 2; if (a[middle] == target) { System.out.println("Element found at index " + middle); //output element found at index 3 break; } else if (a[middle] < target) {
  • 20. 20 left = middle + 1; } else if (a[middle] > target) { right = middle - 1; } } } }
  • 21. 21 Multi-Dimensional Array A multi-dimensional array is very much similar to a single dimensional array. It can have multiple rows and multiple columns unlike single dimensional array, which can have only one row index. It represents data into tabular form in which data is stored into row and columns. Multi-Dimensional Array Declaration datatype[ ][ ] arrayName; Ex int[][] multipliers = new int[10][]; // second dimension is optional String[][] names = new String[5][];
  • 22. 22 Initialization of Array datatype[ ][ ] arrayName = new int[no_of_rows][no_of_columns]; The arrayName is the name of array, new is a keyword used to allocate memory and no_of_rows and no_of_columns both are used to set size of rows and columns elements. Like single dimensional array, we can statically initialize multi-dimensional array as well. int[ ][ ] arr = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
  • 23. 23 Example: class Demo { public static void main(String[] args) { int arr[ ][ ] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}}; for(int i=0;i<3;i++) { for (int j = 0; j < 5; j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } // assigning a value System.out.println("element at first row and second column: " + arr[0][1]); } }
  • 24. 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 element at first row and second column: 2
  • 25. 25 Q1. Addition of two matrix Q2. Subtraction of two matrix Q3 Multiplication of two matrix Wap enter numbers in 2*2 matrix and show their sum import java.io.*; import java.util.*; public class twod { public static void main(String[] args) { int a[][] = new int[2][2]; int sum=0; Scanner in = new Scanner(System.in); System.out.println("enter values from keyboard");
  • 26. 26 for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { a[i][j] = in.nextInt(); sum = sum+ a[i][j]; } System.out.println(" "); System.out.println("n"); } System.out.println(sum); } }
  • 27. 27 WAP to implement array of objects import java.lang.*; class emp { private String name; private double salary; public emp(String n, double s) { name= n; salary=s; } public void print() { System.out.println(name+” “+ salary);
  • 28. 28 } } public class emptest { public static void main(String args[]) emp staff[]=new emp[3]; staff[0]=new emp (“Ajay”,3000); staff[1]=new emp (“Ray”,2000); staff[2]=new emp (“Jay”,5000); for(int i=0;i<3;i++) staff[i].print(); } } Output Ajay 3000 Ray 2000 Jay 6000
  • 29. 29 WAP enter name of 2 student and display them. import java.io.*; import java.util.*; public class string { public static void main(String[] args) { System.out.println("nn"); String a[][] = new String[2][2]; Scanner in = new Scanner(System.in); System.out.println("enter 2 names from keyboard"); for(int i = 0; i < 2; i++)
  • 30. 30 { for(int j = 0; j < 2; j++) { a[i][j] = in.nextLine(); } } for(int i = 0; i <2; i++) { for(int j = 0; j < 2; j++) { System.out.println("n"+a[i][j]); } System.out.println("n"); } } }
  • 31. 31 Java Program Example - Concatenate String import java.util.Scanner; public class concat { public static void main(String args[]) { String str1, str2; Scanner scan = new Scanner(System.in); System.out.print("Enter First String : "); str1 = scan.nextLine(); System.out.print("Enter Second String : "); str2 = scan.nextLine(); System.out.print("Concatenating String 2 into String 1...n");
  • 32. 32 str1 = str1.concat(" "+str2); System.out.print("String 1 after Concatenation is " +str1); } } //output Ajay Singh
  • 33. 33 /* Java Program Example - Find Length of String */ import java.util.Scanner; public class length { public static void main(String args[]) { String str; int len; Scanner scan = new Scanner(System.in); System.out.print("Enter Your Name : "); str = scan.nextLine(); len = str.length(); System.out.print("Length of Entered String is " + len); } }
  • 34. 34 /* Java Program Example - Count Occurrence of Word in Sentence */ import java.util.Scanner; public class count { public static int countWords(String str) { int count = 1; for(int i=0; i<=str.length()-1; i++) { if(str.charAt(i) == ' ' && str.charAt(i+1)!=' ') { count++; } } return count; } public static void main(String args[]) { String sentence;
  • 35. 35 Scanner scan = new Scanner(System.in); System.out.print("Enter a Sentence : "); sentence = scan.nextLine(); System.out.print("Total Number of Words in Entered Sentence is " + countWords(sentence)); // sentence pass to function } } Example Print Odd and Even Numbers from an Array public class oddeven{ public static void main(String args[]){ int a[]={2,3,6,7,4,5}; System.out.println("Odd Numbers:"); for(int i=0;i<a.length;i++){
  • 37. 37 Example to multiply two matrices of 3 rows and 3 columns. */ public class multiply{ public static void main(String args[]){ //creating two matrices int a[][]={{1,1,1},{2,2,2},{3,3,3}}; int b[][]={{1,1,1},{2,2,2},{3,3,3}}; //creating another matrix to store the multiplication of two matrices int c[][]=new int[3][3]; //3 rows and 3 columns //multiplying and printing multiplication of 2 matrices for(int i=0;i<3;i++){ for(int j=0;j<3;j++){
  • 38. 38 c[i][j]=0; for(int k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; }//end of k loop System.out.print(c[i][j]+" "); //printing matrix element }//end of j loop System.out.println();//new line } }} /*Output: 6 6 6 12 12 12 18 18 18 */
  • 39. 39 WAP enter 5 number in array and find largest number. import java.io.*; class largest1 { public static void main(String args[]) throws IOException {InputStreamReader reader =new InputStreamReader(System.in); BufferedReader in =new BufferedReader (reader); int i,large; int mark[]= new int[5]; String text; large=0; for(i=0;i<5;i++) {System.out.println("enter a number");
  • 41. 41 WAP to keep entering the strings till “STOP” is entered import java.util.Scanner; class string1 { public static void main(String args[]) { Scanner b= new Scanner(System.in); String s[]=new String[10]; int i=0; do {s[i]=b.nextLine(); i++; }while(!(s[i-1].equalsIgnoreCase("STOP"))); for(int y=0;y<i;y++) System.out.println(s[y]); }}
  • 42. 42