Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Arrays, code counts how many numbers in specified range

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Arrays, code counts how many numbers in specified range

    I'm very new to programming, and I would greatly appreciate some help with this. The following code is supposed to count how many numbers are in a specified range, 4 to 17, in the array.


    The code I have written (which could be totally off for all I know) is right here:

    import java.io.*;
     
    public class NumberInRange {
    	public static void main(String[] args) {
     
    		int list[] = {14, 1, 22, 17, 36, 7, -43, 5};
    		int min = 4;
    		int max = 17;
    		int count = 0;
    		countInRange();
    	}
     
    		public static int countInRange (int [] list) {
     
    			for (int i = 0; int i < list.length; i++) {
     
    				if (list[i] >= min && list[i] <= max) {
    				count++;
    				}			
    			}
    			return count;
    		}	
    }

    Any help would be greatly appreciated! I do not know what the errors mean, but here are what the four errors say:


    NumberInRange.java:19: '.class' expected
    for (int i = 0; int i < list.length; i++) {
    ^
    NumberInRange.java:19: > expected
    for (int i = 0; int i < list.length; i++) {
    ^
    NumberInRange.java:19: ')' expected
    for (int i = 0; int i < list.length; i++) {
    ^
    NumberInRange.java:19: illegal start of expression
    for (int i = 0; int i < list.length; i++) {
    ^


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Arrays, code counts how many numbers in specified range

    Here are the issues I am seeing:
    1) The integer variables min, max and count are declared locally inside of the main method but you try to access them directly in the countInRange method.
    2) When you call the countInRange method inside of the main method, you do not supply a parameter; which it is expecting.

    Here are some links to help you understand the kinds of variables and an idea of what variable scope is:
    Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Variable Scope : Variable ScopeLanguageJava Tutorial

  3. The Following User Says Thank You to KucerakJM For This Useful Post:

    omarjordan (April 18th, 2012)

Similar Threads

  1. if problem with range of numbers
    By deependeroracle in forum Loops & Control Statements
    Replies: 3
    Last Post: February 17th, 2012, 05:33 AM
  2. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 03:07 PM
  3. help w/ storing/scanning numbers in two dimensional arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2010, 11:56 PM
  4. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM
  5. Conversions of Numbers in Arrays
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 1st, 2010, 07:59 PM

Tags for this Thread