0

I wrote the code below to get a sequence of integers as input and, split them into 2 parts based on a position number k which is given by the user. Numbers larger than the specified number will be added to a list, and the others to another list.

Here'when i run program Variable nums might not have been initialized compile time error is displayed. Can anyone give a solution.

(As i'm a beginner, i run java programs in Command Prompt)

If you are giving an alternative, please explain why this is happening.

import java.util.*;
import java.util.Scanner;

class StringSplit{
public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        List<Integer> part1=new ArrayList<Integer>();
        List<Integer> part2=new ArrayList<Integer>();
        int k,point=0;
        int nums[];         //Declaring variable

        System.out.print("\nEnter the point of split : ");
        k=input.nextInt();

        boolean correct=false;
        String numbers;
        System.out.print("\nEnter the number list : ");
        numbers=input.nextLine();
        String numlist[]=numbers.split(" ");

        nums=new int[numlist.length];       //Declaring the size
        for(int i=0;i<numlist.length;i++){
                nums[i]=Integer.parseInt(numlist[i]);    //Assigning values to the array
        }   


        point=nums[k-1];
        for(int x=0;x<nums.length-1;x++){
                if(nums[x]>point)   part1.add(nums[x]);
                else    part2.add(nums[x]);
        }

        System.out.print("\nPart 1 : ");
        for(int i:part1){
                System.out.print(" "+i);
        }   

        System.out.print("\nPart 2 : ");
        for(int j:part2){
                System.out.print(" "+j);
        }   

}
}
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34

4 Answers4

1

Local variable should be initialized Before making use of the variable.

So assign initial value to variable k. for e.g.

int k = 0;

But as you have this line already-

k=input.nextInt();

You should not get the error.

If you comment this line you will get error at

point=nums[k-1]; 

where you are using the variable.

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
  • have you tried above code? i dont thin this should give CTE – ajay.patel Sep 09 '14 at 12:26
  • Because of this line you are not getting that error - `k=input.nextInt();`. comment this line and see. – Ninad Pingale Sep 09 '14 at 12:29
  • Now the compile time error is gone, but there is a run time error right after the 2nd 'input.nextLine()'. ' Enter the point of split : 2 Enter the number list : Exception in thread "main" java.lang.NumberFormatExcepti on: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException. java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.parseInt(Integer.java:527) at StringSplit.main(StringSplit.java:23) May be the error ois with my computer – Imesha Sudasingha Sep 09 '14 at 12:43
  • This could be the problem http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – schlagi123 Sep 09 '14 at 12:55
0

Kindly recheck the code as i couldn't able to reproduce and get the same error.

Check whether all the local variables are initialized. In your given code, it is initialized. But check the code that you have shared here.

maddy
  • 19
  • 1
0

Declare local variables when you really need it. In this case:

int nums[] = new int[numlist.length];

Right before the loop.

Arnold Galovics
  • 3,246
  • 3
  • 22
  • 33
0

You must declare nums to :

int nums[] = new int[taille] ;
Joël Salamin
  • 3,538
  • 3
  • 22
  • 33
VincentDEJ
  • 239
  • 2
  • 10