Question

In: Computer Science

JAVA JAVA JAVA . I need to convert a string input to int array, for example...

JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.

Solutions

Expert Solution

For converting string to an int you can use the java method Integer.parseInt()

But this is valid only for a string which is of length more than 0 and is a valid integer.

That is why in you case we cannot directly use this method, instead we have to convert your input in usable format.

For this

  • We will first read all of input in a String.
  • Traverse this string and whenever we encounter a digit for the first time, we start putting it in a number.
  • Once started when we face a non digit character or end of string we stop making this number and then store it in array

Please take a look at the Java code below for better understanding.

import java.util.*;

public class StringToIntArray{
        //method which takes in input string and parses int array from it
        static ArrayList<Integer> convert(String inp){
                ArrayList<Integer> arr = new ArrayList<Integer>();

                //return blank array if input is empty
                if(inp.length()==0)
                        return arr;

                int num=0;
                //this variable keeps a track on wheather we have started making out number
                boolean framing = false; 
                int i=0;
                while(i<inp.length()){
                        //check if it is a digit
                        char x = inp.charAt(i);
                        if(x>='0'&&x<='9'){
                                framing=true;
                                num=num*10+(x-'0'); //extract the numerical digit from char representation
                        }else{
                                if(framing){
                                        //if this character is not a digit and we were making the number,
                                        //then it is time to stop
                                        arr.add(num);

                                        //prepare for next run
                                        num=0;
                                        framing=false;
                                }
                        }
                        i++;
                }
                //Once string ends we might still have a number in process which needs to be added
                if(framing){
                                        //if this character is not a digit and we were making the number,
                                        //then it is time to stop
                                        arr.add(num);

                                        //prepare for next run
                                        num=0;
                                        framing=false;
                }
                return arr;
        }
    public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter string with integers.");
      //read all of input in one string
      String inp = sc.nextLine();

      //call conversion method
      ArrayList<Integer> arr = convert(inp);
      System.out.println("Elements in INT array");
      //print output array
      for(int i=0;i<arr.size();i++){
        System.out.print(arr.get(i)+" ");
      }
    }
}

Sample Output


Related Solutions

Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of...
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of strings that have been sorted in order of increasing length, for ties in the string lengths they will be in alphabetical order. The method also takes in a search string and range of indexes (start, end). The method will return a String formatted with the path of search ranges followed by a decision (true or false), see the examples below. Example 1: binarySearchLengths({"a","aaa","aaaaa"},"bb",0,2) would...
i need a pseudocode for a program in java that will convert dollars into euros and...
i need a pseudocode for a program in java that will convert dollars into euros and japanese yen using the print and prinln methods an if, if -else, statement a switch statement a while statement utilizes the file class uses the random class and random number generator and uses at least three methods other than main
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I...
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I want to be able to scan and output which number appears the most and the least. int x =1000 int[] array = new array[x] for(int i = 0 ; i < x; i++){ array[i] = random.nextInt(101); }
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT