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

Thread: Need help with loops and turning into roman numerals

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with loops and turning into roman numerals

    Hello all,

    I am relatively new and need some help with this. It is not allowing me to have two separate inputs. Enter "Name" and "Date". It for some reason is showing up on the same line.

    Thanks for the help

    package javaapplication7;
    import java.util.Scanner;
     
    /**
     *
     * @author Onlyname
     */
    public class JavaApplication7 {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner scan = new Scanner(System.in);
     
            // Input for name
            System.out.print("Name: "); 
            String Name="";
     
            // Input for Date
            System.out.print("Date: ");
            String Date="";
     
            int number = scan.nextInt();
     
            // Roman variable assignment
            String roman="";
     
            if (number<=1900 || number>2100) {
                System.out.println("Invalid. You must enter numbers between 1900 & 2100");
                System.out.println("Enter another number now: ");
     
            }
            while (number>=1000){
                roman +="M";
                number-=1000;
            }
     
            while(number>=900){
                roman += "CM";
                number-=900;
            }
     
            while (number>=500){
                roman += "D";
                number-=500;
            }
     
            while (number>=100){
                roman += "C";
                number-=100;
            }
     
            while (number>=50){
                roman += "L";
                number-=50;
            }
     
            while (number>=10){
                roman += "X";
                number-=10;
            }
     
            while (number>=5){
            roman += "V";
            number-=5;
            }
     
            while (number>=1){
                roman += "I";
                number-=1;
            }
     
            return Name + roman;
        }
    }


  2. #2
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Need help with loops and turning into roman numerals

    // Input for name
            System.out.print("Name: "); 
            String Name="";
     
            // Input for Date
            System.out.print("Date: ");
            String Date="";
     
            int number = scan.nextInt();
    You never got the input from Name nor Date. The only input you got is for number.

    You are trying to get the input of 3 different values at the same time, you can't do that, first you get Name then you get Date and then you get number. What is the Date variable for? you never use it.

    They appear in the same line because you are using .print() which doesn't create new lines, if you are using .print() then you need to type \n for a jump of line. if you don't want to do that then use .println()

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

    Onlyname (September 7th, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Need help with loops and turning into roman numerals

    Take a look at this. You can see there are many different ways of printing to the screen. System.out.print does not contain a line break so anything displayed will show on the same line.

  5. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Need help with loops and turning into roman numerals

    @onlyname,
    You said you wanted to receive three inputs but you have programmed for only one input i.e. the number as taken care of by nextInt(). If you want the other two inputs you need to invoke the appropriate nextXxx() methods of the Scanner class.
    You have logical error in your program.
    Suppose I entered 2200. You print a message that says number is invalid. Then you enter the loop and try to print the roman letters.
    Try to modify your program according to your requirements with the two hints listed earlier.

    Syed.

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loops and turning into roman numerals

    The date is supposed to take any date from 1900 to 2100 and convert into roman numeral, whatever the date maybe; but it has to be within that range.

  7. #6
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Need help with loops and turning into roman numerals

    Quote Originally Posted by syedbhai
    Try to modify your program according to your requirements with the two hints listed earlier.
    You need to do that for now haha.

    But okay, lets do this.
    You need to compile your program mentally from the section i quoted in my first post:
         // Input for name
            System.out.print("Name: "); 
            String Name="";
     
            // Input for Date
            System.out.print("Date: ");
            String Date="";
     
            int number = scan.nextInt();

    you just printed out "Name: "
    You declared a variable Name and assigned nothing to it.
    you printed out "Date: " and there was no new line jump so, so far you have "Name: Date: " in screen.
    you declared a variable Date and assigned nothing to it.
    you declared a variable number and (for the first time) asked the user to input data which will be assigned to number.

    so is like syad said, it is a logical problem, fix that and it will work. (by the way, you explained what Date is but you never used it in the entire code, you are using the variable number)

  8. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loops and turning into roman numerals

    Thanks for the help!!

    I got it to break lines and have 2 separate inputs. However, It is not returning the values that I am asking for in the end. It says "cannot not return value whose result type is void"? Is this a logical error as well.

    Thanks again for the your time.

    package javaapplication7;
    import java.util.Scanner;
     
    /**
     *
     * @author Onlyname
     */
    public class JavaApplication7 {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner scan = new Scanner(System.in);
     
            // Input for name
            System.out.print("Name: "); 
     
            String Name= scan.nextLine();
     
            // Input for Date
            System.out.print("Date: ");
     
     
     
            String Date= scan.nextLine();
     
     
            int number = scan.nextInt();
     
     
            // Roman variable assignment
            String roman="";
     
            if (number<=1900 || number>2100) {
                System.out.println("Invalid. You must enter numbers between 1900 & 2100");
                System.out.println("Enter another number now: ");
     
            }
            while (number>=1000){
                roman +="M";
                number-=1000;
            }
     
            while(number>=900){
                roman += "CM";
                number-=900;
            }
     
            while (number>=500){
                roman += "D";
                number-=500;
            }
     
            while (number>=100){
                roman += "C";
                number-=100;
            }
     
            while (number>=50){
                roman += "L";
                number-=50;
            }
     
            while (number>=10){
                roman += "X";
                number-=10;
            }
     
            while (number>=5){
            roman += "V";
            number-=5;
            }
     
            while (number>=1){
                roman += "I";
                number-=1;
            }
     
            return Name + roman;
        }
    }

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,302
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Need help with loops and turning into roman numerals

    The main() method is by definition, void, and cannot return anything. But you're in luck, there's no reason to return anything. Instead of returning the result, assign it to a variable:

    String result = Name + roman;

    And then print the result, or skip assigning it to a variable, and simply print it:

    System.out.println( "The result is: " + Name + roman );

  10. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loops and turning into roman numerals

    hmmm,

    So, I did this and I am still getting no result.

    I actually had, the System.out.printIn(xxx) earlier, but something was wrong with my logic so I changed it to return. Which I had no idea that it couldnt return anything when i replaced system.out with "return" in the first place.

    Does String result = Name + roman; need to be somewhere specific other than with the others?

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,302
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Need help with loops and turning into roman numerals

    It helps to post your updated code so that we know what changes you made. In place of your return, I used

    System.out.println( Name + roman );

    And I get a result. I can't imagine why you would not be.

  12. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loops and turning into roman numerals

    [code = java]
    package javaapplication7;
    import java.util.Scanner;

    /**
    *
    * @author Onlyname
    */
    public class JavaApplication7 {
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner scan = new Scanner(System.in);

    // Input for name
    System.out.print("Name: ");

    String Name= scan.nextLine();

    // Input for Date
    System.out.print("Date: ");

    int number = scan.nextInt();

    // Roman variable assignment
    String roman="";


    if (number<=1900 || number>2100) {
    System.out.println("Invalid. You must enter numbers between 1900 & 2100");
    System.out.println("Enter another number now: ");


    }
    while (number>=1000){
    roman +="M";
    number-=1000;
    }

    while(number>=900){
    roman += "CM";
    number-=900;
    }

    while (number>=500){
    roman += "D";
    number-=500;
    }

    while (number>=100){
    roman += "C";
    number-=100;
    }

    while (number>=50){
    roman += "L";
    number-=50;
    }

    while (number>=10){
    roman += "X";
    number-=10;
    }

    while (number>=5){
    roman += "V";
    number-=5;
    }

    while (number>=1){
    roman += "I";
    number-=1;
    }

    System.out.println( "The result is: " + Name + roman );
    }
    }
    [/code]

  13. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,302
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Need help with loops and turning into roman numerals

    You made progress. I got 1959 = MCMLVIIII. That's not right. Close, but not right.

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    Onlyname (September 6th, 2013)

  15. #13
    Junior Member
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with loops and turning into roman numerals

    Yea, I made a minor mistake in that. I had a string after the date. So instead of there being an output it just kept looking for that variable. Now I have to figure this out. Thanks a lot for everyones help. I will probably be back later if I can't figure out future steps. Thanks greg

Similar Threads

  1. Turning java files into .zip format
    By Scorks in forum The Cafe
    Replies: 2
    Last Post: January 30th, 2013, 07:58 PM
  2. number turning negative during calculation why???
    By derekxec in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 3rd, 2012, 12:35 PM
  3. Turning a JSP into a JAVA file
    By PyroPlasm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 24th, 2012, 01:51 PM
  4. How to identify a roman numeral
    By dunWorry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2010, 12:58 AM
  5. Roman Numbers
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 12th, 2009, 02:19 AM