1

I am having problems to change the value of a variable. I found This older question, but I am still having problems to apply it to my code. If someone can review my code and give some guidance I will really appreciate it. This is my code:

public class GMTime {

    private static String time;
    private static int hour;
    private static int minutes;
    private static int colon;
    private static String error = null;

    // **********************************************************
    // constructor passing time as a string
    public GMTime(String temp) {
        time = temp;
    }
    // end constructor
    // **********************************************************
    // method checking the colon presence and place between 1-2 index

    public String colonCheck(String error) {
        while (time.contains(":")) {
            colon = time.indexOf(":");
        } // end of while
        if (colon != 1 || colon != 2) {
            error = "Invalid separator entered";
        }
        System.out.println(error);
        return error;
    } // end colon check
    public static String getError(){
    return error;
    }
}

Driver:

import java.util.Scanner;

public class GMUnit6Ch15{

    public static void main(String[] args){

    Scanner stdIn = new Scanner(System.in);
    String time;
    System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
    time = stdIn.next();

    while (!time.equalsIgnoreCase("q")){

    GMTime userTime = new GMTime(time);

    System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
    time = stdIn.next();

    }

I added this just to test if the modified error works.

    System.out.println(GMTime.getError()); 


    }//end of main

}// end of class

What I want to do is if "colon" is not present in "time" change the value of "error" so that I can print it later from the driver.

Community
  • 1
  • 1

1 Answers1

0

There are at least three problems in GMTime and colonCheck():

  1. The variables in GMTime seems not required to be static.
  2. The while (time.contains(":")) loop may be an infinite loop.
  3. The statement if (colon != 1 || colon != 2) is actually always true, no matter what value colon is.

One plausible way to fix these issues is as below (I have tested the code, so please try to start from here and complete the whole program):

GMTime class:

public class GMTime {

    private String time;
    private int hour;
    private int minutes;
    private int colon;
    private String error = null;

    // **********************************************************
    // constructor passing time as a string
    public GMTime(String temp) {
        time = temp;
    }
    // end constructor
    // **********************************************************
    // method checking the colon presence and place between 1-2 index

    public void colonCheck() {
        while (time.contains(":")) {
            colon = time.indexOf(":");
            time = time.substring(colon + 1);
        } // end of while
        if (colon != 1 && colon != 2) {
            error = "Invalid separator entered";
        } else {
            error = "No error";
        }
    } // end colon check

    public String getError() {
        return error;
    }
}

GMUnit6Ch15 class:

import java.util.Scanner;

public class GMUnit6Ch15 {

    public static void main(String[] args) {

        Scanner stdIn = new Scanner(System.in);
        String time;
        System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
        time = stdIn.next();

        while (!time.equalsIgnoreCase("q")) {

            GMTime userTime = new GMTime(time);
            userTime.colonCheck();
            System.out.println(userTime.getError());

            System.out.print("Enter time in the form mm:dd (\"q\" to quit) :");
            time = stdIn.next();            
        }
    }
}

Exemplar input / output:

Enter time in the form mm:dd ("q" to quit) :1111

Invalid separator entered

Enter time in the form mm:dd ("q" to quit) :11:11

No error

Enter time in the form mm:dd ("q" to quit) :q

Community
  • 1
  • 1
Tsung-Ting Kuo
  • 1,171
  • 6
  • 16
  • 21