Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2008, 10:48 AM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
ReV13 is on a distinguished road
Default Need help with a loop
Hi, Im pretty inexperienced with java and have run into a bit of a problem.

Basically i need to make a loop starting with -10 that prints out a value after 0.1; 0,11; 0,12; 0,13 etc. until it reaches 10.

thx
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-01-2008, 11:08 AM
miss.meli's Avatar
Member
 
Join Date: Nov 2008
Location: Austin, TX
Posts: 17
Rep Power: 0
miss.meli is on a distinguished road
Exclamation
Originally Posted by ReV13 View Post
that prints out a value after 0.1; 0,11; 0,12; 0,13 etc. until it reaches 10.
I understand how you want to loop to go from -10 to 10 but I think I'm still confused on how you want it incremented.

Meli
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-01-2008, 11:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
You say that you want to print decimal values?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-01-2008, 11:33 AM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
right,
i can't understand what you want to achieve..
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-01-2008, 12:02 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 486
Rep Power: 2
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Default
Maybe you meant this:
Code:
               double num=0.1D;
		for(int i=-10;i<=10;i++){
                        System.out.println(num);
			num+=0.01D;
			
		}

Last edited by serjant; 12-01-2008 at 12:05 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-01-2008, 01:23 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
There are lots of ways to do that, but not much clear what our thread starter wants.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-01-2008, 02:15 PM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
ReV13 is on a distinguished road
Default
Sorry, I tried to be as clear as I could but im kinda new to java terminology (english isnt my native language eighter):P

Anyways after being stuck for a week, i finally came up with this :

Quote:
int num = -1;
ArrayList <Double> v = new ArrayList<Double>();

for(double n = 0.1; n < 5;n = n + 0.01 ){
v.add(n);
}

for (double i = -10; i <= 10; i = i + v.get(num)) {
num = num + 1;
System.out.println(i);
}
any ways this could be done simpler?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-01-2008, 03:00 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default hhhmmm...
Let's see...
  • The first for statement is to populate 5 elements in the v arraylist
  • The second for statement... I have no clue what this is supposed to do. To begin with, it won't compile... v.get(-1) will make the compiler upchuck big time.
  • What do you want to do with the second for statement? Print the values of the arraylist?

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-01-2008, 03:10 PM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
ReV13 is on a distinguished road
Default
dno, the code works perfectly in eclipse
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-01-2008, 03:14 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
Code:
for (double i = -10; i <= 10; i = i + v.get(num)) {
num = num + 1;
System.out.println(i);
}
what does the second for loop prints?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-01-2008, 03:28 PM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
ReV13 is on a distinguished road
Default
The first loop is to make a "step" to get the second loop running properly. Second one is to print out the actual values I need(-10; -9.9; -9.790...01; -9.670...02; etc.)
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-01-2008, 03:29 PM
Member
 
Join Date: Nov 2008
Posts: 34
Rep Power: 0
beginner21 is on a distinguished road
Default
is your program prints what you want?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-01-2008, 03:30 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default WOW... head spinning...
Let's see...
  • You don't know what the second for loop's task is
  • ... and you say it's working perfectly... huh... then what is this post for?

If you want help, please explain the problem you're having, because it would appear that you want help for something that is working correctly and you don't have a clue as to how it works.

CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-01-2008, 03:44 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default take something back...
Independently of the questions and doubts stated in the above posts, I would like to correct a statements that I made:
Quote:
To begin with, it won't compile... v.get(-1) will make the compiler upchuck big time.
The code would probably compile, but would it fail at run time... and the program is the one that would upchuck.
Sorry if this caused any misunderstandings (haven't had my morning coffee yet).
Clarification: the above questions/doubts still stand.

CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 12-01-2008, 05:25 PM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
ReV13 is on a distinguished road
Default
Basically, I managed to come up with a working (tho crude) solution to my problem after I made this thread.

I was just wondering if I could simplify the code. Thx for the help.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 12-01-2008, 05:48 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
Code:
int num = -1
Why is int Num initialized to -1? Try making it equal to zero.. that may be what you are looking for.

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with While Loop mrdestroy New To Java 14 10-20-2008 03:29 PM
while loop Unknown1369 New To Java 5 07-08-2008 11:15 AM
How to use Do While loop Java Tip java.lang 0 04-17-2008 08:45 PM
How to use While loop Java Tip java.lang 0 04-17-2008 08:44 PM
do...while loop eva New To Java 16 01-31-2008 07:44 AM


All times are GMT +2. The time now is 06:12 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org