| | |
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 30 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; 30 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 |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie nonstatic notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






