3 Replies - 408 Views - Last Post: 08 November 2012 - 10:48 PM Rate Topic: -----

#1 whysmil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 12

using methods.

Posted 08 November 2012 - 10:06 PM

This is what I have so far.
import java.util.Scanner;
import java.util.Date;
import javax.swing.JOptionPane;

public class SongDonghunA2Q2 {
  public static int secondsSinceMidnight (int hour, int min, int sec){ 
    
    String prompt;
    String input;
    String output = "";
    Scanner scanner;
    int hours = 0;
    int minutes=0;
    int seconds=0;
    int sinceMidnight;
    final int MIN_HOURS = 0;
    final int MAX_HOURS = 23;
    final int MIN_MINUTES = 0;
    final int MAX_MINUTES = 59;
    final int MIN_SECONDS = 0;
    final int MAX_SECONDS = 59;
    final int SECONDS_PER_HOUR = 3600;
    final int SECONDS_PER_MINUTE = 60;
    
    prompt = "Enter the time as hh:mm:ss ";
    input = JOptionPane.showInputDialog(null, prompt);
    System.out.println("\nPROMPT:\n" + prompt);
    System.out.println("RESPONSE:\n" + input);
    
    while (input != null && !input.trim().equals("")){
      input = input.trim();
      scanner = new Scanner(input);
      scanner.useDelimiter(":");
      if (scanner.hasNextInt()){
        hours = scanner.nextInt();
        if (MIN_HOURS <= hours && hours <= MAX_HOURS){
          if (scanner.hasNextInt()){
            minutes = scanner.nextInt();
            if (MIN_MINUTES <= minutes && minutes <= MAX_SECONDS){
              if (scanner.hasNextInt()){
                seconds = scanner.nextInt();
                if (MIN_SECONDS <= seconds && seconds <= MAX_SECONDS){
                  sinceMidnight = hours * SECONDS_PER_HOUR + minutes * SECONDS_PER_MINUTE + seconds;
                  output = "There are " + sinceMidnight + " seconds since midnight for a time of " + 
                    input + '.';
                } else {
                  output = "The number of seconds, " + seconds + ", is not between " + 
                    MIN_SECONDS + " and " + MAX_SECONDS + "!";
                }
              } else {
                output = "The number of seconds is missing or is not an integer!";
              }
            } else {
              output = "The number of minutes, " + minutes + ", is not between " + 
                MIN_MINUTES + " and " + MAX_MINUTES + "!";
            }
          } else {
            output = "The number of minutes is missing or is not an integer!";
          }
        } else {
          output = "The number of hours, " + hours + ", is not between " + MIN_HOURS +
            " and " + MAX_HOURS + "!";
        }
      } else {
        output = "The number of hours is missing or is not an integer!";
      }
      JOptionPane.showMessageDialog(null, output);
      System.out.println("OUTPUT:\n" + output);
      
      input = JOptionPane.showInputDialog(null, prompt);
      System.out.println("\nPROMPT:\n" + prompt);
      System.out.println("RESPONSE:\n" + input);
    }
    System.out.println("\nProgrammed by Stew Dent") ;
    System.out.println("Date: " + new Date() ) ;
    System.out.println("** End of processing. **") ;
  }
}


and I have to use method from here(code above). When I put the methods(below) in the code above it gives me an error saying missing return statement and when I put the return statement it give me another error saying illegal start. What is the SOLUTION?

public static int getInRange (Scanner tokens, int low, int high)

public static int secondsSinceMidnight (int hour, int min, int sec)

public static int daysSinceNewYear (int year, int month, int day)

public static int secondsSinceNewYear (int year, int month, int day, int hour, int min, int sec)


Is This A Good Question/Topic? 0
  • +

Replies To: using methods.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10779
  • View blog
  • Posts: 40,135
  • Joined: 27-December 08

Re: using methods.

Posted 08 November 2012 - 10:08 PM

Post your implementations with the methods and the exact errors as they are displayed by the compiler.
Was This Post Helpful? 0
  • +
  • -

#3 whysmil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 12

Re: using methods.

Posted 08 November 2012 - 10:22 PM

I have to input the cases below and convert it to seconds.
0/0/0 0:0:0
9001/12/31 23:59:59
2012/11/18 23:59:59
2012/11/18 23:59
2012/29/02 1:00:00
2012/02/29 1:00:00
2011/02/29 1:00:00
2011/FEB/28 1:00:00

***this is just additional info***
for years, the range is 0-10000
for months, the range is 1-12
for days, the range is 1-31
for hours, the range is 0-23
for minutes and seconds
the range is 0-59



and the error is
File: /Users/SongFamily/Desktop/SongDonghunA3Q2.java  [line: 77]
Error: /Users/SongFamily/Desktop/SongDonghunA3Q2.java:77: illegal start of type

Was This Post Helpful? 0
  • +
  • -

#4 Charlie.beat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 12

Re: using methods.

Posted 08 November 2012 - 10:48 PM

make your secondSinceMidnight method void, so write it like this {code} public void secondsSinceMidnight (int hour, int min, int sec) {/code}
hope this helps.

View PostCharlie.beat, on 09 November 2012 - 05:47 AM, said:

make your secondSinceMidnight method void, so write it like this
 public void secondsSinceMidnight (int hour, int min, int sec) 

hope this helps.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1