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 7512413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:51:44+00:00 2026-05-29T23:51:44+00:00

Quick question, I found answers close to this but nothing that helps me. I

  • 0

Quick question, I found answers close to this but nothing that helps me. I want it to print out a percentage at the end of the code that has 4 numbers after the decimal point, and of course, using an int work work. But using floats gives me an error.

This code:

import java.util.Scanner;

public class HW2johnson_pp4 {
    public static void main(String args[]) {

        Scanner keyboard = new Scanner(System.in);
        System.out.printf("How many numbers will you enter?\n");
        float[] numbers = new float[keyboard.nextFloat()];

        System.out.printf("Enter " + numbers.length + " integers, one per line:\n");

        for (int i = 0; i <= numbers.length - 1; i++) {
            numbers[i] = keyboard.nextInt();
        }

        float sum = 0;
        for (int i = 0; i <= numbers.length - 1; i++) {
            sum += numbers[i];

        }

        System.out.printf("The sum is " + sum + "\n");

        System.out.printf("The numbers are:\n");

        for (int i = 0; i <= numbers.length - 1; i++) {
            float perc = (numbers[i] / sum);
            float perct = (perc * 100);
            System.out.printf(numbers[i] + " which is " + perct + "%% of the sum.\n");
        }
    }
}

Gives the error:

HW2johnson_pp4.java:8: possible loss of precision
found   : float
required: int
        float[] numbers = new float[keyboard.nextFloat()];
  • 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-05-29T23:51:45+00:00Added an answer on May 29, 2026 at 11:51 pm

    You can’t create an array of floats whose length is a floating-point value. That is to say, you can’t have an array with 2.7 elements.

    So the float within the length parameter is getting rounded, causing a loss of precision.

    You wanted keyboard.nextInt() there on line 8, and keyboard.nextFloat() below on line 13.

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

Sidebar

Related Questions

Quick question. feel like a noob but haven't found the right syntax for this
Quick question; I've googled around and found some answers already, but I'm a bit
Quick question about the TransactionScope object. Found this on the internet: When you access
I have one question. I made quick search on the site, but don't found
I have been working on a quick WinForms program. I found this SO question
I've looked for this question on stackoverflow, but didn't found a really good answer
A rather quick question... I can't figure out why this loop never ends... #include
I've found answers to this question for many programming languages, except for C, using
I ran a quick google and SO search and found similar questions but none
Quick question (and sorry if it's already been asked, I have looked but couldn't

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.