Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need Help Creating a Loop.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help Creating a Loop.

    my program compiles with no errors though I need help actually getting the program to loop. I want it to loop if yes is entered , and stop if no is entered. How would I go about doing this? can someone please provide the proper code needed? I am very new to Java and I am trying my best to learn. this program is a project from a book I am reading.

    here is my code:
    /*  Protoype program to be used to prove  first concepts Algorithm project */
     
    import java.io.*;
     
    import java.lang.*;
     
    import java.text.*;
     
    import java.util.*;
     
    //Set up public class
     
    public class  document2
     
    {
     
    //declare main() method
     
    public static void main(String[] args) throws IOException
     
    {
     
    //Declare data types
     
    float length,width,area,circum,loop,yes,no;
     
    //set up input stream
     
    Scanner  readin = new Scanner(System.in);
     
    //prompt for length
     
    System.out.println("\n\n enter value for length");
     
    //stream in length and convert to float
     
    length = readin.nextFloat();
     
    //prompt for width
     
    System.out.println("\n\n enter value for width");
     
    //stream in width and convert to float
     
    width = readin.nextFloat();
     
    //Calculate results
     
    area = length*width;
     
    circum = 2* (length + width);
     
    //prepare data for out put display
     
     DecimalFormat twodig = new DecimalFormat("########.##");
     
     System.out.print("\n\nThe results of the calcuations are\n\n\n");
     
     System.out.print("the area is:\t\t"  +  twodig.format(area) + "\n\n");
     
     System.out.println("the circumference is:\t\t" + twodig.format(circum));
     
     //prompt for loop
     
     System.out.println("\n\n calculate again YES / NO ?");
     
     //stream in width and convert to float
     
    loop = readin.nextFloat();
     
     
     
     }//end main
     
     }//end class


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need Help Creating a Loop.

    I guess you didn't like the help you already received on another forum?

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/43951-need-help-creating-loop.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need Help Creating a Loop.

    This looks over-engineered, or I don't understand what you are doing.
    Simply put, this will continue the loop if the user enters yes, and discontinue it if the user enters no.
    import java.util.Scanner;
     
    public class yesOrNo{
         public static void main(String[] args){
              Scanner input = new Scanner(System.in);
              boolean yes = true;
              final String YES = "yes";
              final String NO = "no";
              String userInput;
              while(yes == true){
                   System.out.print("yes or no: ");
                   userInput = input.nextLine();
                   if (userInput == YES){
                        yes = false;
                   }
                   else if(userInput == NO){
                        yes = true;
                   }
                   else{
                        yes = true;
                   }
              }
         }
    }
    Last edited by bglueck; May 14th, 2011 at 12:32 AM. Reason: Mistake

  4. #4
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Need Help Creating a Loop.

    bglueck, don't spoonfeed. Also don't compare Strings with the equals operator (==), use equals() or equalsIgnoreCase() instead.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need Help Creating a Loop.

    Quote Originally Posted by OutputStream View Post
    Also don't compare Strings with the equals operator (==), use equals() or equalsIgnoreCase() instead.
    Your right. Thanks for catching my mistake.

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  2. Creating multiple arraylists with a loop?
    By DLX in forum Collections and Generics
    Replies: 4
    Last Post: December 28th, 2010, 06:58 PM
  3. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM