it is working but whenever the user input a character giving me an
error about mismatching since the scan input is type int and the input
that user typed is type character.
Reason is you are taking input as integer whenever you input character or string data types are different. Because of that compiler will give you InputMismatchException
.
So these type of error are called runtime errors. You can handle those by using throws
keyword or using try
-catch
block. For these type of program try-catch block is good.
Solution:
Wrap your user input from keyword like below:
try{
if(input.nextInt()!='a')
{
arr[i] = input.nextInt();
}
}catch(InputMismatchException e){
//here you can get error message using 'e' or you can print custom error as your wish.
System.out.println("Finished!");
}
Also what is the easiest way to combine two arrays
int[] firstArray= {3, 4, 4, 5};
int secondArray[] = {2, 4, 4, 5};
int fLen = firstArray.length;
int sLen = secondArray.length;
int thirdArray[] = new int[fLen+sLen];
System.arraycopy(firstArray, 0, thirdArray, 0, fLen);
System.arraycopy(secondArray, 0, thirdArray, fLen, sLen);
for(int i: thirdArray){
System.out.print(i + " ");
}
There is many ways to combine arrays. You can use List
also if you familiar with those. read this question for more info.
UPDATE:
Carlos Heuberger comment:
- do you really want to compare the read integer with the (ascii) code
of 'a'? if the length in the file is 97 this the same as 'a' and the
logic will fail.
- nextInt in the if reads (and consumes) the integer - the first value - so only every second value will be saved in arr.
Change:
Since you only want to get input to only for size of array,
if(input.nextInt()!='a')
{
arr[i] = input.nextInt();
}
To:
if(input.hasNext())
{
arr[i] = input.nextInt();
}
UPDATE(after upate the question):
please donot change the whole question because everyone answer became wrong. Rather than that ask a new question. Anyway,
I fixed it but i am the result is wrong ... the first array input is 4
5 h for the second array is 7 8 1 . the output is 4 5 0 0 7 8 .... i
don't want the zeros also the last element is messing.
0
because you are allocate memory for four indices for first array.
int[] arr1 = new int[4];
Last element missed because you only allcate memory for two indices.
Change:
int[] arr2 = new int[2];
To:
int[] arr2 = new int[3];
To skip the 0
's when displaying:
for (int a = 0; a < arr3.length; a++) {
if(arr3[a] != 0){
System.out.print(arr3[a] + " ");
}
}
This is the fixed program according to update question:
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] arr1 = new int[4];
int[] arr2 = new int[3];
int[] arr3 = new int[arr1.length + arr2.length];
for (int i = 0; i < arr1.length; i++) {
try {
if (input.hasNext()) {
arr1[i] = input.nextInt();
}
}catch (Exception e) {
}
System.arraycopy(arr1, 0, arr3, 0, arr1.length);
}
Scanner input2 = new Scanner(System.in);
for (int i = 0; i < arr2.length; i++) {
try {
if (input2.hasNext()) {
arr2[i] = input2.nextInt();
}
}catch (Exception e) {
}
System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
}
for (int a = 0; a < arr3.length; a++) {
if(arr3[a] != 0)
System.out.print(arr3[a] + " ");
}
}