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

• October 13th, 2013, 03:29 AM
spirates
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.

Quote:

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]);
}

}

}
Quote:

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)
• October 13th, 2013, 03:34 AM
GregBrannon
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?
• October 13th, 2013, 04:12 AM
spirates
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)
• October 13th, 2013, 04:26 AM
GregBrannon
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?
• October 13th, 2013, 04:34 AM
spirates
Re: I get error when putting in more then 10 digits in my vector
No idea :<
• October 13th, 2013, 04:43 AM
GregBrannon
Re: I get error when putting in more then 10 digits in my vector
• October 13th, 2013, 05:00 AM
spirates
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..
• October 13th, 2013, 05:07 AM
GregBrannon
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.
• October 13th, 2013, 11:18 PM
Junky
Re: I get error when putting in more then 10 digits in my vector
Quote:

Originally Posted by spirates
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!
• October 15th, 2013, 01:18 AM
moe091
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.