title: Java compare two strings and print line
body: <p>I know this may be a really stupid question because my professor says I'm very close but I've tried so many different things. I've made an alarm clock and all that is left is to get the alarm option to compare to the time and if they are the same, print "Alarm" so I know it works. I've tried:</p>
  
  <pre><code> if(time.equals(alarmHour))
          System.out.println("Alarm");
  </code></pre>
  
  <p>with </p>
  
  <pre><code>String time = hour + ":" + minute / 10 + minute % 10 + ":" + second / 10 + second % 10;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>String alarmHour;
  
  if(sixAlarm.isSelected())
              alarmHour = "06:00:00";
  </code></pre>
  
  <p>etc. I have printed the time variable so I know it is hh:mm:ss format, which matches the alarmHour string precisely.</p>
  
  <p>But I also tried using a function</p>
  
  <pre><code> public void alarm(String time, String alarmHour){
      if(time.equals(alarmHour))  {
          System.out.println("Alarm");
      }
   }
  </code></pre>
  
  <p>while calling it under the main class</p>
  
  <pre><code>public DigitalClock() {
  // ...
  alarm(time, alarmHour);
  }
  </code></pre>
  
  <p>but it still won't work. I've tried both comparisons for Strings (time == alarmHour) and (time.equals(alarmHour)). Neither work. Someone please help. I'm close to just turning it in as it is because I have no idea why it's not working.</p>
  
  <p>UPDATE --- Complete program:</p>
  
  <pre><code>import java.applet.Applet;
  import java.applet.AudioClip;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;
  import java.net.URL;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Date;
  import javax.swing.*;
  import java.awt.Color;
  import java.util.GregorianCalendar;
  
  public class DigitalClock extends JFrame implements ActionListener {
  JLabel timeLabel = new JLabel();
  JPanel alarmGroup = new JPanel();
  JButton soundButton = new JButton();
  JLabel alarmLabel = new JLabel();
  SimpleDateFormat sdf = new SimpleDateFormat("KK:mm:ss a");
  Timer timer;
  private JCheckBox sixAlarm = new JCheckBox("6:00 AM");
  private JCheckBox eightAlarm = new JCheckBox("8:00 AM");
  private JCheckBox tenAlarm = new JCheckBox("10:00 AM");
  private JCheckBox twelveAlarm = new JCheckBox("12:00 AM");
  private AudioClip audioClip;
  private String alarmHour;
  
  // calendar values
  Calendar calendar = new GregorianCalendar();
  int hour = calendar.get(Calendar.HOUR_OF_DAY);
  int minute = calendar.get(Calendar.MINUTE);
  int second = calendar.get(Calendar.SECOND);
  String time = hour + ":" + minute / 10 + minute % 10 + ":" + second / 10 + second % 10;
  
  public DigitalClock() {
      super();
  
      // get current time from system and display
      timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
  
      // set text and specifications for alarmGroup label
      alarmGroup.setPreferredSize(new Dimension(100, 50));
      alarmGroup.setLayout(new GridLayout(5, 1));
      alarmGroup.add(alarmLabel);
      alarmLabel.setForeground(Color.BLUE);
      alarmLabel.setText("Alarm Group");
      alarmGroup.add(sixAlarm);
      alarmGroup.add(eightAlarm);
      alarmGroup.add(tenAlarm);
      alarmGroup.add(twelveAlarm);
      alarmGroup.setLocation(50, 50);
  
      // set specifications for button
      alarmGroup.add(soundButton);
      soundButton.setText("Test Alarm");
      soundButton.setForeground(Color.BLUE);
      soundButton.setBackground(Color.LIGHT_GRAY);
  
      // set font specifications for time
      timeLabel.setFont(new Font("Dialog", Font.BOLD, 80));
      timeLabel.setForeground(Color.GREEN);
  
      // set timer to update clock ever 0.5 seconds
      timer = new Timer(500, this);
      timer.setRepeats(true);
      timer.start();
  
      // handler class with listeners for checkboxes
       HandlerClass handler = new HandlerClass();
       sixAlarm.addItemListener(handler);
       eightAlarm.addItemListener(handler);
       tenAlarm.addItemListener(handler);
       twelveAlarm.addItemListener(handler);
       buttonClickClass click1 = new buttonClickClass();
       soundButton.addActionListener(click1);
  
      // add label and alarmGroup to DigitalClock and set visibility
      this.add(timeLabel, BorderLayout.NORTH);
      this.add(soundButton, BorderLayout.EAST);
      this.add(alarmGroup);
      this.pack();
      this.setVisible(true);
  
      alarm(time, alarmHour);
  }
  
  public void alarm(String time, String alarmHour){
      if(time.equals(alarmHour))  {
          System.out.println("Alarm");
      }
  }
  
  private class HandlerClass implements ItemListener {
      public void itemStateChanged(ItemEvent event){
          if(sixAlarm.isSelected())  
              alarmHour = "06:00:00";
          if(eightAlarm.isSelected())
              alarmHour = "08:00:00";
          if(tenAlarm.isSelected())
              alarmHour = "10:00:00";
          if(twelveAlarm.isSelected())
              alarmHour = "15:31:45";
      }
  }
  
  class buttonClickClass implements ActionListener{
      @Override
      public void actionPerformed(ActionEvent e){
          if(e.getSource() == soundButton)
          audioClip.play();
      }
  }
  
  public class audioClip extends JApplet {
      public void init(){
       URL urlForAudio = getClass().getResource("audio/denmark.midi");
       audioClip = Applet.newAudioClip(urlForAudio);
       audioClip.loop();
   }
  
  @Override
   public void start(){
       if(audioClip != null)
           audioClip.play();
   }
  
  @Override
   public void stop(){
       if(audioClip != null)
           audioClip.stop();
   }
   }
  
  public void actionPerformed(ActionEvent e) {
      // if timer starts then set new time
      if (e.getSource().equals(timer)) {
          // set new time
          timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
      }
  }
  
  // main method
  public static void main(String[] args) {
      // create frame
      JFrame clockFrame = new DigitalClock();
      clockFrame.setTitle("Java Final - Digital Alarm Clock");
      clockFrame.setSize(485, 300);
      clockFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      clockFrame.setLocationRelativeTo(null);
      clockFrame.setVisible(true);
      clockFrame.getContentPane().setBackground(Color.BLACK);
      clockFrame.setResizable(false);
  }
  }
  </code></pre>