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

3
  • first of all you you need to declare your variable x its not declared anywhere in the code. The for loop should look something like this for(int x=0;x<=1;x++) Commented Dec 24, 2015 at 7:01
  • please post the error Commented Dec 24, 2015 at 7:02
  • Please include the errors in the question. Don't post screenshots, or links to screenshots. Commented Dec 24, 2015 at 7:29

6 Answers 6

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

}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

try { id = Integer.parseInt(stringValue); } catch (NumberFormatException ex) { }
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); 
        }

Comments

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

Comments

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

Comments

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.