Results 1 to 13 of 13
Thread: The function keep looping
- 03-08-2011, 06:36 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
The function keep looping
Hello.im having problem with my program.this program actually to con vert to different texts into binary.the problem is,it keep looping even after the second text was converted.anyone,plz help me.
XML Code:import java.util.*; import java.io.*; public class textconversion{ public static void main(String[] args) { do{ Scanner input = new Scanner(System.in); System.out.println ("Enter Text to Convert: "); String msg = input.nextLine(); byte[] bytes = msg.getBytes(); StringBuilder binary = new StringBuilder(); for (byte b : bytes) { int val = b; for (int i = 0; i < 8; i++) { binary.append((val & 128) == 0 ? 0 : 1); val <<= 1; } binary.append(' '); } System.out.println(); System.out.println(); System.out.println("Your Text"+" <" + msg + "> Successfully Converted Into Binary: "+binary); System.out.println(); } while (true); } }
- 03-08-2011, 06:39 PM #2
I'm not sure what your question is. This seems to do what you would expect. You have a while(true) there, so it's going to loop forever.
What did you expect to see instead?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-08-2011, 06:48 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
actually,i want to stop this loop after the second loops.tq
- 03-08-2011, 06:54 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 03-08-2011, 06:56 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
my mistakes..thanks
- 03-08-2011, 09:26 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
OK. What is the consensus from the more experienced on this:
Java Code:while (true) { get input from user; if (some signal) break; } continue processing
Acceptable? What's the policy on "breaks" in loops?
- 03-08-2011, 09:36 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im not sure what exactly the policy is, I have no problem using breaks where necessary. In a while(true) loop there is no other way(that I know of) to get out of it.
- 03-08-2011, 09:43 PM #8
If it is in a method you can use the return statement.
Personally I dislike while(true) loops. I prefer to use a boolean.Java Code:public void doStuff(int number) { while(true) { System.out.println(number); if(number % 5 ==0) { return; } else { number += 3; } } }
Java Code:boolean flag = true; while(flag) { // do stuff if(condition) { flag = false; } }
- 03-08-2011, 09:53 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I'm wondering as well if people can explain what the general consensus on the use of breaks is, and why they are frowned upon if they are?
And good idea on the use of return instead in methods.
- 03-09-2011, 02:04 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I don't think they're frowned upon, but if you take a step back, your code should be expressive of what you're trying to do, and not merely be the mechanism for getting a certain result. In other words, a while (true) loop with a break is perfectly fine if the idea is that the loop will continue to execute during normal operation, and break at some exceptional condition (or perhaps one of several exceptional conditions). But if, as in this case, the loop is meant to execute exactly two times, a while (true) with a break seems an odd choice. It doesn't communicate intent well, it's confusing to anyone reading your code, and it will be confusing to you when you look at it again after a few months.
-Gary-
- 03-09-2011, 01:38 PM #11
Like many things in programming, there is no "consensus" on this. Does it get the job done and fit inside your head? Then use it.
The reason there is no consensus is that it depends entirely on the context of the program, including who the programmer is. You can't really ask "what is the consensus on this general concept?" because it really depends on the specifics.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-10-2011, 03:56 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
does anyone here know what i need to add in order to do encryption in the code above?
- 03-10-2011, 04:50 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Yep, write a separate encrypt( ... ) method. We don't know what you want to encrypt and we don't know what that method is supposed to return so you have to decide. But whatever you decide, don't decide to cram that code in the same method as the code above.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Calling function in Javascript- from other function
By jdigger in forum New To JavaReplies: 1Last Post: 02-27-2011, 09:00 PM -
Looping
By Dean29126 in forum New To JavaReplies: 3Last Post: 09-08-2010, 02:01 PM -
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03-03-2010, 10:54 PM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
looping a function
By Username in forum New To JavaReplies: 2Last Post: 07-30-2007, 05:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks