Hello
I am just learning Java. I have some elmentary questions. In my code below there are 3 errors that occur:
- How do I declare a string array of size 5 in Java correctly?
- How do I cin :p, take in a string array element. Ie, in C++ cin >> string, how would I write that in Java?
package similie;
import java.util.Scanner;
public class Main {
public Main() {
}
public static void main() {
int cAdjective, cNoun;
int max;
String adjective[5]; // error occurs here
String noun[];
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of adjectives(must be less than 5): ");
cAdjective = scan.nextInt();
System.out.println("Enter number of nouns(must be less than 5): ");
cNoun = scan.nextInt();
for (int i=0; i<cAdjective; i++) {
adjective[i] = scan.nextString(); // I get an error here
}
for (int i=0; i<cNoun; i++) {
noun[i] = (String)System.in.read(); // I get an error here
}
if (cNoun >= cAdjective) max = cNoun;
else max = cAdjective;
System.out.println("max ="+max);
for (int i=0; i<cAdjective; i++) {
for (int j=0; j<cNoun; j++) {
System.out.println(adjective[i]+"as"+noun[j]);
}
}
// do I write return 0; ??
}
}