edodije
edodije

Reputation: 77

How to convert an String Variable with Array to an Integer Variable with Array

I am new to java programming. I am trying to convert an string variable with array to an int variable array

but i have 2 errors and have no idea to fix it,

any help would be great, thanks..

This is my source code :

import java.util.Scanner;
public class stringtoint {

    public static void main (String args[]) {
        Scanner in=new Scanner(System.in);
        String number[]=new String[100];
        int sum=0;
        for(x=0;x<=1;x++)
        {
            System.out.print("input number : ");number[x]=in.next();
            int value[x]= Integer.parseInt(number[x]); // i found "expected here", what should i do, need help, please..
            sum=sum+number[x];
        }

        for(x=0;x<=1;x++)
        {
            System.out.println("Data Number "+(x+1)+" : "+number[x]);   
        }
            System.out.println("Sum :\t "+sum); 
    }
}

This is what the errors look like

Upvotes: 1

Views: 971

Answers (6)

LowLevel
LowLevel

Reputation: 1095

public static void main(String args[]) {
    String[] exampleOfStringArray = {"11", "22", "33"/*, "ab", "cd", "ef", "", null*/};
    int[] intArray = getIntArray(exampleOfStringArray);
    int sum = getSumOf(exampleOfStringArray);
}

private static int[] getIntArray(String... stringArray) /*throws NumberFormatException*/ {
    return Arrays.<String>asList(stringArray).stream().mapToInt(Integer::parseInt).toArray();
}

private static int getSumOf(String... stringArray) /*throws NumberFormatException*/ {
    return Arrays.<String>asList(stringArray).stream().mapToInt(Integer::parseInt).sum();
}

Upvotes: 0

nagaraju
nagaraju

Reputation: 59

when you convert an array of stirngs to an array of integers, we should have an array of integers declared and in the code that you posted there are some syntax errors because you didnt declare integer array before use(int value[x])

and try the below code which will convert string array of numbers(string number[]) into an ineger array of numbers(int value[])

import java.util.Scanner;

public class stringtoint {

    public static void main (String args[]) {
        Scanner in=new Scanner(System.in);
        String number[]=new String[100];
        int value[]= new int[100]; // here I declared an array of integers with the name value
        int sum=0;
        for(int x= 0;x <= 1; x++)
        {
            System.out.print("input number : ");
            number[x]=in.next();
            value[x]= Integer.parseInt(number[x]); // i found "expected here", what should i do, need help, please..
            sum=sum + value[x];
        }
        for(int x=0; x<=1; x++)
        {
            System.out.println("Data Number "+(x+1)+" : "+number[x]);   
        }
        System.out.println("Sum :\t "+sum); 
    }

}

Upvotes: 4

shivam355
shivam355

Reputation: 22

Try below code, it is working.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String number[] = new String[100];
    int sum = 0;
    int noOfInputs = 2;
    int value[] = new int[noOfInputs];
    for (int x = 0; x <= noOfInputs-1; x++) {
        System.out.print("input number : ");
        number[x] = in.next();
        value[x] = Integer.parseInt(number[x]);
        sum = sum + value[x];
    }
    for (int x = 0; x <= noOfInputs-1; x++) {
        System.out.println("Data Number " + (x + 1) + " : " + number[x]);
    }
    System.out.println("Sum :\t " + sum);

}

Upvotes: -2

Vivek Singh
Vivek Singh

Reputation: 2073

There seems problem with declaration and usage of variable in you sample code.

  1. Variable x is not initialzed
  2. An integer value is assigned to and array declaration.

Try the below code to solve your issues.

import java.util.Scanner;

public class stringtoint {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String number[] = new String[100];
        int sum = 0;

        for (int x = 0; x <= 1; x++) {
            System.out.print("input number : ");
            number[x] = in.next();
            int value = Integer.parseInt(number[x]);
            sum = sum + value;
        }

        for (int x = 0; x <= 1; x++) {
            System.out.println("Data Number " + (x + 1) + " : " + number[x]);
        }
        System.out.println("Sum :\t " + sum);
        in.close();
    }
}

Upvotes: 0

Subhankar
Subhankar

Reputation: 692

Create a int array, then use it. int value[x]= Integer.parseInt(number[x]); is an error because your are trying to assign an integer to an array.

Correct one may be...

public static void main (String args[]) {
            Scanner in=new Scanner(System.in);
            String number[]=new String[100];
            int value[]= new int[100];
            int sum=0;
            for(int x=0;x<=1;x++)
            {
                System.out.print("input number : ");
                number[x]=in.next();
                value[x]= Integer.parseInt(number[x]);
                sum=sum+value[x];
            }

            for(int x=0;x<=1;x++)
            {
                System.out.println("Data Number "+(x+1)+" : "+number[x]);   
            }
                System.out.println("Sum :\t "+sum); 
        }

Upvotes: 0

Sunny
Sunny

Reputation: 111

Use in.nextInt() method.

Scanner in = new Scanner(System.in);
        int number[] = new int[100];
        int sum = 0;
        for (int x = 0; x <= 1; x++) {`enter code here`
            System.out.print("input number : ");
            number[x] = in.nextInt();
            sum = sum + number[x];
        }
        System.out.println("Sum :\t " + sum);
        in.close();
    }

Upvotes: 1

Related Questions