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; ??
        
    }
    
}

Guess you have "language fever" you mix few things but it is not so bad. See below my corrections

package similie;

import java.util.Scanner;


public class Main {
    
    public Main() {
    }
    
    public static void main(String[] args) {//missing parameters in main method
        
        int cAdjective, cNoun;
        int max;
        String[] adjective; // wrong declaration
        String[] noun;
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter number of adjectives(must be less than 5): ");
        cAdjective = scan.nextInt();
        adjective = new String[cAdjective];       
        
        
        for (int i=0; i<cAdjective; i++) {
             adjective[i] = scan.next();//no such method scan.nextString();
        }
        
        System.out.println("Enter number of nouns(must be less than 5): ");
        cNoun = scan.nextInt();
        noun = new String[cNoun];
        for (int i=0; i<cNoun; i++) {
            noun[i] = scan.next(); // You never initialized array size plus wrong method used
        }
        
        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; ??
        
    }
    
}

in java...
this is the correct initializing of array...

sample:

int numArray[]={5,3,4,6,90};
or
int numArray[]=new int[6];

Small point: although both forms are legal, most Java coding standards prefer

int[] numArray

rather than

int numArray[]

the logic being that its an array of ints, not an array of numArrays

One may replace peter_budo's code 38-44 with the following c-style code:

System.out.printf("max=%2d", max);
        
        for (String s: adjective)
           for(String n: noun)
           System.out.printf("\n%s as %s",s , n);

I also tried to use this style in initiating the string array noun, e.g.

replace :

for (int i=0; i<cNoun; i++) 
        noun[i] = scan.next();

by

for (String s: noun)
s=scan.next();

which does not work. All the elements remain null after the assignments by loop. It seems that in c_style format, one may not use the elements as left values. I need help to explain this issue.
Attached please find the code I modified

The for-each loop

for (String s : array) {
   s = something

is equivalent to

for (int i = 0; i < array.length; i++) {
   String s = array[i];
   s = something; // reassigns local variable, no effect on array

JamesCherrill, thank you for your clear explaination.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.