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

  • Home
  • SEARCH
  • 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 8474959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:45:59+00:00 2026-06-10T17:45:59+00:00

So I’m trying to get a program that reads a file, with the length

  • 0

So I’m trying to get a program that reads a file, with the length of the file as the first line. And then finds the index of the minimum and the maximum in the file. The annoying thing is, that for some reason, my findMin and findMax methods are not working. It seems as if they are bypassing the for loops. I’m sure I’m just making a silly mistake, but I can’t seem to pinpoint it.

import java.io.File;
import java.util.Scanner;

public class Driver00 {
    public static int minpos;
    public static int maxpos;
    public static double min;
    public static double max;
    public static int numitems;

    public static void main(String[] args) throws Exception {
        Scanner infile = new Scanner(new File("data.txt"));
        int numitems = infile.nextInt();
        double[] array = new double[numitems];
        for (int k = 0; k < numitems; k++) {
            array[k] = infile.nextDouble();
        }
        infile.close();
        int minPos, maxPos;
        minPos = findMin(array);
        maxPos = findMax(array);
        System.out.println("Minimum value: " + minPos);
        System.out.println("Maximum value: " + maxPos);
    }

    private static int findMin(double[] apple) {
        for (int x = 0; x < numitems; x++)
            // not activating
            if (x == 1) {
                if (apple[x] < apple[x - 1]) {
                    min = apple[x];
                    minpos = x;
                } else {
                    min = apple[x - 1];
                    minpos = x - 1;
                }
            } else {
                if (apple[x] < min) {
                    min = apple[x];
                    minpos = x;
                }
            }
        return minpos;
    }

    private static int findMax(double[] banana) {
        for (int x = 0; x < numitems; x++)
            // not activating
            if (x == 1) {
                if (banana[x] > banana[x - 1]) {
                    max = banana[x];
                    maxpos = x;
                } else {
                    max = banana[x - 1];
                    maxpos = x - 1;
                }
            } else {
                if (banana[x] > max) {
                    max = banana[x];
                    maxpos = x;
                }
            }
        return maxpos;
    }
}
  • 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-10T17:46:01+00:00Added an answer on June 10, 2026 at 5:46 pm

    public static int numitems; is ZERO that is why your for loop is not executed

    Why numitems ZERO?

    Because the integer your are getting at int numitems = infile.nextInt(); statement will be assigned to method local numitems variable not for the instance variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.