0

Here is my code, I tried to put a value for the zero-th(0th) position of the array1, but it disn't ask me to get the input value. and it takes space as a value and it is reflected on the out put.

Source Code:

import java.util.*;
public class PhraseOMatic {
 public static void main(String[] agrs){
  int x=1;
  int y=1;
  int z=1;
  int count=0;
  int temp;
  int ran1;
  int ran2;
  int ran3;
  String[] array1 = new String[x];
  String[] array2 = new String[y];
  String[] array3 =new String[z];
  Scanner inp = new Scanner(System.in);
  while(count<3) {
 System.out.println("Enter input");
  temp = inp.nextInt();
  if(count ==0){
   x=temp;
  array1 = new String[x];
  }
  if(count == 1) {
   y=temp;
  array2 = new String[y];
  }
  if(count == 2) {
  z=temp;
  array3 = new String[z];
  }
  count++;
}
System.out.print("Enter the values for the array1");
 for(int i =0; i<x; i++){
System.out.println("i value = "+i);
  array1[i] = inp.nextLine();
  }
int j=0;
System.out.println("Array1 Length = "+array1.length);
while(j<x){
 System.out.println(+j+") " +array1[j]);
j++;
}
j=0;
System.out.print("Enter the values for the array2");
 for(int i =0; i<y; i++){
  array2[i] = inp.nextLine();
  }
System.out.println("Array2 Length = "+array2.length);
while(j<y){
 System.out.println(+j+") " +array2[j]);
j++;
}
j=0;
System.out.print("Enter the values for the array3");
for(int i =0; i<z; i++){
  array3[i] = inp.nextLine();
  }
System.out.println("Array3 Length = "+array3.length);
while(j<z){
 System.out.println(+j+") " +array3[j]);
j++;
}
j=0;
System.out.println("Result : as below");
System.out.println("~~~~~~~~~~~~~~~~~");
count = 1;
while(count < 10) {
ran1 = (int) (Math.random() * x);
ran2 = (int) (Math.random() * y);
ran3 = (int) (Math.random() * z);
System.out.println(count +") " + array1[ran1] + " " +array2[ran2]+ " "+array3[ran3]);
count++;
}
}
}

Where did i make mistake? I couldn't figure out the mistake which i made? The for loop doesn't allow me to enter the value for the array1[0]

Output has been attachedOutput Image

Robert
  • 1
  • **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [ask] – Rob Sep 29 '22 at 11:45

0 Answers0