Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 730997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:58:33+00:00 2026-05-14T06:58:33+00:00

First of all, I’m fairly new to Java, so sorry if this question is

  • 0

First of all, I’m fairly new to Java, so sorry if this question is utterly simple.

The thing is: I have a String[] s made by splitting a String in which every item is a number. I want to cast the items of s into a int[] n.

s[0] contains the number of items that n will hold, effectively s.length-1. I’m trying to do this using a foreach loop:

int[] n;
for(String num: s){
    //if(n is not initialized){
        n = new int[(int) num];
        continue;
    }
    n[n.length] = (int) num;
}

Now, I realize that I could use something like this:

int[] n = new int[(int) s[0]];
for(int i=1; i < s.length; i++){
    n[i-1] = (int) s[i];
}

But I’m sure that I will be faced with that “if n is not initialized initialize it” problem in the future.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:58:33+00:00Added an answer on May 14, 2026 at 6:58 am

    You can’t cast a String to an int. Java is strongly typed, and there is no implicit type conversion like you might find in a scripting language.

    To convert a String to an int, use an explicit conversion, like Integer.parseInt(String).

    All member variables and elements of arrays are initialized with a default value. For int types, the value is 0. For reference types (any subtype of Object), the default value is null. Local variables don’t get a default value, but the compiler analyzes the code to ensure that a value is assigned before the variable is read. If not, the code will not compile.

    I think what you want is something like this:

    int[] n = new int[Integer.parseInt(s[0]);
    for (int idx = 0; idx < n; ++idx)
      n[idx] = Integer.parseInt(s[idx + 1]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I'm quite new to the Android and JAVA world (coming from
First of all there is probably a question like this already but i couldn't
First of all, apologize because I have seen some posts about this, but I
First of all this is all just concept, I have no actual programming done
(First of all, sorry for my english, it's not my first language) I have
i have this code which i use in order to extract first all users
I have this code below, with which I select first all id's from table
First of all this is tough thing to solve, so far I didn't come
First of all, I know how to build a Java application. But I have
First of all, this isn't for a keylogger, it's for an input in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.