Splitting String and put it on int array

后端 未结 8 1360
眼角桃花
眼角桃花 2020-11-28 14:03

I have to input a string with numbers ex: 1,2,3,4,5. That\'s a sample of the input, then I have to put that in an array of INT so I can sort it but is not working the way it

相关标签:
8条回答
  • 2020-11-28 14:35

    You are doing Integer division, so you will lose the correct length if the user happens to put in an odd number of inputs - that is one problem I noticed. Because of this, when I run the code with an input of '1,2,3,4,5,6,7' my last value is ignored...

    0 讨论(0)
  • 2020-11-28 14:35

    Change the order in which you are doing things just a bit. You seem to be dividing by 2 for no particular reason at all.

    While your application does not guarantee an input string of semi colon delimited variables you could easily make it do so:

    package com;
    
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            // Good practice to initialize before use
            Scanner keyboard = new Scanner(System.in);
            String input = "";
            // it's also a good idea to prompt the user as to what is going on
            keyboardScanner : for (;;) {
                input = keyboard.next();
                if (input.indexOf(",") >= 0) { // Realistically we would want to use a regex to ensure [0-9],...etc groupings 
                    break keyboardScanner;  // break out of the loop
                } else { 
                    keyboard = new Scanner(System.in);
                    continue keyboardScanner; // recreate the scanner in the event we have to start over, just some cleanup
                }
            }
    
            String strarray[] = input.split(","); // move this up here      
            int intarray[] = new int[strarray.length];
    
            int count = 0; // Declare variables when they are needed not at the top of methods as there is no reason to allocate memory before it is ready to be used
            for (count = 0; count < intarray.length; count++) {
                intarray[count] = Integer.parseInt(strarray[count]);
            }
    
            for (int s : intarray) {
                System.out.println(s);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 14:38

    Something like this:

      public static void main(String[] args) {
       String N = "ABCD";
       char[] array = N.toCharArray();
    
       // and as you can see:
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);
      }
    
    0 讨论(0)
  • 2020-11-28 14:40

    For input 1,2,3,4,5 the input is of length 9. 9/2 = 4 in integer math, so you're only storing the first four variables, not all 5.

    Even if you fixed that, it would break horribly if you passed in an input of 10,11,12,13

    It would work (by chance) if you used 1,2,3,4,50 for an input, strangely enough :-)

    You would be much better off doing something like this

    String[] strArray = input.split(",");
    int[] intArray = new int[strArray.length];
    for(int i = 0; i < strArray.length; i++) {
        intArray[i] = Integer.parseInt(strArray[i]);
    }
    

    For future reference, when you get an error, I highly recommend posting it with the code. You might not have someone with a jdk readily available to compile the code to debug it! :)

    0 讨论(0)
  • 2020-11-28 14:51

    Let's consider that you have input as "1,2,3,4".

    That means the length of the input is 7. So now you write the size = 7/2 = 3.5. But as size is an int, it will be rounded off to 3. In short, you are losing 1 value.

    If you rewrite the code as below it should work:

    String input;
    int length, count, size;
    Scanner keyboard = new Scanner(System.in);
    input = keyboard.next();
    length = input.length();
    
    String strarray[] = input.split(",");
    int intarray[] = new int[strarray.length];
    
    for (count = 0; count < intarray.length ; count++) {
        intarray[count] = Integer.parseInt(strarray[count]);
    }
    
    for (int s : intarray) {
        System.out.println(s);
    }
    
    0 讨论(0)
  • 2020-11-28 14:52
    String input = "2,1,3,4,5,10,100";
    String[] strings = input.split(",");
    int[] numbers = new int[strings.length];
    for (int i = 0; i < numbers.length; i++)
    {
      numbers[i] = Integer.parseInt(strings[i]);
    }
    Arrays.sort(numbers);
    
    System.out.println(Arrays.toString(numbers));
    
    0 讨论(0)
提交回复
热议问题