lb1021
lb1021

Reputation: 123

Unexpected outcome with System.out.println

I'm in the process of making a program using input from a text file, it only has 2 lines of text in it which is

My code is meant to read the user's input which converts hours to minutes, and then asks for a number of changes. If the hours entered are 02:00 which is 120 minutes and the changes entered are 2 or less then it will come back saying 'acceptable', and if not it will read 'unacceptable' however I am having a bit of trouble formulating this. If anybody could provide assistance I would appreciate it greatly!

Code to follow:

import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class InputOutput {

public static void main(String[] args) throws IOException{

    final Scanner S = new Scanner(System.in);
    final Scanner inFile = new Scanner(new FileReader("task.txt"));

    // open file and associate objects
    int IOminutes = Integer.parseInt(inFile.next());
    int changes = Integer.parseInt(inFile.next());

    // close the input file
    inFile.close();

    System.out.print("Specify Time (HH:MM): ");
    String givenTime = S.next();

    System.out.print("Specify Changes: ");
    String givenChanges = S.next();

        // save the index of the colon
        int colon = givenTime.indexOf(':');

        // strip the hours preceding the colon then convert to int      
        int givenHours = Integer.parseInt(givenTime.substring(0, colon));

        // strip the mins following the colon then convert to int
        int givenMins = Integer.parseInt(givenTime.substring(colon + 1, givenTime.length()));

        // calculate the time's total mins
        int mins = (givenHours * 60) + givenMins;

        // using given time
        System.out.println(givenTime + " = " + mins + " minutes");

            if (!givenTime.equals(IOminutes) && changes >= 3) {
                System.out.println("Time: " + givenTime + ", Changes: " + givenChanges + " = unacceptable!");
            } else if (givenTime.equals(IOminutes) && changes <= 2) {
                System.out.println("Time: " + givenTime + ", Changes: " + givenChanges + " = acceptable!");
            }
        S.close();
    }
}

Upvotes: 1

Views: 180

Answers (1)

jgreve
jgreve

Reputation: 1253

Your inputs (file-based and user-based) look reasonable. By the time you reach your if-elseif logic on line 40, you have the following values (all values based on the problem description in the question): loaded from "task.txt"... IOminutes: 120 changes: 2

user input:
givenTime="02:00"
givenChanges=2
givenHours=2
givenMins=0
mins=2*60+0 = 120

Your conversion from strings to integers looks like no problem.

Your desired outcome of "acceptable" / "unacceptable" is hard for me to understand; not what it is doing, but Why it is doing that. I'm having trouble understanding why you have two "changes".

This would make more sense to me if you just had: task.txt: IOminutes=120, changes=2 given: time="hh:mm" Now compute difference (in minutes) between task.txt's IOminutes and user's given time. Let's call that difference givendiff. Then you have something like: if givendiff > changes then unacceptable.

Examples (user input values more or less made up):

task.txt: IOminutes=120, changes=2
test 1: given time="02:00"  (computed givendiff=0,   so acceptable)
test 2: given time="01:50"  (computed givendiff=-10, so unacceptable)
test 3: given time="02:05"  (computed givendiff=5,   so unacceptable)
test 3: given time="02:02"  (computed givendiff=2,   so acceptable)
test 3: given time="01:58"  (computed givendiff=-2,  so acceptable)

I would encourage you to review the original requirements and verify whether your user is supposed to be give you an extra "changes" in addition to task.txt's changes. Or if you're supposed to compute a the difference between task.txt's IOminutes and the user-entered value, and complain if that difference exceeds task.txt's changes value.

I would go further but this seems like a homework or code-challenge problem; if so, hopefully this is enough to help nudge your perspective to re-thing what "changes" means in the original requirements. Good luck.

Upvotes: 2

Related Questions