Characheter Sentinel value

Problem with sentinel value with char variable

Page 1 of 1

1 Replies - 2958 Views - Last Post: 11 October 2010 - 03:57 AM Rate Topic: -----

#1 mkaredia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-October 10

Characheter Sentinel value

Posted 10 October 2010 - 10:26 PM

Hi I am trying to code a program to make a simple pocket calculator and I am stuck a sentinel value dealing with char variable. the problem is that i need to have this output "Enter 'Y' to continue with another calculation or 'N' to exit:"

here is programming everything works fine but i m stuck with sentinel value (char variable)

import java.io.*;
public class calculator
{
    private static double num1;
    private static double num2;
    private static String Snum1;
    private static String Snum2;
    private static double ans;

    public static void main(String args[])throws IOException
    {
        String readinp;
        int func;
        char termin;
        int er = 1;
        String termini;

        while(termin == N )
        {
        	System.out.println("POCKET CALCULATOR\n");
            System.out.println("Choose from the following options:\n");
            System.out.println("1. Addition ");
            System.out.println("2. Subtraction ");
            System.out.println("3. Multiplication ");
            System.out.println("4. Division ");
            System.out.println("5. Square Root\n ");
            do
            {
                er = 1 ;

                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                readinp = br.readLine();
                try
                {
                    func = Integer.parseInt(readinp);
                }
                catch(NumberFormatException oa)
                {
                    System.out.println("You entered an invalid character!");
                    er = 0;
                    func = 0;
                }
            } while(er == 0);

            switch(func)
            {
                case 1:
                    do
                    {
                        er=1;
                        System.out.print("\nPlease enter the first integer: ");
                            BufferedReader na1 = new BufferedReader(new InputStreamReader(System.in));
                            Snum1 = na1.readLine();
                            try
                            {
                                num1 = Double.parseDouble(Snum1);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while (er == 0);

                    do
                    {
                        er = 1;
                        System.out.print("\nPlease enter the second integer:");
                            BufferedReader na2 = new BufferedReader(new InputStreamReader(System.in));
                            Snum2 = na2.readLine();
                            try
                            {
                                num2 = Double.parseDouble(Snum2);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while (er == 0);

                    ans = num1 + num2;
                    System.out.println("\nAnswer:\n " + num1 + " + " + num2 + " = " + ans);
                break;

                case 2:
                    do
                    {
                        er = 1;
                        System.out.print("\nPlease enter the first integer: ");
                            BufferedReader ns1 = new BufferedReader(new InputStreamReader(System.in));
                            Snum1 = ns1.readLine();
                            try
                            {
                                num1 = Double.parseDouble(Snum1);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while(er == 0);

                    do
                    {
                        er = 1;
                        System.out.print("\nPlease enter the second integer: ");
                            BufferedReader ns2 = new BufferedReader(new InputStreamReader(System.in));
                            Snum2 = ns2.readLine();
                            try
                            {
                                num2 = Double.parseDouble(Snum2);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while (er == 0);

                    ans = num1 - num2;
                    System.out.println("\nAnswer\n" + num1 + " - " + num2 + " = " + ans);
                break;

                case 3:
                    do
                    {
                        er = 1;
                        System.out.print("\nPlease enter the first integer: ");
                            BufferedReader nm1 = new BufferedReader(new InputStreamReader(System.in));
                            Snum1 = nm1.readLine();
                            try
                            {
                                num1 = Double.parseDouble(Snum1);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while(er == 0);

                    do
                    {
                        er = 1;
                        System.out.print("\nPlease enter the second integer: ");
                            BufferedReader nm2 = new BufferedReader(new InputStreamReader(System.in));
                            Snum2 = nm2.readLine();
                            try
                            {
                                num2 = Double.parseDouble(Snum2);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while (er == 0);

                    ans = num1 * num2;
                    System.out.println("\nAnswer\n " + num1 + " * " + num2 + " = " + ans);
                break;

                case 4:
                    do
                    {
                        er = 1;
                        System.out.print("\nPlease enter the first integer: ");
                            BufferedReader nd1 = new BufferedReader(new InputStreamReader(System.in));
                            Snum1 = nd1.readLine();
                            try
                            {
                                num1 = Double.parseDouble(Snum1);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while(er == 0);

                    do
                    {
                        er = 1;
                        System.out.print("\nPlease enter the second integer: ");
                            BufferedReader nd2 = new BufferedReader(new InputStreamReader(System.in));
                            Snum2 = nd2.readLine();
                            try
                            {
                                num2 = Double.parseDouble(Snum2);
                            }
                            catch(NumberFormatException oa)
                            {
                                System.out.println("You entered an invalid character!");
                                er = 0;
                            }
                    } while (er == 0);

                    ans = num1 / num2;
                    System.out.println("\nAnswer\n" + num1 + " / " + num2 + " = " + ans);
                break;

                default:
             System.out.println("Invalid choice!  Try again!");
            }

            System.out.println("Enter 0 to terminate");
            BufferedReader ter = new BufferedReader(new InputStreamReader(System.in));
            termini = ter.readLine();
            try
            {
                termin = Character.parseChar(termini);
            }
                catch(NumberFormatException oa)
            {
                termin = 1;
            }
            if(termin == 0)
            {

            }

        }
    }
}


This post has been edited by macosxnerd101: 10 October 2010 - 10:35 PM
Reason for edit:: Added code tags. Please use them in the future.


Is This A Good Question/Topic? 0
  • +

Replies To: Characheter Sentinel value

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Characheter Sentinel value

Posted 11 October 2010 - 03:57 AM

You need to prompt "Play again?" Using just a Standard println(), and then looking at that character, you can loop with this condition:

while(termin == 'N' )



Where termin is a char:
termin = br.readLine().toUpperCase().charAt(0);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1