The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Some beginner Java errors

MarcoND7MarcoND7 Registered User regular
edited November 2009 in Help / Advice Forum
Hey guys! I have had some trouble with a java program I am working on, and seeing as my teacher has disappeared, I need some help from knowledgeable people. So I came here. :D

Here is the program:
// The "RomanNumerals" class.
import java.awt.*;
import hsa.Console;

public class RomanNumerals
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        String para = c.readLine ();
        String[] result = para.split ("\\s");

        int num1 = 0, num2 = 0, total=0, x;

        for (x = 0 ; x < result.length ; x++)
        {

            if (result [x].equals ("I") || result [x].equals ("i"))
            {
                if (x == result.length || result [(x + 1)].equals ("I")|| result [(x + 1)].equals ("i"))
                    total += 1;
                else
                    total -= 1;
            }
            else if (result [x].equals ("V") || result [x].equals ("v"))
            {
                if (x == result.length || result [(x + 1)].equals ("I") || result [(x + 1)].equals ("i")|| result [x + 1].equals ("V") || result [x + 1].equals ("v"))
                    total += 5;
                else
                    total -= 5;
            }
            else if (result [x].equals ("X") || result [x].equals ("x"))
            {
                if (x == result.length || result [x + 1].equals ("I") || result [x + 1].equals ("V") || result [x + 1].equals ("i") || result [x + 1].equals ("v") || result [x + 1].equals ("X") || result [x + 1].equals ("x"))
                    total += 10;
                else
                    total -= 10;
            }
            else if (result [x].equals ("L") || result [x].equals ("l"))
            {
                if (x == result.length || result [x + 1].equals ("I") || result [x + 1].equals ("V") || result [x + 1].equals ("i") || result [x + 1].equals ("v") || result [x + 1].equals ("X") || result [x + 1].equals ("x") || result [x + 1].equals ("L") || result [x + 1].equals ("l"))
                    total += 50;
                else
                    total -= 50;
            }
            else if (result [x].equals ("C") || result [x].equals ("c"))
            {
                if (x == result.length || result [x + 1].equals ("I") || result [x + 1].equals ("V") || result [x + 1].equals ("i") || result [x + 1].equals ("v") || result [x + 1].equals ("X") || result [x + 1].equals ("x") || result [x + 1].equals ("L") || result [x + 1].equals ("l") || result [x + 1].equals ("C") || result [x + 1].equals ("c"))
                    total += 100;
                else
                    total -= 100;
            }
            else if (result [x].equals ("D") || result [x].equals ("d"))
            {
                if (x == result.length || result [x + 1].equals ("I") || result [x + 1].equals ("V") || result [x + 1].equals ("i") || result [x + 1].equals ("v") || result [x + 1].equals ("X") || result [x + 1].equals ("x") || result [x + 1].equals ("L") || result [x + 1].equals ("l") || result [x + 1].equals ("C") || result [x + 1].equals ("c") || result [x + 1].equals ("D") || result [x + 1].equals ("d"))
                    total += 500;
                else
                    total -= 500;
            }
            else if (result [x].equals ("D") || result [x].equals ("d"))
            {
                if (x == result.length || result [x + 1].equals ("I") || result [x + 1].equals ("V") || result [x + 1].equals ("i") || result [x + 1].equals ("v") || result [x + 1].equals ("X") || result [x + 1].equals ("x") || result [x + 1].equals ("L") || result [x + 1].equals ("l") || result [x + 1].equals ("C") || result [x + 1].equals ("c") || result [x + 1].equals ("D") || result [x + 1].equals ("d") || result [x + 1].equals ("M") || result [x + 1].equals ("m"))
                    total += 1000;
                else
                    total -= 1000;
            }

        }
        c.println (total);
        // Place your program here.  'c' is the output console
    } // main method
} // RomanNumerals class

It essentially reads a Roman numeral with spacces between it and gives its value. When I run it, it compiles fine, but this message comes up when I put in a number:
java.lang.ArrayIndexOutOfBoundsException: 1
at RomanNumerals.main(RomanNumerals.java:23)

The line on which the error occurs changes depending on whichever letter I put in. (here I put in an "i", and the error was on line 23).

Thanks for all your help guys! (Sorry if I don't respond quickly)

EDIT: The program didn't come out indented. Sorry about that.

The author is not responsible for any bad puns, jokes, or other jackassy things. Thank you.
MarcoND7 on

Posts

  • Shazkar ShadowstormShazkar Shadowstorm Registered User regular
    edited November 2009
    next time use the code tags
    in fact edit it and use the code tags and then i'll read it because without indents its blahhh

    Shazkar Shadowstorm on
    poo
  • MarcoND7MarcoND7 Registered User regular
    edited November 2009
    next time use the code tags
    in fact edit it and use the code tags and then i'll read it because without indents its blahhh

    What do you mean by code tags? I just copy and pasted. :P

    MarcoND7 on
    The author is not responsible for any bad puns, jokes, or other jackassy things. Thank you.
  • Shazkar ShadowstormShazkar Shadowstorm Registered User regular
    edited November 2009
    its basically because your x in your for loop goes from 0 to less than the length of result

    right

    but then you try to access result(x+1)
    and that means you are accessing result(length of result) on your last loop

    thus you are out of the array bounds, which indexes from 0 to length of result - 1

    Shazkar Shadowstorm on
    poo
  • Shazkar ShadowstormShazkar Shadowstorm Registered User regular
    edited November 2009
    MarcoND7 wrote: »
    next time use the code tags
    in fact edit it and use the code tags and then i'll read it because without indents its blahhh

    What do you mean by code tags? I just copy and pasted. :P

    i mean using [code]

    Shazkar Shadowstorm on
    poo
  • MarcoND7MarcoND7 Registered User regular
    edited November 2009
    its basically because your x in your for loop goes from 0 to less than the length of result

    right

    but then you try to access result(x+1)
    and that means you are accessing result(length of result) on your last loop

    thus you are out of the array bounds, which indexes from 0 to length of result - 1


    So you are saying I should make x equal to 1 at the beginning of the for loop?

    What I'm confused about is how does x become less than zero? Thanks again for your help, and I put the code tags in.

    MarcoND7 on
    The author is not responsible for any bad puns, jokes, or other jackassy things. Thank you.
  • GanluanGanluan Registered User regular
    edited November 2009
    No, what he's saying is in your final loop (where x = the length of your array) don't do any of the checks on what's in index x + 1. That is an out of bounds index, and you don't need to read it anyway because there's nothing there.

    Ganluan on
  • DaenrisDaenris Registered User regular
    edited November 2009
    MarcoND7 wrote: »
    its basically because your x in your for loop goes from 0 to less than the length of result

    right

    but then you try to access result(x+1)
    and that means you are accessing result(length of result) on your last loop

    thus you are out of the array bounds, which indexes from 0 to length of result - 1


    So you are saying I should make x equal to 1 at the beginning of the for loop?

    What I'm confused about is how does x become less than zero? Thanks again for your help, and I put the code tags in.

    x doesn't become less than 0. He said you're accessing result[x+1], but x goes from 0 up to the length of result-1. So on the last iteration of the loop, x = (result.length-1), so when you try to access result[x+1] it's beyond the bounds of the array.

    Say I have an array that's length 5. The indices of the array will be 0, 1, 2, 3, and 4. So on my last run through the loop, x = 4, but I'm trying to access index x+1 in the array, and there is no index of 5 in my array.

    Daenris on
  • TechBoyTechBoy Registered User regular
    edited November 2009
    I would really, really, reaaaaallllly recommend that you reconsider your algorithm :P

    So your program basically takes in a string of Roman numerals and turns them into decimal numbers, right? XVI becomes 16, MMIX becomes 2009, etc.

    Right now, what you're doing is reading in a character and then checking to see if the character after it is going to be an "ordinary" case. There are two issues with this:
    1) What if you're at the last number? (Answer, you get the out of bounds error you're currently experiencing)
    2) After a while, your if statements begin to look ridiculous.

    You can easily fix both issues by just reconsidering your algorithm.

    If you want a hint, consider that right now you're checking for the "ordinary" case. Wouldn't it make more sense to check for the less-common "special" case?

    TechBoy on
    tf2_sig.png
  • JHunzJHunz Registered User regular
    edited November 2009
    Also, for the sanity of anyone who has to look at this code, please look into the String.equalsIgnoreCase method.

    Edit: And watch out for copy-paste errors. You've got a serious one in your code block to add 1000.

    JHunz on
    bunny.gif Gamertag: JHunz. R.I.P. Mygamercard.net bunny.gif
  • exmelloexmello Registered User regular
    edited November 2009
    General hint for the future: If you find your self copy-pasting a chunk of text more than 3 times, there's a better way to do it.

    exmello on
  • MarcoND7MarcoND7 Registered User regular
    edited November 2009
    Thanks for all your help guys, I understand what you mean by an out of bounds error.

    I will be taking your advice and trashing the whole thing and starting from the ground up.

    I'll be able to figure it out now. PA Forums have proven themselves once again. Thanks! ;)

    MarcoND7 on
    The author is not responsible for any bad puns, jokes, or other jackassy things. Thank you.
  • KlorgnumKlorgnum Registered User regular
    edited November 2009
    exmello wrote: »
    General hint for the future: If you find your self copy-pasting a chunk of text more than 3 times, there's a better way to do it.

    This. When you're repeating what is essentially the same code over and over, why not just put it into a function that you can pass the important functions into? It's much more concise and readable to do it that way.

    Also, comment your code as you go. If you stopped working on this for a week and came back to it, I have very little doubt that you'd have a hard time figuring out what it was doing and how to work on it. It gets more and more important to do this as your projects get bigger.

    Klorgnum on
Sign In or Register to comment.