1

I made a simple calculator to calculate the total time of songs. Iam using a while function but I dont know how to break it. Because of this I had to make it go on forever and after each new imput from me, it writes back the total time up to this point.

Example:

12 21 12:21 31 41 44:02

the bold is what i type in and the non bold is what the eclipse writes back to me.

What I want is that i type in X amount of numbers, and when iam done I press some key, escape for example and then it would write back the total time..

here is my full code

import java.util.Scanner;

public class train { 
    public static void main(String[] args) {            
        int songM, songS, totalS=0, totalM=0, y=1;
        Scanner scan = new Scanner(System.in);
        System.out.println("write songs, exit with -1");

        while(y > 0) {
            songM = scan.nextInt();
            songS = scan.nextInt();

            totalM = songM + totalM;
            totalS = songS + totalS;


            if(totalS >59) {
                totalM = totalM + 1;
                totalS = totalS % 60;
            }
            if(totalS<=9) {
                System.out.println(totalM+":0"+totalS );
            } else {
                System.out.println(totalM+":"+totalS );             
            }
        }       
    }
}
kiheru
  • 6,588
  • 25
  • 31
user2779497
  • 77
  • 1
  • 3
  • 9
  • What you want to do is detect a particular key-press, like ESC. This might help: http://stackoverflow.com/questions/10736226/break-a-loop-if-esc-was-pressed – Johnson Wong Sep 14 '13 at 15:50
  • @Darkurio mmm .. He cannot catch key event from command line – fujy Sep 14 '13 at 15:55

5 Answers5

0

In your for loop check for -1 input:

    System.out.println("write songs, exit with -1");

    while(y > 0) {
        songM = scan.nextInt();
        songS = scan.nextInt();

        if(songM.equals("-1") || songS.equals("-1"))
             break;

        totalM = songM + totalM;
        totalS = songS + totalS;


        if(totalS >59) {
            totalM = totalM + 1;
            totalS = totalS % 60;
        }
        if(totalS<=9) {
            System.out.println(totalM+":0"+totalS );
        } else {
            System.out.println(totalM+":"+totalS );             
        }
    }   
fujy
  • 5,168
  • 5
  • 31
  • 50
0

Get rid of the variable y and change you while loop to while(true).

Surround the whole while loop with a try block like this:

try{
  while(true){
   ...
  }
}catch(InputMismatchException e){
 /* Just quit */
}

Then instruct the user to type quit when they're done but in reality anything that's not an int will exit the loop.

UFL1138
  • 632
  • 3
  • 10
0

Another way to do it if you wanted to enforce the keyword 'quit' to exit would be to do the following:

while(true) {
    try{
        songM = scan.nextInt();
        songS = scan.nextInt();
    } catch (InputMismatchException e){
        if(scan.nextLine().trim().equalsIgnoreCase("quit")){
            System.out.println("Goodbye.");
            break;
        } else {
            System.out.println("Unrecognized input, please retry.");
            continue;
        }
    }
    ...
  }
UFL1138
  • 632
  • 3
  • 10
0

I recommend you use do-while loop and naming convention, here is my code

public class Train
{
    public static void main(String[] args)
    {
        int songM, songS, totalS = 0, totalM = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("write songs, exit with < 0");

        do
        {
            songM = scan.nextInt();
            songS = scan.nextInt();
            if (songM >= 0 && songS >= 0)
            {
                totalM += songM;
                totalS += songS;

                if (totalS > 59)
                {
                    totalM = totalM + 1;
                    totalS = totalS % 60;
                }
                if (totalS <= 9)
                {
                    System.out.println(totalM + ":0" + totalS);
                }
                else
                {
                    System.out.println(totalM + ":" + totalS);
                }
            }
            else
            {
                break;
            }
        }
        while (songM >= 0 || songS >= 0);
        System.out.println("End.");
    }
}
Tiep PT
  • 1
  • 1
0

Last version and I'm moving on...

public class Train { 
    public static void main(String[] args) {            
        int totalS=0, totalM=0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Input song lengths in mm:ss, exit with 'quit'.");

        readInput: while(true) {
            String songLengths = scan.nextLine();
            String[] inputs = songLengths.split(" ");
            for(int i=0; i<inputs.length; i++){
                String input = inputs[i].trim();
                int split = input.indexOf(':');
                if(split>=0){
                    try{
                        String songMIn = input.substring(0, split);
                        String songSIn = input.substring(split+1);
                        int songM = 0;
                        /* Allows :30 for 30 seconds. */
                        if(songMIn.length()>0){ 
                            songM = Integer.parseInt(songMIn);
                        }
                        int songS = 0;
                        /* Allows 3: for 3 minutes. */
                        if(songSIn.length()>0){ 
                            songS = Integer.parseInt(songSIn);
                        }
                        /* Don't add times unless both were parsed successfully. */
                        totalM += songM;
                        totalS += songS;
                    } catch(NumberFormatException nfe){
                        System.out.printf("Input %s was not recognized as a valid time.\n", input);
                    }
                } else if(input.equalsIgnoreCase("quit")){
                    break readInput;
                } else {
                  System.out.printf("Input %s was not recognized.\n", input);
                }
            }
        }

        while(totalS > 59) {
            totalM++;
            totalS -= 60;
        }

        scan.close();

        System.out.print(totalM+":");
        System.out.println((totalS<10?"0":"")+totalS );
        System.out.println("Done.");
    }
}
UFL1138
  • 632
  • 3
  • 10