#How to change a `Float` into a `float[]`

72 messages · Page 1 of 1 (latest)

shadow schooner
#

I've got this bit of code. comissionInput = scnr.next(); comissionSchedule[i] = Float.parseFloat(comissionInput); but the 2nd line doesn't work because of missmatch types of float and float[] because comissionSchedule is a float[]. Does anyone know how to allow comissionInput to be put into comissionSchedule?

buoyant thistleBOT
#

This post has been reserved for your question.

Hey @shadow schooner! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

shadow schooner
river void
#

comissionSchedule is a float[]
that makes it not an issue. you're doing [i] on it, so you need a float there
if comissionSchedule itself was a float, you'd have issues

#

also, you have no Float here. Float.parseFloat yields a float.

shadow schooner
#

But I'm getting an error on the comissionSchedule[i] = ... ?

#

It's Type mismatch: cannot convert from float to float[]

river void
#

that means comissionSchedule[i] is a float[], so comissionSchedule is a float[][]

shadow schooner
#

yes correct sorry

river void
#

(btw, the right spelling is "commission", if you aren't aware)

shadow schooner
#

lol thanks

buoyant thistleBOT
# shadow schooner lol thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

shadow schooner
#

yea i've spelled it like 3 different ways in my code so far so oops

river void
#

so what are you trying to achieve with turning a float into a float[]? just wrap it in a 1-element array? that doesn't seem too useful
an array holds multiple values, but here you have just one

shadow schooner
#
String commissionInput = null;
            
int numOfCommissionInputs = 0;
System.out.println("Please enter the number of inputs you will have for the employee's comission schedule: ");
numOfCommissionInputs = scnr.nextInt();
            
while (commissionInput != "q") {
                
    float[][] commissionSchedule; // 2d array declaration
    commissionSchedule = new float[numOfCommissionInputs/2][2]; // 2d array initialization
    for (int i = 0; i < commissionSchedule.length; i++) {
        System.out.println("Please enter the minimum number of units sold, and then the corresponding price per unit. ");
        commissionInput = scnr.next();
        commissionSchedule[i] = Float.parseFloat(commissionInput);
        for (int j = 0; j < commissionSchedule[i].length; j++) {
                  commissionInput = scnr.next();                commissionSchedule[i][j] = Float.parseFloat(commissionInput);
        }
    }

}
river void
#

that while doesn't work btw

#

(we can get to that later)

shadow schooner
#

So if commissionInput is "q" i want it to quit, and I want the user to input the values into the 2d array

#

lol

#

ok

river void
#

is commissionInput supposed to be the size of the array?

shadow schooner
#

commissionInput is supposed to be the values inside of the array

river void
#

oh yeah yeah misread

shadow schooner
#

sry ik the code is awful

river void
#

when, then you'd have to just get input twice for 1 inner array
there's no reason to get input in the outer loop if you have an inner, there's nothing you need

shadow schooner
#

wait thats so true

#
System.out.println("Please enter the minimum number of units sold, and then the corresponding price per unit. ");
                    for (int j = 0; j < commissionSchedule[i].length; j++) {
                        commissionInput = scnr.next();
                        commissionSchedule[i][0] = Float.parseFloat(commissionInput);
                        commissionInput = scnr.next();
                        commissionSchedule[i][j] = Float.parseFloat(commissionInput);

                    }
#

like that?

#

wait

#

my brain isnt braining

river void
#

why not just use nextFloat

#

or nextDouble

shadow schooner
#

because it has to be a string to check if it's "q"

river void
#

oh right

shadow schooner
#

bc if "q" it stops

river void
#

you want that to work in the middle of this thing?

shadow schooner
#

I don't even need the j loop right? if the first input is going to be [i][0] and 2nd is going to be [i][1]?

#

yea

river void
#

but if you want to be able to break from any point in the loop, you have to check to break, a while won't work

#

a for could work but it'd be ugly as hell

shadow schooner
#

at the beginning

river void
#

no, != doesn't work for strings

shadow schooner
#

equalsIgnoreCase

river void
#

so 2 reasons why it wouldn't work, the != is the one i noticed fist

shadow schooner
#

i always do that

#

ugh

river void
# shadow schooner equalsIgnoreCase

sure that would work for the comparison, but because of your logic flow, the while won't be able to check the input while the inner fors are running, so that issue still persists

shadow schooner
#

yea true

#

how would you do if it you wanted it to check everytime?

river void
#

just use if and break here, it'll end up being much cleaner

shadow schooner
#

true

#

ok thank you thats so smart

buoyant thistleBOT
# shadow schooner ok thank you thats so smart

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

river void
#

for funzies, here's how you'd do it with a while

Scanner input = new Scanner(System.in);
int commissionCount = input.nextInt();
double[][] commissions = new double[commisionCount][2];

String val;
int i = 0;
int j = 0;
while (i < numCommissions && j < 2 && !(val = input.next()).equalsIgnorecase("q")) {
  commissions[i++][j++] = Float.parseFloat(val);
  if (j >= 2) j = 0;
}
#

look at that spaghetti

shadow schooner
#

lmao that is very gross

#

last thing

#

and ill stop bothering u

#
numOfCommissionInputs = scnr.next();
            if (numOfCommissionInputs.equalsIgnoreCase("q")) {
                break;
            }
            numOfCommissionInputs = Integer.parseInt(numOfCommissionInputs);
#

Why can't I do that last line?

river void
#

you can't change the types of variables

#

parseInt takes a String and returns an int, so there's no way you can convert it and reassign it to itself

shadow schooner
#

So then how do I check if it's a "q", and then break if so, but if not, change it into an int?

river void
#

you need a separate variable

#

also if you're using nested loops to check for q only once, then a single break; won't work. you'll need break label;, return; or System.exit(0);

shadow schooner
#

thank you so much

buoyant thistleBOT
# shadow schooner thank you so much

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.