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: I get error when putting in more then 10 digits in my vector

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I get error when putting in more then 10 digits in my vector

    I've written this and whats happening is you type in a random number combination, and then the code will tell you how many unique numbers your number combination contains. For example typing in 5545 would result in a 2. This works well with up to 10 numbers, if you type more then that I get a error.. Here is the code and down at bottom is the error message i get in eclipse.



    import java.util.Random;

    import se.lth.cs.window.SimpleWindow;
    import se.lth.cs.p.ovn.turtle.Turtle;

    import java.util.Scanner;
    public class p5 {

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int apa=0;
    System.out.println("ange tal");
    int tal = scan.nextInt();
    boolean hej [] = new boolean[10];
    if(tal==0){
    hej[0]=true;
    System.out.println("awejawejwaejae");

    }
    while(tal>0){
    int z = tal%10;
    hej[z]=true;
    tal=tal/10;
    }



    for(int k=0; k<10; k++)
    if(hej[k]==true){
    apa++;
    }
    System.out.println(apa);
    for(int k=0; k<10; k++){
    System.out.println(hej[k]);
    }


    }

    }
    Exception in thread "main" java.util.InputMismatchException: For input string: "123123123123123"
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at p5.main(p5.java:15)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    What error? And doesn't your example have 3: 55, 54, and 45?

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    Exception in thread "main" java.util.InputMismatchException: For input string: "123123123123123"
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at p5.main(p5.java:15)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    That error means that the input "123123123123123" cannot be parsed to the int expected by the Scanner method. Any ideas why that might be?

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    No idea :<

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    The answer is on this page.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    cant you tell me whats wrong? I really dont have time right now to read all that..

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    You don't have time? Well, read it when you do. It's not going anywhere.

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

    Kewish (October 13th, 2013)

  10. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    Quote Originally Posted by spirates View Post
    cant you tell me whats wrong? I really dont have time right now to read all that..
    But you do have time to sit around, whine and wait for someone to post the answer so you can read it!
    Improving the world one idiot at a time!

  11. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get error when putting in more then 10 digits in my vector

    Haha I had to make an account just to help you out. Although I do see where the guy who just gave you the page to read is coming from, you will have to get used to researching and finding answers to these things on your own if you want to become a good programmer. It gets easier the more you do it is best to try and get good at it as soon as you can.

    Anyway, to solve your problem. an int in java is limited to a certain size. java int's are only (i totally forget how many) bits in size and therefore can only store as big a number as can fit within that many bits. If you need to be able to store bigger numbers, declare your variable as a 'long' instead of an 'int'. A long is just like an int but is bigger and can therefore store larger numbers.

Similar Threads

  1. Vector of array [logic error]
    By Rexshine in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 3rd, 2013, 04:02 PM
  2. putting comments in for code
    By BLUEJ in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2013, 07:52 PM
  3. Putting codes in order......
    By rdmighty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 1st, 2012, 12:51 PM
  4. Help in putting actions to buttons
    By juliusmasa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 14th, 2011, 07:22 AM
  5. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM