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