Results 1 to 13 of 13
  1. #1
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default 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);
    	
    	
    }
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default

    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!

  3. #3
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    actually,i want to stop this loop after the second loops.tq

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by stegano View Post
    actually,i want to stop this loop after the second loops.tq
    Then don't use a do { ... } while (true); loop. It loops forever. I'm sure you can create a loop that only loops twice. <--- hint: a for-loop can do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    my mistakes..thanks

  6. #6
    eXistenZ is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    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.
    If it is in a method you can use the return statement.
    Java Code:
    public void doStuff(int number) {
        while(true) {
            System.out.println(number);
            if(number % 5 ==0) {
                return;
            } else {
                number += 3;
            }
        }
    }
    Personally I dislike while(true) loops. I prefer to use a boolean.
    Java Code:
    boolean flag = true;
    while(flag) {
        // do stuff
        if(condition) {
            flag = false;
        }
     }

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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-

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default

    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!

  12. #12
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    does anyone here know what i need to add in order to do encryption in the code above?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by stegano View Post
    does anyone here know what i need to add in order to do encryption in the code above?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  2. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 03:01 PM
  3. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 11:54 PM
  4. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  5. looping a function
    By Username in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •