1

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

Deepak Yadav
  • 6,804
  • 5
  • 31
  • 49
edodije
  • 67
  • 2
  • 11

6 Answers6

4

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); 
    }

}
nagaraju
  • 59
  • 6
1

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();
    }
Sunny
  • 111
  • 7
0

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); 
        }
Subhankar
  • 692
  • 7
  • 25
0

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();
    }
}
Vivek Singh
  • 2,047
  • 11
  • 24
0
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();
}
LowLevel
  • 1,085
  • 1
  • 13
  • 34
-2

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);

}
shivam355
  • 22
  • 5