java - Using the arrays index values from one for-loop in another -


i trying count number of times unique number exists in array, amount of indexes used subject amount of elements entered. operational besides 1.the first value isn't being taken consideration.the loop checking arrays.length -1 number 3 showing count of 1 though entered 3 twice. know best way fix run loop doesn't use arrays.length -1 wouldn't able compare entry 1 next if(a[i] == a[i + 1] && a[i] != 0) see if there more 1 occurrence of value. think best thing store count values in count method corresponding array value counted , loop outside of method possible ? can't see way of doing rather new java. may have guidance :)

import java.util.scanner;   public class prac_exeone  {         static int count  = 1;         static int numberused = 0; // static used show class wide variable , there 1 copy.         static int[] array =  new int [50]; // maximum elements inside array can used 10;         int size;                public int fillarray(int[] a)          {                  system.out.println("enter " + a.length + " nonnegative numbers.");                  system.out.println("mark end of list negative number.");                  scanner keyboard = new scanner(system.in);                   int next;                  int index = 0;                  next = keyboard.nextint();                  while ((next >= 0) && (index < a.length ))                    {                      numberused++;                      a[index] = next;                      index++;                      // print out each value of next                      system.out.println(next);                      next = keyboard.nextint();                      //system.out.println("number of indexes used" + numberused);                  }                  keyboard.close(); // close keyboard can't continue used.                  return index;         }               public int[] sort(int[] arrays)             {                  for(int = 0;i < arrays.length -1 ;i++ )                 {                      int store = 0;                     // move larger values right.                     if (arrays[i + 1 ] < arrays[i])                     {                         store = arrays[i];                         arrays[i] = arrays[i + 1];                         arrays[i + 1] = store;                     }                     // sort swapped smaller values left.                         for(int j = i; j > 1; j--)                         {                            if (arrays[j] < arrays[j - 1])                            {                            store = arrays[j];                            arrays[j] = arrays[j - 1];                            arrays[j - 1] = store;                            }                       }                 }                 return arrays;              }             public void count(int[] a)             {                     //for each element in array go through if conditons.                 system.out.println("n " + "count");                 for(int = 0;i < a.length -1;i++)                 {                        if(a[i] == a[i + 1] && a[i] != 0)                     {                         count++;                      }                     if(a[i] != a[i+1])                     {                         count = 1;                     }                     if (a[i] !=  0)                      {                     system.out.println(a[i] + " " + count);                      }                 }             }              public static void main(string[] args)              {                 prac_exeone score = new prac_exeone();                 score.fillarray(array);                 score.sort(array);                 score.count(array);               }         } 

input:

enter 50 nonnegative numbers. mark end of list negative number. 3 3 2 2 -2 

output:

n count 3 1 2 2 2 1 

desired result:

in nutshell want program count values correctly display value on left under n , amount of times in array 1 right underneath count

for sort function,

for (int j = i; j > 1; j--) 

should be

for (int j = i; j > 0; j--) 

i put system.out.println(arrays.tostring(array)); after call sort , saw 3 in beginning, led me fact you're skipping first element.

note there way more efficient sorting algorithms.

for count function, you're resetting count @ wrong time , printing often. changed follows:

public void count(int[] a) {     //for each element in array go through if conditions.     system.out.println("n " + "count");     (int = 0; < a.length - 1; i++)     {            if (a[i] != 0)         {             if (a[i] == a[i + 1])             {                 count++;             }             // if next element different, counted of             //   current element, print it, reset count             else             {                 system.out.println(a[i] + " " + count);                 count = 1;             }         }     }     // haven't processed last element yet,     if (a[a.length-1] != 0)         system.out.println(a[a.length-1] + " " + count); } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -