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 9 of 9

Thread: Need help with loop

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Need help with loop

    So i got this assignment in computer science that computes change from money given etc. etc.

    Anyways what i need help with is at the end of the program it asks you if you want to run the program again. I got it to run it again one more time with an if statement but it only runs on more time and it runs again no matter what you type in when it asks you if you want to run it again.

    How would i put it in a loop and mke it run over and over and if they type no it ends the program?

    All help is appreciated very much

    import java.io.*;
    public class Temp2
     
    {
    	public static void main(String[] args) throws IOException {
        	BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
     
        		double prc;
        		double rcvd;
    	    	int dllrs,qrtrs,dms,nckls,pnns;
    	    	System.out.println("Enter Amount Recieved");
    	    	rcvd=Double.parseDouble(input.readLine());
    	        System.out.println("Enter Price Of Item");
    	        prc=Double.parseDouble(input.readLine());
    	        double chngdue=rcvd-prc;
    	        System.out.println("Change Due"+" "+chngdue);
    		    double lftvr;
    		    dllrs=(int)chngdue/1;
    		    System.out.println("Dollars:"+" "+dllrs);
    		    lftvr=chngdue-dllrs;
    		    qrtrs=(int)(lftvr/.25);
    		    System.out.println("Quarters:"+" "+qrtrs);
    		    lftvr-=(qrtrs*.25);
    		    dms=(int)(lftvr/.10);
    		    System.out.println("Dimes:"+" "+dms);
    		    lftvr-=(dms*.10);
    		    nckls=(int)(lftvr/.05);
    		    System.out.println("Nickels:"+" "+nckls);
    		    lftvr-=(nckls*.05);
    		    pnns=(int)(lftvr/.01);
    		    System.out.println("Pennies:"+" "+pnns);
    		    System.out.println("Would you like to run the program again");
    		    String answer="yes";
    		    answer=input.readLine();
    		         if (answer=="yes")
    			        System.out.println("OK"); 
    			    	System.out.println("Enter Amount Recieved");
    			    	rcvd=Double.parseDouble(input.readLine());
    			        System.out.println("Enter Price Of Item");
    			        prc=Double.parseDouble(input.readLine());
    			        System.out.println("Change Due"+" "+chngdue);
    				    System.out.println("Dollars:"+" "+dllrs);
    				    System.out.println("Quarters:"+" "+qrtrs);
    				    System.out.println("Dimes:"+" "+dms);
    				    System.out.println("Nickels:"+" "+nckls);
    				    System.out.println("Pennies:"+" "+pnns);
    		System.out.println("Would you like to run theprogram again");
    				    answer=input.readLine();
     
     
     
     
     
     
     
     
     
     
    		}
    	}


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Need help with loop

    The easiest way I've found to loop a program is to use a do while loop around the entire thing. What you have is the original program, then a duplicate inside an if statement (which by the way needs {} if you want it to do more than the first line of code beneath it). This is just unnecessary code.

    Without "doing it for you", try something like this:

    do {
         boolean repeat = false;
         // Code for program here
         // ...
         System.out.println("Would you like to run the program again?");
         System.out.println("Y or y for yes. Any other key for no");
         char response = input.nextLine().charAt(0);
         if ((response == 'Y') || (response == 'y'))
              repeat = true;
    } while (repeat == true);

    You can alter that to allow the user to input N or n for no, and also provide a catch if they enter something other than Y y N n. Hope this helps!

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loop

    Do i need to import anything?

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Need help with loop

    Here is the one I did.

    import javax.swing.*;
     
    public class katzensteinHW3 {
    	public static void main( String[] args ) {
     
    	//...Varibles declared for whole program.    
    	int amount, originalAmount, halfDollar, quarters, dimes, nickels, pennies, selection;
    	boolean runAgain = true;  
     
    	  //...Loop
    	  do{
     
    	//...Enter the integer 1-100.
    	String amountString = JOptionPane.showInputDialog(
    	           "Enter a whole number from 1 to 99.\n"
    	           + "I will output a combination of coins\n"
    	           + "that equals that amount of change."); 	  
     
    	amount = Integer.parseInt(amountString);
    	originalAmount = amount;    
     
    	halfDollar = amount/50;		  
    	amount = amount%50;
    	quarters = amount/25;
    	amount = amount%25;
    	dimes = amount/10;
    	amount = amount%10;
    	nickels = amount/5;
    	amount = amount%5;
    	pennies = amount;	 
     
    //...Showes orginalAmount (Integer intered for amountString).
    	   JOptionPane.showMessageDialog(null,
    		                       originalAmount 
    		+ " cents in coins can be given as:\n"
          		+ halfDollar + " half dollar\n"          
    		+ quarters + " quarters\n"
    		+ dimes + " dimes\n"
    		+ nickels + " nickels and\n"
    		+ pennies + " pennies");
     
    		//...Asks if you want to run anouther.
    		selection = JOptionPane.showConfirmDialog(null,
    			  "Would you like to run another?","Confirmation",
    									JOptionPane.YES_NO_OPTION);
     
    		runAgain = (selection == JOptionPane.YES_OPTION);
     
      		 //...Continues the loop.	   
    		}while (runAgain);
     
    		System.exit(0); //...Ends the window.  
       }
    }

  5. The Following User Says Thank You to jeremykatz For This Useful Post:

    SwEeTAcTioN (October 24th, 2009)

  6. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Need help with loop

    It's usually best to let someone learn on their own and just guide them than give them the answer, jeremykatz.

    But in response to your question, the only import you would need is for the Scanner class if you choose to use that.

  7. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Re: Need help with loop

    So im still having problems it say "cannot find symbol" for respone or repeat

    import java.io.*;
    import javax.swing.*;
    public class Temp2
     
    {
    	public static void main(String[] args) throws IOException {
        	BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
    	do {
     
               double prc;
        		double rcvd;
    	    	int dllrs,qrtrs,dms,nckls,pnns;
    	    	System.out.println("Enter Amount Recieved");
    	    	rcvd=Double.parseDouble(input.readLine());
    	        System.out.println("Enter Price Of Item");
    	        prc=Double.parseDouble(input.readLine());
    	        double chngdue=rcvd-prc;
    	        System.out.println("Change Due"+" "+chngdue);
    		    double lftvr;
    		    dllrs=(int)chngdue/1;
    		    System.out.println("Dollars:"+" "+dllrs);
    		    lftvr=chngdue-dllrs;
    		    qrtrs=(int)(lftvr/.25);
    		    System.out.println("Quarters:"+" "+qrtrs);
    		    lftvr-=(qrtrs*.25);
    		    dms=(int)(lftvr/.10);
    		    System.out.println("Dimes:"+" "+dms);
    		    lftvr-=(dms*.10);
    		    nckls=(int)(lftvr/.05);
    		    System.out.println("Nickels:"+" "+nckls);
    		    lftvr-=(nckls*.05);
    		    pnns=(int)(lftvr/.01);
    		    System.out.println("Pennies:"+" "+pnns);
    		    System.out.println("Would you like to run the program again");
         System.out.println("Would you like to run the program again?");
         System.out.println("Y or y for yes. Any other key for no");
         boolean repeat= false;
         char response= input.readLine().charAt(0);
         if ((response == 'Y') || (response == 'y'))
     
              repeat= true;
    } while (repeat==true);
     
     
    		         if (response=='Y')
    			    	System.out.println("Enter Amount Recieved");
    			        double rcvd;
    			    	rcvd=Double.parseDouble(input.readLine());
    			        System.out.println("Enter Price Of Item");
    			      	double prc;
    			        prc=Double.parseDouble(input.readLine());
    			    	int dllrs,qrtrs,dms,nckls,pnns;
    			        double chngdue=rcvd-prc;
    			        System.out.println("Change Due"+" "+chngdue);
    				    double lftvr;
    				    dllrs=(int)chngdue/1;
    				    System.out.println("Dollars:"+" "+dllrs);
    				    lftvr=chngdue-dllrs;
    				    qrtrs=(int)(lftvr/.25);
    				    System.out.println("Quarters:"+" "+qrtrs);
    				    lftvr-=(qrtrs*.25);
    				    dms=(int)(lftvr/.10);
    				    System.out.println("Dimes:"+" "+dms);
    				    lftvr-=(dms*.10);
    				    nckls=(int)(lftvr/.05);
    				    System.out.println("Nickels:"+" "+nckls);
    				    lftvr-=(nckls*.05);
    				    pnns=(int)(lftvr/.01);
    				    System.out.println("Pennies:"+" "+pnns);
    				    System.out.println("Would you like to run the program again");
    		     System.out.println("Would you like to run the program again?");
    		     System.out.println("Y or y for yes. Any other key for no");
     
     
     
     
     
     
     
     
     
     
    		}
    	}

  8. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Need help with loop

    Try defining them both right before the do while loop.

    boolean repeat = false; // A value is necessary
    char response

    Then remove the char in front of response within the loop.

  9. The Following User Says Thank You to Bill_H For This Useful Post:

    SwEeTAcTioN (October 25th, 2009)

  10. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loop

    Thanks man it runs like a charm now

  11. #9
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Need help with loop

    Quote Originally Posted by SwEeTAcTioN View Post
    Thanks man it runs like a charm now
    No problem, glad I could help!

Similar Threads

  1. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM
  2. literal Loop
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 10th, 2009, 10:03 PM
  3. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  4. For loop tricks
    By helloworld922 in forum Java Programming Tutorials
    Replies: 0
    Last Post: October 4th, 2009, 01:09 AM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM