#How to change a `Float` into a `float[]`
72 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @shadow schooner! Please use
/closeor theClose Postbutton 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.
also its in a for loop where the counter = i
comissionSchedule is a float[]
that makes it not an issue. you're doing[i]on it, so you need afloatthere
ifcomissionScheduleitself was afloat, you'd have issues
also, you have no Float here. Float.parseFloat yields a float.
But I'm getting an error on the comissionSchedule[i] = ... ?
It's Type mismatch: cannot convert from float to float[]
that means comissionSchedule[i] is a float[], so comissionSchedule is a float[][]
yes correct sorry
(btw, the right spelling is "commission", if you aren't aware)
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.
yea i've spelled it like 3 different ways in my code so far so oops
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
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);
}
}
}
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
is commissionInput supposed to be the size of the array?
commissionInput is supposed to be the values inside of the array
oh yeah yeah misread
sry ik the code is awful
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
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
because it has to be a string to check if it's "q"
oh right
bc if "q" it stops
you want that to work in the middle of this thing?
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
theoretically yeah
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
is this why you said the while loop doesn't work?
at the beginning
no, != doesn't work for strings
equalsIgnoreCase
so 2 reasons why it wouldn't work, the != is the one i noticed fist
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
just use if and break here, it'll end up being much cleaner
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.
right after every input, check if it's q, if it is, break accordingly
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
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?
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
So then how do I check if it's a "q", and then break if so, but if not, change it into an int?
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);
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.