Noob: Declare & Initialise String Array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Noob: Declare & Initialise String Array

 
0
  #1
30 Days Ago
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?



  1. package similie;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Main {
  7.  
  8. public Main() {
  9. }
  10.  
  11. public static void main() {
  12.  
  13. int cAdjective, cNoun;
  14. int max;
  15. String adjective[5]; // error occurs here
  16. String noun[];
  17. Scanner scan = new Scanner(System.in);
  18.  
  19. System.out.println("Enter number of adjectives(must be less than 5): ");
  20. cAdjective = scan.nextInt();
  21.  
  22. System.out.println("Enter number of nouns(must be less than 5): ");
  23. cNoun = scan.nextInt();
  24.  
  25. for (int i=0; i<cAdjective; i++) {
  26. adjective[i] = scan.nextString(); // I get an error here
  27. }
  28.  
  29. for (int i=0; i<cNoun; i++) {
  30. noun[i] = (String)System.in.read(); // I get an error here
  31. }
  32.  
  33. if (cNoun >= cAdjective) max = cNoun;
  34. else max = cAdjective;
  35.  
  36. System.out.println("max ="+max);
  37.  
  38. for (int i=0; i<cAdjective; i++) {
  39. for (int j=0; j<cNoun; j++) {
  40. System.out.println(adjective[i]+"as"+noun[j]);
  41. }
  42. }
  43.  
  44.  
  45. // do I write return 0; ??
  46.  
  47. }
  48.  
  49. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,198
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #2
30 Days Ago
Guess you have "language fever" you mix few things but it is not so bad. See below my corrections
  1. package similie;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Main {
  7.  
  8. public Main() {
  9. }
  10.  
  11. public static void main(String[] args) {//missing parameters in main method
  12.  
  13. int cAdjective, cNoun;
  14. int max;
  15. String[] adjective; // wrong declaration
  16. String[] noun;
  17. Scanner scan = new Scanner(System.in);
  18.  
  19. System.out.println("Enter number of adjectives(must be less than 5): ");
  20. cAdjective = scan.nextInt();
  21. adjective = new String[cAdjective];
  22.  
  23.  
  24. for (int i=0; i<cAdjective; i++) {
  25. adjective[i] = scan.next();//no such method scan.nextString();
  26. }
  27.  
  28. System.out.println("Enter number of nouns(must be less than 5): ");
  29. cNoun = scan.nextInt();
  30. noun = new String[cNoun];
  31. for (int i=0; i<cNoun; i++) {
  32. noun[i] = scan.next(); // You never initialized array size plus wrong method used
  33. }
  34.  
  35. if (cNoun >= cAdjective) max = cNoun;
  36. else max = cAdjective;
  37.  
  38. System.out.println("max ="+max);
  39.  
  40. for (int i=0; i<cAdjective; i++) {
  41. for (int j=0; j<cNoun; j++) {
  42. System.out.println(adjective[i]+"as"+noun[j]);
  43. }
  44. }
  45.  
  46.  
  47. // do I write return 0; ??
  48.  
  49. }
  50.  
  51. }
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC