Results 1 to 8 of 8

Thread: Arrays

  1. #1
    swim_fan08 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Question Arrays

    Can someone please, help me correct my errors and get my while structure working?

    Errors I get:

    java:28: while expected do System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)");

    java:30: ')' expected num = scan.nextLine();

    Java Code:
    import java.util.Scanner;
    
    public class Occurrences
    {
       public static void main (String[] args)
       {
    	   Scanner num = new Scanner(System.in);
    
    	   //-----------------------------------------
    	   //Reads an arbitrary number of integers that
    	   //are in the range of 0 to 15 inclusive.
    	   //Counts how many occurrences of each are
    	   //entered.
    	   //----------------------------------------
    
          int[] array = new int[15];
          int index, num, next;
    
    		while (num >= 0 || num <= 15)
    		numbers[index] = next;
    		{
    		do System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)");
    		num = scan.nextLine();
    		}
    
    
          //---------------------------------------------------
    	  //display a histogram that shows the number of times
    	  //a number was entered in each of the following ranges:
    	  //0 - 5, 6 - 10, and 11 - 15. Show one asterisk for each
    	  //occurence entered.
    	  //----------------------------------------------------
    	  String[] asterisk={"0- 5: ","6- 10: ","11- 15:"};
          for (index=0;index<num;index++)
          {
    		  next=numbers[index];
    
    					if (next<5) asterisk[0] +="*";
    					else if (next<6) asterisk[1] +="*";
    					else if (next<15) asterisk[2] +="*";
    
    			}
    
    			for (index=0;index<3;index++)
     				System.out.println(asterisk[index]);
       }
    
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    do System.out.println...
    what is do? do what? that's the error.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I would just scrape your while loop and re-write it. You've got it completely ferschmuttered. For info on how to do while loops, please look here: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Best of luck.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by angryboy View Post
    do System.out.println...
    what is do? do what? that's the error.

    Yep, please copy-paste your error message to see here. No one don't know what the error is.

  5. #5
    swim_fan08 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class Occurrences
    {
       public static void main (String[] args)
       {
    	   Scanner scan = new Scanner(System.in);
    
    	   //-----------------------------------------
    	   //Reads an arbitrary number of integers that
    	   //are in the range of 0 to 15 inclusive.
    	   //Counts how many occurrences of each are
    	   //entered.
    	   //----------------------------------------
    
    
    	  final int LIMIT = 15;
    
          int[] array = new int[LIMIT];
          int num, next, index;
    
    		while (num < 0 || num > 15)
    		{
    
    		System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)");
    		num = scan.nextLine();
    
    		while (num !=16)
    		{
    
    		System.out.println ("Enter a number between 0 and 15. (Press 16 to quit the program)");
    		num = scan.nextLine();
    		}
    	}
    
    
          //---------------------------------------------------
    	  //display a histogram that shows the number of times
    	  //a number was entered in each of the following ranges:
    	  //0 - 5, 6 - 10, and 11 - 15. Show one asterisk for each
    	  //occurence entered.
    	  //----------------------------------------------------
    	  String[] asterisk={"0- 5: ","6- 10: ","11- 15:"};
          for (index=0;index<num;index++)
          {
    		  next=array[index];
    
    					if (next<5) asterisk[0] +="*";
    					else if (next<6) asterisk[1] +="*";
    					else if (next<15) asterisk[2] +="*";
    
    			}
    
    			for (index=0;index<array.size;index++)
     				System.out.println(asterisk[index]);
       }
    
    }
    I just edited and got fewer errors:

    java:32: incompatible types
    found : java.lang.String
    required: int
    num = scan.nextLine();

    java:38: incompatible types
    found : java.lang.String
    required: int
    num = scan.nextLine();

    java:60: cannot find symbol
    symbol : variable size
    location: class int[]
    for (index=0;index<array.size;index++)

    My program is suppose to ask a user for a number between 0-15 inclusive, not allow the user to enter invalid data, when the user enters invalid data its suppose to display a histogram of the occurrences of the numbers entered and its suppose to use a while structure. And since I am using Scanner as the name of the class it should not be used as a variable that is receiving a value from the input stream. Thats why I am using


    Java Code:
    int num;
       num = scan.nextLine()
    Which is were some of my errors are coming from. :(

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    You want to take a look at the Scanner API.
    -- Scanner.nextLine:String
    -- Scanner.nextInt:int

    You want to get an int, so you should use nextInt instead.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Your "while" conditions are not correct:
    Java Code:
    while (num [B][COLOR="Red"]<[/COLOR][/B] 0 || num [B][COLOR="red"]>[/COLOR][/B] 15)
    The above indicates that it should loop while num is less than zero and greater than 15: I think you want the following:
    Java Code:
    while (num [B][COLOR="Blue"]>=[/COLOR][/B] 0 || num [B][COLOR="blue"]<=[/COLOR][/B] 15)
    Now, the above code indicates that the "while" loop will work while num is greater or equal than zero or less or equal to 15.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Help!! With arrays
    By ookie833 in forum New To Java
    Replies: 8
    Last Post: 12-14-2008, 12:57 AM
  2. Arrays
    By TheRocket in forum New To Java
    Replies: 6
    Last Post: 12-10-2008, 06:00 PM
  3. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03:15 AM
  4. 2D Arrays
    By Major90 in forum New To Java
    Replies: 6
    Last Post: 11-06-2008, 02:08 PM
  5. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •