Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: String and int problem in swing program

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default String and int problem in swing program

    I just started programming with Java the other day, so I don't really know all too much. I am making a program that asks you for a day of the month, month, and year, and then tells you what day of the week that is.

    import javax.swing.JOptionPane ;
     
    public class ChurchAustinA1Q1 {
    	public static void main(String[] args) {
    		final String PROMPT_DAY = "Enter a day of the month." ;
    		final String PROMPT_MONTH = "Enter a month. "  ;
    		final String PROMPT_YEAR = "Enter a year. " ;
    		final String YYYY ;
    		final int DAY ;
    		final int MONTH ;
    		final int CENTURY ;
    		final int YEAR ;
    		final int WEEK;
     
    		DAY = JOptionPane.showInputDialog(null, PROMPT_DAY) ;
    		MONTH = JOptionPane.showInputDialog(null, PROMPT_MONTH) ;
    		YYYY = JOptionPane.showInputDialog(null, PROMPT_YEAR) ;
    		CENTURY = YYYY.substring(0,2) ;
    		YEAR = YYYY.substring(2,4) ;
    		if ( MONTH < 3 ) {
    			MONTH = MONTH + 12 ;
    			YEAR = YEAR - 1 ;
    		}
     
    		WEEK = ( ( DAY + ( ( ( MONTH + 1 ) * 26 ) / 10 ) ) + YEAR + ( YEAR / 4 ) + ( CENTURY / 4 ) + ( 5 * CENTURY ) ) ;
     
    		JOptionPane.showMessageDialog(null, "Week day: " + WEEK ) ;
    	}
    }

    But I get this problem:

    C:\Users\Austin\Desktop\ChurchAustinA1Q1.java:24: incompatible types
    found   : java.lang.String
    required: int
    		DAY = JOptionPane.showInputDialog(null, PROMPT_DAY) ;
    		                                 ^
    C:\Users\Austin\Desktop\ChurchAustinA1Q1.java:25: incompatible types
    found   : java.lang.String
    required: int
    		MONTH = JOptionPane.showInputDialog(null, PROMPT_MONTH) ;
    		                                   ^
    C:\Users\Austin\Desktop\ChurchAustinA1Q1.java:27: incompatible types
    found   : java.lang.String
    required: int
    		CENTURY = YYYY.substring(0,2) ;
    		                        ^
    C:\Users\Austin\Desktop\ChurchAustinA1Q1.java:28: incompatible types
    found   : java.lang.String
    required: int
    		YEAR = YYYY.substring(2,4) ;
    		                     ^
    4 errors
     
    Tool completed with exit code 1

    But if I change those variables to String, then the WEEK equation won't work because those variables need to be int. It's probably something really simple that I'm missing, but I need help.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problem with String and int

    Hello, the reason this is happening is because the showInputDialog method returns a string but you are trying to store the return into an int.

    DAY = JOptionPane.showInputDialog(null, PROMPT_DAY) ;

    If you change the variable DAY to be of type String this will work or you could use Integer.parseInt to parse the String.

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    duckman (October 19th, 2009)

Similar Threads

  1. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  2. How to invert a word String
    By Truffy in forum Java Programming Tutorials
    Replies: 16
    Last Post: October 3rd, 2009, 02:44 AM
  3. [SOLVED] Java program to invert a string and display the last enter key in uppercase
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 27th, 2009, 02:33 AM
  4. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM
  5. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM