LOOP in JAVA cont… JAVA by Ravinder Nath Rajotiya - April 16, 2021April 16, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle Loop ControlsExample-1: Input ValidationExample-2: user control of Loop Loop Controls Here is an example of range validation, but has no control as how many times the user can take to input the correct value Example-1: Input Validation package com.example; import javax.swing.*; public class loopContinued { public static void main(String[] args) { //input validation String input; int number; input= JOptionPane.showInputDialog(“Please enter a number” + “between 1 and 100”); number=Integer.parseInt(input); // now let us validate while(number <1 || number >100) // OR operator to check outside range { JOptionPane.showMessageDialog(null, “Not a valid number”); input=JOptionPane.showInputDialog(“Please enter a number between 1 and 100”); number = Integer.parseInt(input); } } } Example-2: user control of Loop In the example below, we use control the loop variable package com.example; import javax.swing.*; public class loopContinued { public static void main(String[] args) { //input validation String input; int Maxnumber; input=JOptionPane.showInputDialog(“How high should I go to square the number”); Maxnumber = Integer.parseInt(input); System.out.println(“number Squareed “); for(int i=1; i<= Maxnumber; i++) { System.out.println( i + “\t\t\t\t” + i*i); } }s } 3rd Example: Running total, is the summing the number to the previous total. Let us see through the example In this example, system ask the user how many days sale data is available, then he is prompted to daily sales data and calculates the running total. Also in the example we will use formatted output. For that, we use the class DecimalFormat We use within quottes ## .. and 00 DecimalFormat Dollar=new DecimalFormat(“#,##0.00”) this means if there is something belo #; show it else don’t show If there is something belo 0, show it else show 0. On the decimal side if there are 2 zeros, show upto 2-decimal places. Number or zeros after decimal point will be digits after (.). See declaration and useage in showMessageDialog(); package com.example; import javax.swing.*; public class loopContinued { public static void main(String[] args) { //input validation String input; int days; // ask user to give how many days data is required double sales;//ask use to give daily sales double totalSales=0.0; // keep record or running total DecimalFormat Dollar =new DecimalFormat(“#,##0.00”); input=JOptionPane.showInputDialog(“please input number of days the sales availble?”); days = Integer.parseInt(input); for(int count =0; count<days; count++) { input=JOptionPane.showInputDialog(“Enter sales for the day”+(count+1)); sales=Double.parseDouble(input); totalSales += sales; } JOptionPane.showMessageDialog(null, “Total sale is: “+Dollar.format(totalSales)); System.exit(0); } } Example-4: Sentinal values A sentinal values can be used to notify program to stop acquireing inputs. Ask user to input a value. Then we double for the user multiplying by 2. Please enter value to double untill he chooses to stop. package com.example; import javax.swing.*; public class loopContinued { public static void main(String[] args) { //input validation String input; int value; int doubleValue; input=JOptionPane.showInputDialog(“Please enter a value to double” + “0 to stop”); value=Integer.parseInt(input); while(value!=0) { doubleValue= value * 2; JOptionPane.showMessageDialog(null, “”+ value + “doubled is”+doubleValue); input=JOptionPane.showInputDialog(“Please enter a value to double” + “0 to stop”); value=Integer.parseInt(input); } System.exit(0); } } Share on Facebook Share Send email Mail Print Print