brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

What idea is emphasized through repetition

The a value of a function in the form f(x) = ax2 + bx + c is negative. which statement must be true?

What experimental evidence led to the development of this atomic model from the one before it?

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > Convert string to int array java
ds

Convert string to int array java

By John Published November 9, 2022
Share

This is the Java Program to Convert a String to an Integer Array.

Problem Description
Given a string consisting of various numbers separated by spaces, convert it into an array of Integers, such that every number occupies a position in the array, and for every invalid number, there is a -1 in the array.

Example:
For the String str = “19 46 8999 abc 133”;
The array will be [19, 46, 8999, -1, 133].

Problem Solution
The idea is to split the string into various substrings whenever space is encountered while traversing through the string, and then convert those substrings into the integers they represent.

Program/Source Code
Here is the source code of the Java Program to Convert a String to an Integer Array. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

//Java Program to Convert a String to an Integer Array

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringToIntegerArray {
// Function to convert String into an Integer array
static int[] toIntegerArray(String str){
int
String[] splitArray = str.split(” “);
int[] array = new int[splitArray.length];
for(i=0;i

TAGGED: Convert string to int array java
John November 9, 2022 November 9, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?