-2
        int[] arr1 = new int[4];
        int[] arr2 = new int[2];
   \\this is a method to scan the arrays
         for (int i = 0; i < arr.length; i++) {
            try {
                if (input.hasNext()) {
                    arr[i] = input.nextInt();
                }
            } catch (Exception e) {
                System.out.println("Finished!");
            }
       \\at the main function 
            System.arraycopy(arr1, 0, arr3, 0, arr1.length);
            System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
            for (int i = 0; i < arr3.length; i++) {
            System.out.print(arr3[i] + " ");
        }

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

Thank you all so much!

4 Answers4

3

Try,

while(input.hasNextInt())
   input.nextInt();
}
Bgvv1983
  • 1,256
  • 1
  • 13
  • 27
0

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:

  1. 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.
  2. 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] + " ");
     }
}
Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • 1. 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. 2. `nextInt` in the if reads (and consumes) the integer - the first value - so only every second value will be saved in `arr` – user85421 Jun 02 '17 at 06:13
  • Ok thank you I will update it. I hope to point it out but forgotten. – Blasanka Jun 02 '17 at 06:16
  • thank you .. it worked but the output is not correct... i updated the code and posted it back .... i appreciate your help – Matt Andrew Jun 02 '17 at 12:43
  • please donot change the whole question because everyone answer became wrong. Rather than that ask a new question. Anyway, I updated and added the program for your question. Take a look at it. Please please dont change whole question anymore and any question because Its affecting others answers also. – Blasanka Jun 02 '17 at 16:59
0

you can use

public static void main(String[] args) 
 {
     Scanner in = new Scanner(System.in);
     int a[]=new int[10];
     try
     {
         for(int i=0;i<10;i++){
             a[i]=in.nextInt();
         }
     }
     catch(Exception e)
     {
         System.out.println("quit");
     }

 }

Since any char input will throw an exception catch section will execute. and about joining two arrays: Since you have asked simple way:

int a[]={1,2,3,4};
int b[]={5,6,7};
int c[]=new int[a.length+b.length];
int j=0;

for(int i=0;i<a.length;i++){
    c[j]=a[i];
    j++;
   }
for(int i=0;i<b.length;i++){
    c[j]=b[i];
    j++;
   }
for(int i=0;i<c.length;i++){
    System.out.println(c[i]);
   }
Sumit raj
  • 821
  • 1
  • 7
  • 14
-1

If you want to get first character from the input, write below.

    Scanner scan=new Scanner(System.in);
    if(scan.next().charAt(0)=='a'){
        System.out.println("correct");
    }
Yoonsung Lee
  • 115
  • 1
  • 9