This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Loosing Count in Array when I switch if statements  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I switch through the if statements I loose count and
I have to many results
Projected earnings with increments of $5,000 in increased sales
1 $95,000.00 $.00 $35,000.00 //Leaves first if loop
1 $100,000.00 $15,000.00 $50,000.00
2 $105,000.00 $15,750.00 $50,750.00
3 $110,000.00 $16,500.00 $51,500.00
4 $115,000.00 $17,250.00 $52,250.00//leaves the second if loop
4 $120,000.00 $18,000.00 $53,000.00
5 $125,000.00 $18,812.50 $53,812.50
6 $130,000.00 $19,625.00 $54,625.00
7 $135,000.00 $20,437.50 $55,437.50// This is where it should stop
8 $140,000.00 $21,250.00 $56,250.00
9 $145,000.00 $22,062.50 $57,062.50
10 $150,000.00 $22,875.00 $57,875.00

What can I do to prevent this?
 
Sheriff
Posts: 6775
469
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cherokee Nelson wrote:


How many times you want to iterate this loop?
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:How many times you want to iterate this loop?


And do you want to create a new DecimalFormat object every time you iterate the loop?

Winston
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cherokee Nelson wrote:What can I do to prevent this?


To be honest: dunno; but that's because I have absolutely no idea what you're trying to do.

You plainly know; and you've attacked it like a robot - "I have to do this...then I have to do this...then I have to do this..." - and you've ended up with a mess.

So:
#1. Stop Coding.
#2. Explain to us exactly what you want to happen. In English, not in "Java-ese".

and then we have a point to start from.

And oddly enough, in doing so, you might work out what's wrong.

Winston
 
Cherokee Nelson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to created a list for projected sales with %15 commission at %80 target sales + a %1.25 acceleration factor after target is met, and total earnings.
I created an Array List for the project but wanted to create an Array.
That way I could call on specific lines if desired not just the list
So I created a method calculates a %50 increase in sales, subtracts the original sales and divides by the$ 5000 increments to determines the rows [int projRange]. (the double sales is boxed and (un)boxed to get an integer value)
I had not noticed the decimal format was inside the loop. (Thank you)
 
Bartender
Posts: 706
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said that earnings.length is 9, so why do you say "it should stop" when count==7?
 
Marshal
Posts: 5995
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I created an Array List for the project but wanted to create an Array.
That way I could call on specific lines if desired not just the list


You can access individual elements in a List with
  list.get(index);
 
Knute Snortum
Marshal
Posts: 5995
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed you write lines like this:
  sales = (sales + projIncrements);
Although there is nothing wrong with this line, a more common and terse way to write it is
  sales += projIncrements
 
Knute Snortum
Marshal
Posts: 5995
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I had not noticed the decimal format was inside the loop.


One reason for that is your code is not properly formatted. Beginning programmers often skip formatting, but this is an error.
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cherokee Nelson wrote:I have to created a list for projected sales with %15 commission at %80 target sales + a %1.25 acceleration factor after target is met, and total earnings.


OK, well first: that sounds like three things to me; not one.

Second: you have an array called 'earnings', and a loop that appears to go through it; but nowhere in that loop do I see anything that refers to an earnings element - ie, 'earnings[count]' (and BTW, the usual name for a variable like that is 'i' - for "index" - not 'count').

And I still have no idea what "commission" or "target sales" or "acceleration factor" means, or how they're calculated, so you still haven't explained the problem.

You obviously have some idea of what's required, but you're not doing a good job of explaining it to us - and since your code plainly isn't working and you want help, that's where you need to start.

Assume we know nothing, and explain to us - line by line if need be - exactly what that list needs to contain.

Winston
 
Bartender
Posts: 5854
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing your if/elseif should look more like this:
 
Liutauras Vilda
Sheriff
Posts: 6775
469
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by reading tutorial Winston gave you.

Currently you have only 1 method there. Clearly you can make 3 or 4, might even 5 out of that code and make it more readable. Don't be scared to overload your code with amount of methods you use, you can revert it back always if you feel you went too far.
After all, in that manner your problem would be narrowed and divided into 4 or 5 different smaller problems. That way solving your exercise will require to think you about less things at a time.

1 method - salesProjections() <-- requires renaming as this name doesn't say much what it does.
2 method - printIntro() <-- that could be your line 3 inside only (that is clearly too much, but it might will help you to see the power of having methods in terms of code readability).
3 method - calucateSales(parameters list) <-- wraps the lines which appears in your code on lines 12-16, 20-24, 28-32.
4 method - printResults(parameters list) <-- wraps the statement which appears on lines 14 or 22 or 34.

Also, spend more time on choosing variable names. Many of those in your code as very confusing. i.e aFact, aFactor, minCom, com, fixed. As you see - non of those makes any sense taken out of code - while they actually should.

Don't rush yourself in writing code <- once again, read Winston's tutorial, there you'll find important information needed to solve such exercises, more precise - almost all exercises.

 
Carey Brown
Bartender
Posts: 5854
57
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also replace

with

And remove lines: 16, 24, and 36.
 
Cherokee Nelson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for %99 of your suggestions. The else if solution cleared up my issue and the list flows through without any glitches. If I use JavaRanch again I will be sure to completely explain my problem, my solution, and the issues I am having. Thanks again. I do hope the else if statement I used was not too much jargon for anyone
Please enter Mentulo,M.'s annual sales: $89750
Annual Sales Earned Commission Total Salary
$89,750.00 $.00 $35,000.00
Projected earnings with increments of $5,000 in increased sales
1 $94,750.00 $.00 $35,000.00
2 $99,750.00 $14,962.50 $49,962.50
3 $104,750.00 $15,712.50 $50,712.50
4 $109,750.00 $16,462.50 $51,462.50
5 $114,750.00 $17,212.50 $52,212.50
6 $119,750.00 $17,962.50 $52,962.50
7 $124,750.00 $18,771.88 $53,771.88
8 $129,750.00 $19,584.38 $54,584.38
9 $134,750.00 $20,396.88 $55,396.88

Thank you again Now I am off to clean up my work.
 
Knute Snortum
Marshal
Posts: 5995
156
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And be sure to post the code back here so we can all profit from its evolution.
 
Good night. Drive safely. Here's a tiny ad for the road:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!