Results 1 to 20 of 20

Thread: How to compare the Public String [] value
- 09-07-2014, 07:26 PM #1
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
How to compare the Public String [] value
Hi guys,
i want to make a program in which i write the Months Strings via while into the checkbox.
I already did that but i have also to add an day if February is a loop day.
So my question is how to say java that if Months is equal to February & year is a leap year, add 1. (i didn't wrote the year code because it's not relevant for my problem.)
Here is my code:
Java Code:public String[] Months ={"January","February","March","April","May", "June", "July", "August", "September", "Oktober", "November", "December"}; public Asg1KeapYear() { initComponents(); int MonthNo = 0; while (MonthNo < Months.length) { cbxMonth.addItem(Months[MonthNo]); MonthNo = MonthNo + 1; }
Java Code:if (((Months.equals ("February")) && (year % 400 == 0)) || ((year % 4 == 0) && (year % 100 != 0))) { monthNum = monthNum + 1; }
EDIT: Netbeans shows me .equals() on incompatible types on Months.equals, do i have to declare it somehow?Last edited by taurus22; 09-07-2014 at 08:20 PM.
- 09-07-2014, 07:49 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: How to compare the Public String [] value
What exactly is monthNum? Based on your supplied code, it is unclear why you need to check if the month
is February. You need to determine if the year is a leap year and then modify the number of days associated
with February.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-07-2014, 08:13 PM #3
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Re: How to compare the Public String [] value
i made also an array for the number of the days, monthNum is the solution. For example January has 31 days.
Here is the Code for that:
Java Code:int[] monthNumber = {31, 28, 31, 30, 31, 30 , 31 , 31 , 30 , 31 ,30 ,31}; int month = cbxMonth.getSelectedIndex(); int monthNum = monthNumber[month];
However thank you for your helpLast edited by taurus22; 09-07-2014 at 08:16 PM.
- 09-07-2014, 10:45 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: How to compare the Public String [] value
Then you don't need to compare to February. You already know that the number of days for February is index number 1. So
after you determine that the year is a leap year, just increment the appropriate number of days.
Java Code:monthNumber[1]++;
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-08-2014, 04:16 AM #5
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Re: How to compare the Public String [] value
Ah Thank you I'll try it :)
EDIT
It still shows me 28 days, this is the full code.
Java Code:public class Asg1KeapYear extends javax.swing.JFrame { public String[] Months ={"January","February","March","April","May", "June", "July", "August", "September", "Oktober", "November", "December"}; public Asg1KeapYear() { initComponents(); int MonthNo = 0; while (MonthNo < Months.length) { cbxMonth.addItem(Months[MonthNo]); MonthNo = MonthNo + 1; } int year=1800; while(year<2014) { cbxYear.addItem(year + 1); year = year + 1; } }
Java Code:int[] monthNumber = {31, 28, 31, 30, 31, 30 , 31 , 31 , 30 , 31 ,30 ,31}; int month = cbxMonth.getSelectedIndex(); int monthNum = monthNumber[month]; int year = cbxYear.getSelectedIndex(); if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { monthNumber[1]++; } txtResult.setText(monthNum + "" ); }
Last edited by taurus22; 09-08-2014 at 10:54 AM.
- 09-08-2014, 05:22 AM #6
Re: How to compare the Public String [] value
this is the full code
Could you arrange the code so it could be copied, compiled and executed for testing?If you don't understand my response, don't ignore it, ask a question.
- 09-08-2014, 03:43 PM #7
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Re: How to compare the Public String [] value
Java Code:monthNumber[1]++;
Java Code:monthNum = monthNum + 1
I think there is a problem with comparing the year. I wrote the code with a loop which is below to get the years and months within the program and it works fine, but it doesn't work when i try comparing if year is a leap year or not.
So i can't compare the year from while with onlyJava Code:int years = cbxYear.getSelectedIndex();
Sorry for my bad english
Java Code:public class Asg1KeapYear extends javax.swing.JFrame { public String[] Months ={"January","February","March","April","May", "June", "July", "August", "September", "Oktober", "November", "December"}; public Asg1KeapYear() { initComponents(); int MonthNo = 0; while (MonthNo < Months.length) { cbxMonth.addItem(Months[MonthNo]); MonthNo = MonthNo + 1; } int year=1800; while(year<2014) { cbxYear.addItem(year + 1); year = year + 1; } }
Last edited by taurus22; 09-08-2014 at 04:27 PM.
- 09-08-2014, 04:16 PM #8
Re: How to compare the Public String [] value
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
it doesn't work when i try comparing if year is a leap year or not.
It never finds a leap year?
It finds the wrong year as a leap year?
It sometimes finds a leap year but misses other years?
Have you tried debugging the code to see what it is doing wrong?If you don't understand my response, don't ignore it, ask a question.
- 09-08-2014, 04:28 PM #9
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Re: How to compare the Public String [] value
If i put in
Java Code:monthNumber[1]++
Java Code:MonthNo = MonthNo + 1;
- 09-08-2014, 04:31 PM #10
Re: How to compare the Public String [] value
If i put in
monthNumber[1]++
nothing happens.
You need to post complete code that will compile, execute and show the problem. It is not possible to see what is wrong with only one line of code.
If the problem is with the leap year detection code, you need to write a special purpose testing program for debugging that code.If you don't understand my response, don't ignore it, ask a question.
- 09-08-2014, 04:39 PM #11
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Re: How to compare the Public String [] value
Yeah i know but it doesn't increment. This is the complete code, made with netbeans
Java Code:public class Asg1KeapYear extends javax.swing.JFrame { public String[] Months ={"January","February","March","April","May", "June", "July", "August", "September", "Oktober", "November", "December"}; public Asg1KeapYear() { initComponents(); int MonthNo = 0; while (MonthNo < Months.length) { cbxMonth.addItem(Months[MonthNo]); MonthNo = MonthNo + 1; } int year =1800; while(year<2014) { cbxYear.addItem(year + 1); year = year + 1; } }
Java Code:private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) { int[] monthNumber = {31, 28, 31, 30, 31, 30 , 31 , 31 , 30 , 31 ,30 ,31}; int month = cbxMonth.getSelectedIndex(); int monthNum = monthNumber[month]; int years = cbxYear.getSelectedIndex(); if ((years % 400 == 0) || ((years % 4 == 0) && (years % 100 != 0))) { monthNumber[1]++; } txtResult.setText(monthNum + "" ); }
- 09-08-2014, 04:43 PM #12
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: How to compare the Public String [] value
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-08-2014, 04:51 PM #13
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Re: How to compare the Public String [] value
aren't these the cbx variables?
Java Code:public String[] Months ={"January","February","March","April","May", "June", "July", "August", "September", "Oktober", "November", "December"}; public Asg1KeapYear() { initComponents(); int MonthNo = 0; while (MonthNo < Months.length) { cbxMonth.addItem(Months[MonthNo]); MonthNo = MonthNo + 1; } int year =1800; while(year<2014) { cbxYear.addItem(year + 1); year = year + 1; } } }
- 09-08-2014, 04:54 PM #14
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: How to compare the Public String [] value
Where are they declared? Where is your initComponents() method? Where do you add the Dialogs to the JFrame?
Normally, we don't want all the code but you aren't showing us enough to help you solve the problem.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-08-2014, 04:56 PM #15
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Re: How to compare the Public String [] value
Oh you want everything? Even things that are standard? Then here
Java Code:/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package taulantasg1; /** * * @author Taulant */ public class Asg1KeapYear extends javax.swing.JFrame { public String[] Months ={"January","February","March","April","May", "June", "July", "August", "September", "Oktober", "November", "December"}; public Asg1KeapYear() { initComponents(); int MonthNo = 0; while (MonthNo < Months.length) { cbxMonth.addItem(Months[MonthNo]); MonthNo = MonthNo + 1; } int year =1800; while(year<2014) { cbxYear.addItem(year + 1); year = year + 1; } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); btnCalculate = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); txtResult = new javax.swing.JTextField(); cbxMonth = new javax.swing.JComboBox(); cbxYear = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Taulant's leap year checker"); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel1.setText("Month:"); btnCalculate.setText("Calculate..."); btnCalculate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCalculateActionPerformed(evt); } }); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel2.setText("Result:"); txtResult.setForeground(new java.awt.Color(255, 0, 0)); jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel3.setText("Year:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(43, 43, 43) .addComponent(jLabel2)) .addGroup(layout.createSequentialGroup() .addGap(32, 32, 32) .addComponent(jLabel1))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE) .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(22, 22, 22)) .addGroup(layout.createSequentialGroup() .addGap(45, 45, 45) .addComponent(cbxMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel3) .addGap(37, 37, 37) .addComponent(cbxYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(64, 64, 64)))) .addGroup(layout.createSequentialGroup() .addGap(32, 32, 32) .addComponent(btnCalculate) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(cbxMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cbxYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnCalculate) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30)) ); pack(); }// </editor-fold> private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) { int[] monthNumber = {31, 28, 31, 30, 31, 30 , 31 , 31 , 30 , 31 ,30 ,31}; int month = cbxMonth.getSelectedIndex(); int monthNum = monthNumber[month]; int years = cbxYear.getSelectedIndex(); if ((years % 400 == 0) || ((years % 4 == 0) && (years % 100 != 0))) { monthNumber[1]++; } txtResult.setText(monthNum + "" ); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Javaâ„¢ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Asg1KeapYear.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Asg1KeapYear.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Asg1KeapYear.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Asg1KeapYear.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Asg1KeapYear().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnCalculate; private javax.swing.JComboBox cbxMonth; private javax.swing.JComboBox cbxYear; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JTextField txtResult; // End of variables declaration }
Last edited by taurus22; 09-08-2014 at 05:01 PM.
- 09-08-2014, 05:17 PM #16
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: How to compare the Public String [] value
Ok, now I know what the problems are:
First, now that I see what is going on, you do need to check the month. But use the index instead of the name.
Second, you can use monthNum instead of the way I told you because that is what you are setting in the dialog.
Just add 1 to monthNum like you did before. I will post that code since I contributed to doing it another way.
Java Code:if (month == 1 && ((years % 400 == 0) || ((years % 4 == 0) && (years % 100 != 0)))) { monthNum++; } txtResult.setText(monthNum + "");
The real problem is in your year (you should have printed it out to make certain you were retrieving the year).
What you are getting is the index of the year in the combo box. So you need to add the base year to the index
to get the actual year. Your base year is 1801 so add that to years.
In the future you should sprinkle print statements throughout your code to verify your variables. It is the basic
and most powerful debugging tool I know.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-08-2014, 05:21 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-08-2014, 05:24 PM #18
Re: How to compare the Public String [] value
As Jim said, printing the value of years would have shown you what the code was doing.
If you don't understand my response, don't ignore it, ask a question.
- 09-08-2014, 05:38 PM #19
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: How to compare the Public String [] value
And a couple more things regarding JComboBoxes.
1. JComboBox is a generic type so you should specify the type of the values.
Java Code:cbxMonth = new javax.swing.JComboBox<Integer>(); cbxYear = new javax.swing.JComboBox<Integer>();
don't need to worry about the base year as you are now getting the
selected year.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-09-2014, 05:13 AM #20
Member
- Join Date
- Sep 2014
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
If Statement that refuses to compare a indexed string array to written string.
By JordiLaForge in forum New To JavaReplies: 2Last Post: 04-28-2013, 02:50 AM -
public int countOccurrences(char c) & public int countOccurrences(String s)
By LetsG0Blue in forum New To JavaReplies: 4Last Post: 02-19-2013, 05:36 PM -
Program fails to compare a String Token with a String.
By yasarqamar in forum New To JavaReplies: 4Last Post: 07-11-2012, 01:42 PM -
how to compare one string with another dynamic length pattern string?
By ravi.josih53 in forum New To JavaReplies: 11Last Post: 08-15-2011, 11:12 AM -
Why we use String compare to String Buffer
By Sharath_Forums in forum New To JavaReplies: 1Last Post: 12-06-2010, 07:21 AM
Bookmarks