Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:17:51+00:00 2026-06-13T11:17:51+00:00

I have no idea why my second for loop won’t execute.. It compiles but

  • 0

I have no idea why my second for loop won’t execute.. It compiles but when I run this it does not work ~_~

import java.util.Scanner;

public class ranges
{


 public static void main (String[] args)

  {

 Scanner scan = new Scanner (System.in);

       int size;
       int input;
       int count = 0;
       int occurence = 0;
       System.out.print ("Please enter the size of your array: ");
       size = scan.nextInt();
       int[] list = new int[size];



       for (int index = 0 ; index < list.length ; index++)
       {
           System.out.print ("Please enter an integer between 0 to 50: ");
           input = scan.nextInt();

           if ( input >= 0 && input <= 50 )
           {
               list[index] = input;
            }
           else
           {
               System.out.println ("Invalid output, please try again");
               index--;
           }
        }




       int right = (list.length)-1;
       int left = 0;

        for (int counter = left ; counter < list.length ; counter++)
        {
            while ( right >= left )
            {
                if ( list[left] == list[right] )
                {
                    occurence++;
                    right--;
                }
            }
           right = (list.length)-1;
           System.out.println ("The number " + list[left] + " was added " + occurence + "4 times");
        }

         for (int value : list)
        {
            System.out.print (value + " ");
        }
       ;








    }
}

My updated for loop to evaulate occurences

for (int left = 0 ; left < list.length ; left++)

{

        while ( right >= left )
        {
            if ( list[left] == list[right] )
            {
                occurence++;

            }
            right--;
        }


       System.out.println ("The number " + list[left] + " was added " + occurence + " times");
       right = (list.length)-1;
       occurence = 0;
    }

I have cleaned it up a bit, and now occurences are same as the inputs

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:17:52+00:00Added an answer on June 13, 2026 at 11:17 am

    You second for is also working. The problem is in while loop condition i.e. while ( right >= left ). If list[left] == list[right] is not equal, it goes in infinite loop as neigther right nor left changing in that case.

    I think, you need to change your while as below(move right-- outside the if condition):

      while ( right >= left )
       {
         if ( list[left] == list[right] )
          {
            occurence++;
          }
          right--;
        }
    

    Two more issues:

    Please re-initialize occurence =0; before the while loop so that it counts occurence of each number and remove 4 from your System.out.println() e.g. below:

    for (int counter = left ; counter < list.length ; counter++)
    { 
      occurence = 0; //< initialize to 0
      while ( right >= left )
      {
          if ( list[left] == list[right] )
          {
             occurence++;
           }
               right--;
       }
       right = (list.length)-1;
           //remove 4 after "occurance +"
       System.out.println ("The number " + list[left] + 
                                                " was added " + occurence + " times");
     }
    

    EDIT: working sample with HashMap:

           Map<Integer, Integer> scannedNums = new HashMap<Integer, Integer>();
            for (int counter = left ; counter < list.length ; counter++)
            {
                if(scannedNums.get(list[counter]) == null){
                    scannedNums.put(list[counter], 1);
                }else{
                    int currentCount = scannedNums.get(list[counter]);
                    scannedNums.put(list[counter], currentCount+1);
                }
            }
    
            Set<Integer> nums = scannedNums.keySet();
            Iterator<Integer> numIter = nums.iterator();
            while(numIter.hasNext()){
                int number = numIter.next();
                System.out.println ("The number " + number + 
                        " was added " + scannedNums.get(number) + " times");
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This script works, but only if I run it twice. Anyone have an idea
I have this code, but i want in second loop a decrease of $p
I have no idea why this is happening. The error I get is the
How can i synchronize the time (second , hour). i have this int minuto
In C I had working code but have no idea why it worked, so
So, I have the following line of code, that does a for loop and
Anyone have idea about how to bind Boolean in sqlite with objective - c.
I have idea to write errors from my application to the Windows Event Viewer
Anyone have idea how to pass value from iPhone application to the REST-Webservice using
Any body have idea how to put controls dynamically in asp.net 3.5 ? if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.