Search in sources :

Example 36 with InputMismatchException

use of java.util.InputMismatchException in project Java by TheAlgorithms.

the class LowestBasePalindrome method main.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = 0;
    while (true) {
        try {
            System.out.print("Enter number: ");
            n = in.nextInt();
            break;
        } catch (InputMismatchException e) {
            System.out.println("Invalid input!");
            in.next();
        }
    }
    System.out.println(n + " is a palindrome in base " + lowestBasePalindrome(n));
    System.out.println(base2base(Integer.toString(n), 10, lowestBasePalindrome(n)));
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException)

Example 37 with InputMismatchException

use of java.util.InputMismatchException in project j2objc by google.

the class InputMismatchExceptionTest method test_Constructor.

/**
 * java.util.InputMismatchException#InputMismatchException()
 */
@SuppressWarnings("cast")
public void test_Constructor() {
    InputMismatchException exception = new InputMismatchException();
    assertNotNull(exception);
    assertTrue(exception instanceof NoSuchElementException);
    assertTrue(exception instanceof Serializable);
}
Also used : Serializable(java.io.Serializable) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 38 with InputMismatchException

use of java.util.InputMismatchException in project jvm-serializers by eishay.

the class MediaContent method unmarshal.

/**
	 * Deserializes the object.
	 * @param buf the data source.
	 * @param offset the initial index for {@code buf}, inclusive.
	 * @return the final index for {@code buf}, exclusive.
	 * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
	 * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
	 * @throws InputMismatchException when the data does not match this object's schema.
	 */
public int unmarshal(byte[] buf, int offset) {
    int i = offset;
    try {
        byte header = buf[i++];
        if (header == (byte) 0) {
            int length = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                length |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (length > colferListMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.mediaContent.images length %d exceeds %d elements", length, colferListMax));
            Image[] a = new Image[length];
            for (int ai = 0; ai < length; ai++) {
                Image o = new Image();
                i = o.unmarshal(buf, i);
                a[ai] = o;
            }
            this.images = a;
            header = buf[i++];
        }
        if (header == (byte) 1) {
            this.media = new Media();
            i = this.media.unmarshal(buf, i);
            header = buf[i++];
        }
        if (header != (byte) 0x7f)
            throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
    } catch (IndexOutOfBoundsException e) {
        if (i - offset > colferSizeMax)
            throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
        if (i >= buf.length)
            throw new BufferUnderflowException();
        throw new RuntimeException("colfer: bug", e);
    }
    if (i - offset > colferSizeMax)
        throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
    return i;
}
Also used : InputMismatchException(java.util.InputMismatchException) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 39 with InputMismatchException

use of java.util.InputMismatchException in project jvm-serializers by eishay.

the class Media method unmarshal.

/**
	 * Deserializes the object.
	 * @param buf the data source.
	 * @param offset the initial index for {@code buf}, inclusive.
	 * @return the final index for {@code buf}, exclusive.
	 * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
	 * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
	 * @throws InputMismatchException when the data does not match this object's schema.
	 */
public int unmarshal(byte[] buf, int offset) {
    int i = offset;
    try {
        byte header = buf[i++];
        if (header == (byte) 0) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.media.uri size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.uri = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 1) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.media.title size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.title = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 2) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.width = x;
            header = buf[i++];
        } else if (header == (byte) (2 | 0x80)) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.width = -x;
            header = buf[i++];
        }
        if (header == (byte) 3) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.height = x;
            header = buf[i++];
        } else if (header == (byte) (3 | 0x80)) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.height = -x;
            header = buf[i++];
        }
        if (header == (byte) 4) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.media.format size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.format = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 5) {
            long x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                if (shift == 56 || b >= 0) {
                    x |= (b & 0xffL) << shift;
                    break;
                }
                x |= (b & 0x7fL) << shift;
            }
            this.duration = x;
            header = buf[i++];
        } else if (header == (byte) (5 | 0x80)) {
            long x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                if (shift == 56 || b >= 0) {
                    x |= (b & 0xffL) << shift;
                    break;
                }
                x |= (b & 0x7fL) << shift;
            }
            this.duration = -x;
            header = buf[i++];
        }
        if (header == (byte) 6) {
            long x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                if (shift == 56 || b >= 0) {
                    x |= (b & 0xffL) << shift;
                    break;
                }
                x |= (b & 0x7fL) << shift;
            }
            this.size = x;
            header = buf[i++];
        } else if (header == (byte) (6 | 0x80)) {
            long x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                if (shift == 56 || b >= 0) {
                    x |= (b & 0xffL) << shift;
                    break;
                }
                x |= (b & 0x7fL) << shift;
            }
            this.size = -x;
            header = buf[i++];
        }
        if (header == (byte) 7) {
            int length = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                length |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (length > colferListMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.media.persons length %d exceeds %d elements", length, colferListMax));
            String[] a = new String[length];
            for (int ai = 0; ai < length; ai++) {
                int size = 0;
                for (int shift = 0; true; shift += 7) {
                    byte b = buf[i++];
                    size |= (b & 0x7f) << shift;
                    if (shift == 28 || b >= 0)
                        break;
                }
                if (size > colferSizeMax)
                    throw new SecurityException(format("colfer: field serializers/colfer/media.media.persons size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
                int start = i;
                i += size;
                a[ai] = new String(buf, start, size, this._utf8);
            }
            this.persons = a;
            header = buf[i++];
        }
        if (header == (byte) 8) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.media.copyright size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.copyright = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 9) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.bitrate = x;
            header = buf[i++];
        } else if (header == (byte) (9 | 0x80)) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.bitrate = -x;
            header = buf[i++];
        }
        if (header == (byte) 10) {
            this.hasBitrate = true;
            header = buf[i++];
        }
        if (header == (byte) 11) {
            this.flashPlay = true;
            header = buf[i++];
        }
        if (header == (byte) 12) {
            this.javaPlay = true;
            header = buf[i++];
        }
        if (header != (byte) 0x7f)
            throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
    } catch (IndexOutOfBoundsException e) {
        if (i - offset > colferSizeMax)
            throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
        if (i >= buf.length)
            throw new BufferUnderflowException();
        throw new RuntimeException("colfer: bug", e);
    }
    if (i - offset > colferSizeMax)
        throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
    return i;
}
Also used : InputMismatchException(java.util.InputMismatchException) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 40 with InputMismatchException

use of java.util.InputMismatchException in project Gaffer by gchq.

the class AndTest method shouldThrowExceptionWhenInputTypesAreNotCompatible.

@Test
public void shouldThrowExceptionWhenInputTypesAreNotCompatible() {
    // Given
    final ConsumerFunctionContext<Integer, FilterFunction> funcContext1 = mock(ConsumerFunctionContext.class);
    final ConsumerFunctionContext<Integer, FilterFunction> funcContext2 = mock(ConsumerFunctionContext.class);
    final FilterFunction func1 = mock(FilterFunction.class);
    final FilterFunction func2 = mock(FilterFunction.class);
    final And and = new And(Arrays.asList(funcContext1, funcContext2));
    given(func1.getInputClasses()).willReturn(new Class<?>[] { Integer.class, Date.class });
    given(func2.getInputClasses()).willReturn(new Class<?>[] { String.class, Long.class, Double.class });
    given(funcContext1.getFunction()).willReturn(func1);
    given(funcContext2.getFunction()).willReturn(func2);
    given(funcContext1.getSelection()).willReturn(Arrays.asList(0, 1));
    given(funcContext2.getSelection()).willReturn(Arrays.asList(1, 2, 3));
    // When / Then
    try {
        and.getInputClasses();
        fail("Exception expected");
    } catch (InputMismatchException e) {
        assertNotNull(e);
    }
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) InputMismatchException(java.util.InputMismatchException) FilterFunctionTest(uk.gov.gchq.gaffer.function.FilterFunctionTest) Test(org.junit.Test)

Aggregations

InputMismatchException (java.util.InputMismatchException)82 Scanner (java.util.Scanner)56 NoSuchElementException (java.util.NoSuchElementException)47 Locale (java.util.Locale)23 BufferUnderflowException (java.nio.BufferUnderflowException)5 Pattern (java.util.regex.Pattern)5 BufferedReader (java.io.BufferedReader)4 BigInteger (java.math.BigInteger)4 UnicodeReader (gdsc.core.utils.UnicodeReader)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 TodoList (de.djuelg.neuronizer.domain.model.preview.TodoList)2 TodoListHeader (de.djuelg.neuronizer.domain.model.todolist.TodoListHeader)2 Point (java.awt.Point)2 Serializable (java.io.Serializable)2 BigDecimal (java.math.BigDecimal)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1