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

Thread: I need help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help

    hey,
    I`m new in this forum and this is my first experiance in such forums....
    Something about me: my name is Marijana, I`am a 19 years old and i am study...and don`t know what to say another...
    Now I need help from you.My code below is for multipling two binary number in IEEE 735 standard and I have this
    package proekt2;
     
    public class ArrayUtils{
     
        static String[] lookupTable = {
            "0+0+0=00",
            "0+0+1=01",
            "0+1+0=01",
            "0+1+1=10",
            "1+0+0=01",
            "1+0+1=10",
            "1+1+0=10",
            "1+1+1=11",
        };
       public  static String lookup(char b1, char b2, char c) {
            String formula = String.format("%c+%c+%c=", b1, b2, c);
            for (String s : lookupTable) {
     
                if (s.startsWith(formula)) {
                    return s.substring(s.indexOf("=") + 1);
     
                }
            }
            throw new IllegalArgumentException();
     
        }
       public static String zeroPad(String s, int length) {
            while (s.length() < length) {
                s = "0" + s;
            }
            return s;
        }
        public static String add(String s1, String s2) {
            int length = Math.max(s1.length(), s2.length());
            s1 = zeroPad(s1, length);
            s2 = zeroPad(s2, length);
            String result = "";
            char carry = '0';
            for (int i = length - 1; i >= 0; i--) {
                String columnResult = lookup(s1.charAt(i), s2.charAt(i), carry);
                result = columnResult.charAt(1) + result;
                carry = columnResult.charAt(0);
            }
            if (carry == '1') {
                result = carry + result;
            }
            return result;
        }
     
     public static String multiply(String s1, String s2) {
        String result = "";
        String zeroSuffix = "";
        for (int i = s2.length() - 1; i >= 0; i--) {
            if (s2.charAt(i) == '1') {
                result = add(result, s1 + zeroSuffix);
            }
            zeroSuffix += "0";
        }
        return result;}
    }

    i call in another class with in this way
    String proiz=null;
      int pBrojac=0;
        System.out.print(ArrayUtils.add(binaren, binaren2));
        System.out.println();
        System.out.print(ArrayUtils.zeroPad(proiz, pBrojac));
        System.out.println();
        System.out.print(ArrayUtils.multiply(binaren, binaren2));


    and the mistake is this

    Exception in thread "main" java.lang.IllegalArgumentException
    at proekt2.ArrayUtils.lookup(ArrayUtils.java:24)
    at proekt2.ArrayUtils.add(ArrayUtils.java:40)
    at proekt2.Kraj.main(Kraj.java:188)
    Java Result: 1

  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I need help

    Hello Marijana.
    Welcome to the Java Programming Forums.

    Don't forget you need to have the binaren and binaren2 variables..

    		String proiz = null;
    		int pBrojac = 0;
     
    		String binaren = "binaren";
    		String binaren2 = "binaren2";
     
    		System.out.print(ArrayUtils.add(binaren, binaren2));
    		System.out.println();
    		System.out.print(ArrayUtils.zeroPad(proiz, pBrojac));
    		System.out.println();
    		System.out.print(ArrayUtils.multiply(binaren, binaren2));

    Looking at your code, you are throwing the IllegalArgumentException() which would suggest there is a problem here:

       public  static String lookup(char b1, char b2, char c) {
            String formula = String.format("%c+%c+%c=", b1, b2, c);
            for (String s : lookupTable) {
     
                if (s.startsWith(formula)) {
                    return s.substring(s.indexOf("=") + 1);
     
                }
            }
            throw new IllegalArgumentException();
     
        }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help

    yes, i know that there is the problem but u don`t know how to repair it