-1

I suddenly get struck to initiate String array in the part of a program. The idea is to read String input from the Scanner and make an array of String. I wrote it as following,

    Scanner sc = new Scanner(System.in);

    String parts [] ; 

    while(sc.hasNext() ){

        String str = sc.nextLine(); 

        // the str value suppose to be *1, 2, 3, 4, 5, 99, 1, 2, 3, 4, 5*
        parts = new String[] { str.split(", ")}; // need correction 

    } 

I actually need an Integer array, but, I would better do it in next iteration using

Ingeter.valueOf(str_value)

How to write properly the String array generation inside the while loop ?

Monica Hübner
  • 269
  • 1
  • 4
  • 16
  • 1
    What is the return type of `str.split(", ")`? – Sotirios Delimanolis Nov 24 '15 at 22:49
  • 2
    "String parts [] ;" is not even a proper declaration. It should be "String[] parts;" – Arsen Nov 24 '15 at 22:50
  • 2
    @Arsen Nope. `String parts[]` is a valid java declaration. – Zizouz212 Nov 24 '15 at 22:51
  • 2
    @Arsen it is proper, simply not usually used (it was added so C coders would feel better in Java). – Pshemo Nov 24 '15 at 22:51
  • Actually, both of the declaration is correct. Yes, the str.split(", ") is already an array. But, I can't just access it outside of the while loop. The compiler is telling that the variable parts is not initiated. – Monica Hübner Nov 24 '15 at 22:54
  • @Zizouz212 Ok. You got me. Now I'm really surprised. – Arsen Nov 24 '15 at 22:54
  • Nah, don't worry. I write all my code as `String[] parts` just because you can read it normally :) But for some reason, both compile fine, even those `String parts[]` is not common at all... :/ – Zizouz212 Nov 24 '15 at 22:56

3 Answers3

4

split is already returning String[] array so simply assign it to your String parts [] reference:

parts = str.split(", ");
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Okay, then why can't I access it outside of the while loop ? I wrote another for loop and compiler is tells the Sting array variable is not initiated. for (String s: parts ){ System.out.println(s); } – Monica Hübner Nov 24 '15 at 22:52
  • @MonicaHübner: What happens if there is no user input? `while (sc.hasNext())` might never be true. (Frankly, the simplest solution is probably just not to have a loop at all, and just unconditionally get a line.) – Louis Wasserman Nov 24 '15 at 22:53
  • @MonicaHübner Because according to compiler there is possibility that you will never enter your `while(sc.hasNext() )` (when `hasNext` will return `false` at first call) in which case your array will still not be initialized. – Pshemo Nov 24 '15 at 22:53
  • perhaps, try/ catch will work in this case ? I have an phone interview in 5 minutes and I will back afterwards. – Monica Hübner Nov 24 '15 at 22:55
  • How about just initialize it to an empty array? – Mike Christensen Nov 24 '15 at 22:56
  • The issue is that Arrays can't grow. – Zizouz212 Nov 24 '15 at 22:57
  • @MikeChristensen You can initialize it with some value which will represent that this variable doesn't store array from user, like you can simply initialize `parts` with `null`. – Pshemo Nov 24 '15 at 22:58
  • @Zizouz212 - It doesn't need to grow. The `str.split()` call would overwrite the old reference, and the empty array would be lost and garbage collected later. – Mike Christensen Nov 24 '15 at 22:59
2

Seeing some confusion in comments, it may be more appropriate for you to use a List, rather than an array. Here's a working example:

List parts = new ArrayList();

while (sc.hasNext()) 
{
    String str = sc.readLine();

    for (String i : str.split(", ")) 
    {
        parts.add(Integer.valueOf(Integer.parseInt(i)));
    }
}
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
0

I provided the entire solution below where you can get one single element which is not in a pair.

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in); 
    boolean bol = false; 

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();

    while(sc.hasNext() ){

        String str = sc.nextLine(); 

        for (String s: str.split(", ") ){

            int t = Integer.valueOf(s); 
            map.put(t, map.containsKey(t) ? map.get(t) + 1 : 1);
        }      
    } 


    if (bol){

        for (Map.Entry<Integer, Integer> entry : map.entrySet() ){


        if ( entry.getValue() == 1){
            System.out.println(entry.getKey());
        }
    }
    }

    else {

        Iterator it = map.entrySet().iterator();

        while ( it.hasNext() ){

            Map.Entry pair = (Map.Entry) it.next();

            if ( pair.getValue() == 1 ){

                System.out.println(pair.getKey());
            }
        }

    }

}
Monica Hübner
  • 269
  • 1
  • 4
  • 16