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 10 of 10

Thread: help with if and else if program

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default help with if and else if program

    hello, i am trying to write a program that asks the user to enter a number from 1 through 10 and then the program will display the roman numeral for that number.
    i am also adding a error message in which i haven't yet because im still trying to figure out how to the program will do the roman numeral.'
    i have used the if and else if. but when i input a number it just repeats the number back to me.

    the program cimpiles but it doesn't do what i want. here is what i have so far. how can i get the program to display the roman numeral after the number is entered.


    import javax.swing.JOptionPane;
     
    public class Romannumeral
    {
    	public static void main(String[] args)
    	{
    	double number;
     
    	String input;
     
     
     
    	number = Double.parseDouble(JOptionPane.showInputDialog("Enter a 
     
    number within the range of 1 through 10."));
     
     
     
    	if (number == 1)
    	{
              System.out.println("I");
    	} 
     
    	else if (number == 2)
    	{
                System.out.println("II");
    	} 
     
    	else if (number == 3)
    	{  
              System.out.println("III");
    	} 
     
    	else if (number == 4) 
    	{
              System.out.println("IV");
    	}
     
    	else if ( number == 5 )
    	{
               System.out.println("V");
    	}
     
    	else if (number == 6)
    	{
    	   System.out.println("VI");
    	}
     
    	else if (number == 7)
    	{    
    	   System.out.println("VIII");
    	}
     
    	else if (number == 8)
    	{
    	    System.out.println("VIIII");
    	}
     
    	else if (number == 9)
    	{
     	   System.out.println("IX");
    	}
     
    	else if (number == 10)
    	{
    	  System.out.println("X");
    	}
     
    	JOptionPane.showMessageDialog(null, "Roman numeral: + input " );
     
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help with if and else if program

    Your code is mixing JOptionPane's and console I/O. My advice: choose one or the other. If you wish to use the console as the UI, then accept the user input from System.in - in which case the roman numeral will be printed back given the current code. If you wish to use a JOptionPane, then you must convert the number to a roman numeral String value and notify the user of this value.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help with if and else if program

    aw you make sense. i totally forgot about that. i will change it and see if it works. also on the joptioon dialog box where i put +imput is that correct or should i put + number after i change the system.out

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help with if and else if program

    Quote Originally Posted by kittykat0953 View Post
    aw you make sense. i totally forgot about that. i will change it and see if it works. also on the joptioon dialog box where i put +imput is that correct or should i put + number after i change the system.out
    Best way to figure this out is to try it every way you wish. One can learn a lot just by experimenting - change code, compile, and run, doing so through an iterative cycle to figure out how certain changes impact behavior. If you change something and it doesn't make sense, post a specific question with the code

  5. The Following User Says Thank You to copeg For This Useful Post:

    kittykat0953 (February 7th, 2014)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with if and else if program

    Some comments:
    The code tests the user's input by comparing it to integer values: 1,2, etc. Why read the user's input into a double variable?
    Always have an else statement at the end of a chain of if/else if statements that prints a message if none of the if/else if statements were true. It's nice to know when that happens. A message will tell you for sure.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    kittykat0953 (February 7th, 2014)

  8. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help with if and else if program

    so what your saying is that i shouldn't call number a double. i finished the program and it works. at the end i added an error message.

     
    	else if (number == 10)
    	{
    	   JOptionPane.showMessageDialog(null, "Roman numeral: X");
    	}
    	else 
    	{
    	    JOptionPane.showMessageDialog(null, "Invalid Number");
    	}
     
    	System.exit(0);
     
     
    	}
     
    }


    like this. when i enter a number beside 1 - 10 it says invalid number. but what did u say about the double . i don't really understand. (beginner) lol

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with if and else if program

    The error message should include the value of number so the user can see what the wrong value is.

    Since roman numerals are all whole numbers, the code should use ints. There aren't any values like: 4.344
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help with if and else if program

    i understand.
    i tried doing it with an int but when i tried to compile it didn't work. it said that on this part it didn't recognize (of). so i put in the double and it worked.

     
    number = Double.parseDouble(JOptionPane.showInputDialog("Enter a number within the range of 1 through 10."));

    it said it didn't recognize the of after the word range

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with if and else if program

    it didn't work.
    Hard to say what the problem was without seeing the full text of the error message.

    didn't recognize the of after the word range
    That is very strange. The compiler does NOT complain about things inside of Strings (except when the escape character (\) is misused).
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Junior Member
    Join Date
    Jan 2014
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: help with if and else if program

    ok i change it and it work you where right, i probably did it wrong or misspelled something the first time

Similar Threads

  1. Replies: 1
    Last Post: February 6th, 2014, 01:11 PM
  2. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  3. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 03:56 PM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM