Note that there are some explanatory texts on larger screens.

plurals
  1. POWrite text to a string if an if statement returns true and and different text if else
    text
    copied!<p>OK, so here what I need. I have an if statement that if it returns true, then to add text to a string, and if it is else, then add different text to the same string. Heres my code:</p> <pre><code> import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; public class theclock extends JFrame { theclock() { final JLabel timeField = new JLabel(); timeField.setFont(new Font("sansserif", Font.PLAIN, 20)); Container content = this.getContentPane(); content.setLayout(new FlowLayout()); content.add(timeField); setTitle("Norway"); setSize(150,70); javax.swing.Timer t = new javax.swing.Timer(1000,new ActionListener() { public void actionPerformed(ActionEvent e) { Calendar cal = new GregorianCalendar(); String a = ""; //empty string that I want to add text to String h = String.valueOf(cal.get(Calendar.HOUR)+8); //I know its not the easiest way to add 8 hours, but im experimenting. int i = Integer.parseInt(h); if (i&gt;12) { i=i-12; a = "A.M"; //what to add to the string if it is true } else { i=i; //haven't applied myself to this part yet, so i know its probably wrong, but its just a place holder a = "P.M"; //for when it is else, i should say pm. } String m = String.valueOf(cal.get(Calendar.MINUTE)); String s = String.valueOf(cal.get(Calendar.SECOND)); timeField.setText("" + i + ":" + m + ":" + s + " " + a); //where the string is shown. } }); t.start(); } public static void main(String[] args) { JFrame clock = new theclock(); clock.setVisible(true); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload