Norseback
Norseback

Reputation: 315

Java help regarding loops

I'm basically new to this programming stuff. There is a part of the code that I couldn't understand and don't know how to do. Probably what our instructor didn't discuss. What I want to happen is that when I input "Y", it would retry the whole program. If I input "N", it would exit. Here is the code

import java.io.*;
import javax.swing.JOptionPane;

public class Doge {

    public static void main(String[] args) 
    {
        int start = 0;
        int end = 0;
        int step = 0;
        String input = "";
        BufferedReader in = new BufferedReader (
                new InputStreamReader (System.in));

        try 
        {
            System.out.println("Input starting number: ");
            input = in.readLine();
            start = Integer.parseInt(input);

            System.out.println("Input ending number: ");
            input = in.readLine();
            end = Integer.parseInt(input);

            System.out.println("Input step number: ");
            input = in.readLine();
            step = Integer.parseInt(input);

        }
        catch (IOException e)
        {
            System.out.println("Error");
        }

        System.out.println("");
        if (start>=end)
        {
            System.out.println("Starting number should be lesser than the ending number.");
        }
        while (start<=end)
        {
            System.out.println(start);
            start = start+step;
        }

        String result = "";
        char y = 'Y';
        char n = 'N';

        BufferedReader it = new BufferedReader (
                new InputStreamReader(System.in));
        try 
        {
            System.out.print("Do you want to retry (Y/N)? ");
            result = it.readLine();
        }
        catch(IOException e)
        {
            System.out.println("Error");
        }
        start = 0;
        end = 0;
        step = 0;
        input = "";
        BufferedReader in2 = new BufferedReader (
                new InputStreamReader (System.in));
        while ("y".equals(result))
        {
            try 
            {
                System.out.println("Input starting number: ");
                input = in2.readLine();
                start = Integer.parseInt(input);

                System.out.println("Input ending number: ");
                input = in2.readLine();
                end = Integer.parseInt(input);

                System.out.println("Input step number: ");
                input = in2.readLine();
                step = Integer.parseInt(input);
            }
            catch (IOException e)
            {
                System.out.println("Error");
            }

            System.out.println("");

            while (start<=end)
            {
                System.out.println(start);
                start = start+step;
            }
        }

        if ("n".equals(result))
            System.exit(0);
    }
}

I'm quite confused. I was thinking of writing the same codes but the input starting number line loops after placing a value for the step number.

Upvotes: 1

Views: 116

Answers (1)

V-SHY
V-SHY

Reputation: 4125

Understand do..while loop in Java....here

Problems I found in your code

  1. BufferedReader no reuse
  2. Your retry result only check once but not everytime it finsh one loop of process
  3. String compare with "y" but not the char y = 'Y'; you defined. char y = 'Y'; should be String y = "Y"; (The comparison with .equals() is case-sensitive. So if you want include 'y' into your consideration, you can use .equalsIgnoreCase()
  4. { and } not pairing well to display statement in same scope (ease reading)

I rewrite your code, hope can help you understand more

import java.io.*;
import javax.swing.JOptionPane;

public class Doge {

    public static void main(String[] args) 
    {
        int start = 0;
        int end = 0;
        int step = 0;
        String input = "";
        String result = "";
        boolean invalidInput = false;
        //String y = "Y";
        //String n = "N";

        //reuse same BufferedReader
        BufferedReader in2 = new BufferedReader (
                new InputStreamReader (System.in));

        //use do while to have statement run once then only check retry 
        do
        {
            try 
            {
                do
                {
                    System.out.println("Input starting number: ");
                    input = in2.readLine();
                    start = Integer.parseInt(input);

                    System.out.println("Input ending number: ");
                    input = in2.readLine();
                    end = Integer.parseInt(input);

                    if (start >= end)
                    {
                        System.out.println("Starting number should be lesser than the ending number.");
                        invalidInput = true;
                    }
                    else
                        invalidInput = false;
                } while (invalidInput);  //loop as long as the start >= end

                System.out.println("Input step number: ");
                input = in2.readLine();
                step = Integer.parseInt(input);
            }
            catch (IOException e)
            {
                System.out.println("Error");
            }

            System.out.println("");

            while (start<=end)
            {
                System.out.println(start);
                start = start+step;
            }

            try 
            {
                System.out.print("Do you want to retry (Y/N)? ");
                result = in2.readLine();
                //it will exit if answer is "N" or "n"
                //check whether result is String n (ignore case)
                //if (n.equalsIgnoreCase(result))
                //only accept "n"
                if ("n".equals(result))
                    System.exit(0);
            }
            catch(IOException e)
            {
                System.out.println("Error");
            }
        //} while (y.equalsIgnoreCase(result)); //loop if "y" or "Y"
        } while ("y".equals(result));

        //if here have other process, then your result that is neither 'Y' nor 'N' will run here    
    }
}

After run, I only know .equals() and .equalsIgnoreCase() can only be used on String but not char

Upvotes: 1

Related Questions