Ok, I have been trying to write a program that will read in a list of positive integers (including zero) and display some statistics regarding the integers.

This is what I have to do,

After you have placed all of the integers into an array, your program should perform the following tasks, in this order: 

1. Display the count of how many numbers were read 


2. Display the smallest number in the array 


3. Display the largest number in the array 


4. Display the median (the median is the integer that is stored in the middle position of the array) 


5. Display the average of all the numbers 


6. Allow the user to search the array for a specified value. 
First, ask the user to enter an integer to search for. 

Next, search the array to determine if the given integer is in the array.

If the integer is not found, display a message stating that the integer is not in the list. 

If the integer is found, then display the position number of where you found the integer. If the integer happens to be in the array more than once, then you only need to tell the first position number where you found it.

After performing the search, ask the user if he/she wants to search for more integers. Continue searching for numbers until the user answers "N".

And this is what I have so far,

import java.util.*;

public class ListStats {

//This is for the display part	
	public static void main(String[] args) {
		
		Scanner kbd = new Scanner(System.in);
		
		int numsList = 0;
	
		System.out.println("The amount of numbers read are: ");
		
		System.out.println("The smallest number found was: " + );
		
	}
		
//This is for getting the numbers from the user and putting thm in a array	
	public static int getNum(long[] num)
	{
		for (int i = 0; i < num.length; i++)
	{
			Scanner kbd = new Scanner(System.in);
			int nums;
			
			System.out.println("Enter a list of number ranging from 0 to 100 and put them from smallest to largest; ");			
			nums = kbd.nextInt();
			
			long []list = new long [nums];
			
		
	}
		return nums;
	}
	
//This is for finding the largest value	
	public static int findMax(int[] nums) 
	{
		int i = nums[0];
		
		for (int j = 1; j < nums.length; j++) 
		{
            if (nums[j] > i) 
            {
            	i = nums[j];
            }
        }

        return i;

	}

//This is for finding the smallest value
	public static int findMin(int[] nums) 
	{        
        
        int a = nums[0];

 
        for (int j = 1; j < nums.length; j++) 
        {
            if (nums[j] < a) 
            {
                a = nums[a];
            }
        }

        return a;
    }

//This is for finding the a specified value
	  public static int findVal(int[] nums, int val) 
	  {
	        
	        for (int b = 0; b < nums.length; b++) 
	        {
	            if (nums[b] == val) 
	            {
	                return b;
	            }
	        }
	        
	        return -1;
	    }

So my questions are, how do I put all the methods in the display part?

And how do I finish the 6th part?

And what am I doing wrong?

Thanks

Dani AI

Generated

Common problems in the posted code: the input method creates a new array inside the loop and returns a variable that never exists outside the method; types are mixed (long[] vs int[]); findMin uses the wrong index assignment; and multiple Scanner objects are created. Methods are called from main simply by passing the filled array (for example int min = findMin(arr);). As suggested, use a loop for repeated searching; 's idea of a do/while for re-asking is fine but the posted sample has a few syntax/logic errors (missing semicolons, misuse of a boolean variable).

A compact, working pattern is shown below: read tokens into an ArrayList (stop on a negative number or a sentinel like N), convert to an int[], sort for median, compute min/max/average, then run a search loop that asks repeatedly until the user types N.

import java.util.*;

public class ListStats {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter non-negative integers; type a negative number or N to finish:");
        List<Integer> list = new ArrayList<>();
        while (sc.hasNext()) {
            String t = sc.next();
            if (t.equalsIgnoreCase("N")) break;
            try {
                int v = Integer.parseInt(t);
                if (v < 0) break;
                list.add(v);
            } catch (NumberFormatException ex) {
                System.out.println("Not an integer; enter an integer or N to finish.");
            }
        }

        if (list.isEmpty()) {
            System.out.println("No numbers provided.");
            return;
        }

        int[] arr = list.stream().mapToInt(Integer::intValue).toArray();
        Arrays.sort(arr);

        int count = arr.length;
        int min = arr[0];
        int max = arr[count - 1];
        int median = arr[(count - 1) / 2]; // lower-middle if count is even
        double average = Arrays.stream(arr).average().orElse(0.0);

        System.out.println("Count: " + count);
        System.out.println("Min: " + min);
        System.out.println("Max: " + max);
        System.out.println("Median (middle element): " + median);
        System.out.printf("Average: %.2f%n", average);

        // repeated search
        while (true) {
            System.out.print("Enter integer to search (or N to quit): ");
            String s = sc.next();
            if (s.equalsIgnoreCase("N")) break;
            try {
                int key = Integer.parseInt(s);
                int pos = -1;
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] == key) { pos = i; break; }
                }
                System.out.println(pos == -1 ? key + " is not in the list." : key + " found at index " + pos);
            } catch (NumberFormatException ex) {
                System.out.println("Type an integer or N to quit.");
            }
        }
    }
}

Notes and small cautions: if the assignment explicitly requires the "median" to be the average of two middle values when the count is even, replace the median calculation accordingly. When reporting a position, state whether it is a zero-based index or a 1-based position to match grading expectations. Avoid creating multiple Scanners on System.in (use one and pass it around or keep it in main).

Recommended Answers

All 2 Replies

For number 6, you can use the scanner again (so that you know what the user wants to search for). This will most likely involve a while loop

I have not yet compiled this but I think this works on your 6th problem...

import java.io.*;
public class prob6{
     public static void main(String [] args) throws Exception{
         InputStreamReader Reader = new  InputStreamReader(System.in);
         BufferedReader Buffer = new BufferedReader(Reader);
         int []  inArr = {1,2,3,4,5,6}
         boolean found = false;
         int search = 0;
         String ans = null;
         do{
            boolean = false;
            System.out.print("What number you want to search in the array? :");
            search = Integer.parseInt(Buffer.readLine());
            for(int i= 0; i<=5; i++){
              if(inArr[i] == search){
                  System.out.println("Number  "+search+ " was found at location " + i);
                  found = true;
                  break;
               }
             }
             if(found == false){
                  System.out.println("Number " + search + " was not found");
             }
         System.out.println("Try Again[Y/N]");
         ans=Buffer.readLine();
         }while(ans.equals("N")==false){
      
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.