Solved

Java arrays and calculations

Posted on 2009-05-18
13
314 Views
Last Modified: 2012-05-07
I am trying to prompt the user to enter 26 integers(scores) and I want the integers to fall into the specefic category based on the  score the user entered. I want the user to be prompt to enter an integer 26 times. For example If  6 of the 26 scores falls in  the range1 category I would like the counter to be 6. For some reason when i ran it I got the wrong output data.
1 Student has a range below 24
0 Students have a range between 25-49
0 Students have a range between 50-74
and so on with the rest of all the values being 0
Whats wrong with my code?
import java.io.*;
import java.util.*;
 
public class myArray
{
    static Scanner console = new Scanner(System.in);
 
 
   public static void main(String[] args)
    	{
 
 
			int[] item = new int[26];
 
            int range1=0, range2=0, range3=0, range4=0, range5=0, range6=0, range7=0,range8=0;
            int index;
            System.out.println("Enter 26 numbers");
            for (index =0; index < item.length; index++)
                item[index] = console.nextInt();
 
 
                if (item.length <=24)
                    range1++;
                else if (item.length >= 25  && item.length <=49)
                    {
                    range2++;
				    }
                else if (item.length >= 50 && item.length <=74)
                    {
                    range3++;
				    }
                else if(item.length >= 75 && item.length <=99)
                    {
                   range4++;
			        }
                else if(item.length >= 100 && item.length <=124)
				    {
				    range5++;
				    }
				else if(item.length >= 125 && item.length <=149)
				    {
				    range6++;
				    }
                else if(item.length >= 150 && item.length <=174)
				    {
				    range7++;
				    }
                else if(item.length >= 175 && item.length <=200)
				    {
				   range8++;
				    }
 
            System.out.println( range1 + "Students have scores that range in the  24 and below category");
            System.out.println( range2 + "Students have scores that range between 25 and 49");
            System.out.println( range3 + "Students have scores that range between 50 and 74");
            System.out.println( range4 + "Students have scores that range between 75 and 99");
            System.out.println( range5 + "Students have scores that range between 100 and 124");
            System.out.println( range6 + "Students have scores that range between 125 and 149");
            System.out.println( range7 + "Students have scores that range between 150 and 174");
            System.out.println( range8 + "Students have scores that range between 175 and 200");
	   }
}

Open in new window

0
Comment
Question by:Valisha
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24417963
>                 if (item.length <=24)

you are comparing the wrong value, should be:

                if (item[index] <=24)

same for all the other comparisons

0
 

Expert Comment

by:struggle
ID: 24417969
Hi,

you seem to mixing your lengths and elements, item.length, if I remember correctly returns the number of elements in an array, you probably want to change all your tests to something like (for example)

                else if (item[index] >= 50 && item[index] <=74)

Cheers

0
 

Author Comment

by:Valisha
ID: 24418048
Hi again when I ran the program this time. I got in error message in the command window that says Exception thread "main" java.lang.ArrayIndexOutofboundsException: 26.
press any key to continue

The println output didn't even display.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Expert Comment

by:struggle
ID: 24418058
Please post your code in its new state thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 24418062
make sure you aren't incrementing index inside your loop

0
 

Author Comment

by:Valisha
ID: 24418070
import java.io.*;
import java.util.*;

public class myArray
{
    static Scanner console = new Scanner(System.in);


   public static void main(String[] args)
          {


                  int[] item = new int[26];

            int range1=0, range2=0, range3=0, range4=0, range5=0, range6=0, range7=0,range8=0;
            int index;
            System.out.println("Enter 26 numbers:");
            for (index =0; index < item.length; index++)
                item[index] = console.nextInt();


                if (item[index] <=24)
                    range1++;
                else if (item[index] >= 25  && item[index] <=49)
                    {
                    range2++;
                            }
                else if (item[index] >= 50 && item[index] <=74)
                    {
                    range3++;
                            }
                else if(item[index] >= 75 && item[index] <=99)
                    {
                   range4++;
                          }
                else if(item[index] >= 100 && item[index] <=124)
                            {
                            range5++;
                            }
                        else if(item[index] >= 125 && item[index] <=149)
                            {
                            range6++;
                            }
                else if(item[index] >= 150 && item[index] <=174)
                            {
                            range7++;
                            }
                else if(item[index] >= 175 && item[index] <=200)
                            {
                           range8++;
                            }
               


            System.out.println( range1 + "Students have scores that range in the  24 and below category");
            System.out.println( range2 + "Students have scores that range between 25 and 49");
            System.out.println( range3 + "Students have scores that range between 50 and 74");
            System.out.println( range4 + "Students have scores that range between 75 and 99");
            System.out.println( range5 + "Students have scores that range between 100 and 124");
            System.out.println( range6 + "Students have scores that range between 125 and 149");
            System.out.println( range7 + "Students have scores that range between 150 and 174");
            System.out.println( range8 + "Students have scores that range between 175 and 200");
         }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 24418075
need to include the if/else in your loop (by adding braces)

            for (index =0; index < item.length; index++)
            {
                item[index] = console.nextInt();


                if (item[index] <=24)
                    range1++;
                else if (item[index] >= 25  && item[index] <=49)
                    {
                    range2++;
                            }
                else if (item[index] >= 50 && item[index] <=74)
                    {
                    range3++;
                            }
                else if(item[index] >= 75 && item[index] <=99)
                    {
                   range4++;
                          }
                else if(item[index] >= 100 && item[index] <=124)
                            {
                            range5++;
                            }
                        else if(item[index] >= 125 && item[index] <=149)
                            {
                            range6++;
                            }
                else if(item[index] >= 150 && item[index] <=174)
                            {
                            range7++;
                            }
                else if(item[index] >= 175 && item[index] <=200)
                            {
                           range8++;
                            }
                  }
0
 

Author Comment

by:Valisha
ID: 24418126
The program is only accepting 1 number then the system.out.println executes instead of 26  numbers. How do I create a loop that goes through until all numbers are entered  instead of just one number.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 24418134
Actually, I don't see why a Java array is needed in this case. You can process each score right away when the user inputs, like this:

int N = 26;
int item = 0;

for (int i=0; i<N; i++) {
      item = console.nextInt();

      if (item <= 24)
            range1++;
      // etc...
}
0
 
LVL 20

Expert Comment

by:a_b
ID: 24418457
You need to change the item.length within the for loop to item[index]. The item.length will alway return 26 which is what you r result is in the current code.
0
 
LVL 92

Expert Comment

by:objects
ID: 24418700
> The program is only accepting 1 number then the system.out.println executes instead of 26  numbers. How do I create a loop that goes through until all numbers are entered  instead of just one number.

by making the change i posted above

0
 
LVL 92

Expert Comment

by:objects
ID: 24418722
import java.io.*;
import java.util.*;

public class myArray {
      static Scanner console = new Scanner(System.in);

      public static void main(String[] args) {

            int[] item = new int[26];

            int range1 = 0, range2 = 0, range3 = 0, range4 = 0, range5 = 0, range6 = 0, range7 = 0, range8 = 0;
            int index;
            System.out.println("Enter 26 numbers");
            for (index = 0; index < item.length; index++) {
                  item[index] = console.nextInt();

                  if (item[index] <= 24)
                        range1++;
                  else if (item[index] >= 25 && item[index] <= 49) {
                        range2++;
                  } else if (item[index] >= 50 && item[index] <= 74) {
                        range3++;
                  } else if (item[index] >= 75 && item[index] <= 99) {
                        range4++;
                  } else if (item[index] >= 100 && item[index] <= 124) {
                        range5++;
                  } else if (item[index] >= 125 && item[index] <= 149) {
                        range6++;
                  } else if (item[index] >= 150 && item[index] <= 174) {
                        range7++;
                  } else if (item[index] >= 175 && item[index] <= 200) {
                        range8++;
                  }
            }

            System.out
                        .println(range1
                                    + "Students have scores that range in the  24 and below category");
            System.out.println(range2
                        + "Students have scores that range between 25 and 49");
            System.out.println(range3
                        + "Students have scores that range between 50 and 74");
            System.out.println(range4
                        + "Students have scores that range between 75 and 99");
            System.out.println(range5
                        + "Students have scores that range between 100 and 124");
            System.out.println(range6
                        + "Students have scores that range between 125 and 149");
            System.out.println(range7
                        + "Students have scores that range between 150 and 174");
            System.out.println(range8
                        + "Students have scores that range between 175 and 200");
      }
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24419185
You don't need all those if statements:
import java.io.*;
import java.util.*;
 
public class myArray {
    static Scanner console = new Scanner(System.in);
 
    public static void main(String[] args) {
 
	int[] item = new int[8];
 
	int range1 = 0, range2 = 0, range3 = 0, range4 = 0, range5 = 0, range6 = 0, range7 = 0, range8 = 0;
	int index;
	System.out.println("Enter 26 numbers");
	for (index = 0; index < 26; index++) { // Don't actually use this magic number 26
	    int range = console.nextInt() / 25;
	    if (range < item.length) { // (discard out-of-bounds)
		item[range]++;
	    }
	}
	// Display however you see fit
	System.out.println(java.util.Arrays.toString(item));
    }
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example 17 51
Java string replace 11 54
Java Restore security prompts not working 10 57
Java: anonymous class 4 29
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question