1 Assume we have a file that contains 10 words assume the wo

1) Assume we have a file that contains 10 words (assume the word “CCBC”) and each word is on a new line. What would the output be with the following code:

Scanner fileReader = new Scanner (new File (“sometext.txt”));

int count=0;

while (fileReader.hasNextLine( ) ) {

                System.out.println(count);

                count++;

}

System.out.println(“CCBC!!”);

2)Write a method that constructs a two-dimensional array of integers named “data” that should be declared with four rows and seven columns. Write a loop to initialize the third row of the data to store the numbers 1 through 7.

3) Write a method that constructs a two-dimensional array of integers with 5 rows and 10 columns. Fill the array with a multiplication table, so that array element [i][j] contains the value I * j. Use nested for- loops to build the array.

Solution


Answer for first question: Infinite loop is formed here as there is no advancement of line in the while loop. We need to use \"fileReader.nextLine() ; \" inside the while loop to make it a finite loop.

Answer for the second question is as follows:

public static void data(int rows, int columns){
int[][] number = new int[rows][columns];
  
for(int i = 0; i < rows; i++){
for(int j = 0; j <columns; j++) {

if(i == 3){
for(int fillNum= 1 ; fillNum <=7 ; fillNum++){
number[i][j] = fillNum;
}
}else{
number[i][j] = 0;
}
}

}
}
  
  
Answer for the third question is as follows:
  
public static void fillMultipliedData(int rows, int columns){
int[][] number = new int[rows][columns];
  
for(int i = 0; i < rows; i++){
for(int j = 0; j <columns; j++) {
number[i][j] = i*j;
}

}
  
}

1) Assume we have a file that contains 10 words (assume the word “CCBC”) and each word is on a new line. What would the output be with the following code: Scann
1) Assume we have a file that contains 10 words (assume the word “CCBC”) and each word is on a new line. What would the output be with the following code: Scann

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site