| | |
Noob: Declare & Initialise String Array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
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[i], how would I write that in Java?
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[i], how would I write that in Java?
Java Syntax (Toggle Plain Text)
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; ?? } }
-1
#2 27 Days Ago
Guess you have "language fever" you mix few things but it is not so bad. See below my corrections
Java Syntax (Toggle Plain Text)
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; ?? } }
Last edited by peter_budo; 27 Days Ago at 6:00 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- comparing stings to string array values (C++)
- How do I convert a vector to a String array ? (Java)
- Declaring string array, initializing later... (C++)
- Copy string into an array (C#)
- ArrayList to multi-dimensional string array (C#)
- Geting elements from a String Array (C++)
Other Threads in the Java Forum
- Previous Thread: how do i go about building a network monitor usin java??
- Next Thread: Netbeans installation problem
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working






