arrays - How to take multiple integers as single input separated by space in java, then break them into integers? -
need take input 1 2 3 or of length separated space break separate integers. have tried array
, strings
, charat()
, string.split()
method not working.
input can 1 2 3 4 or 1 2 or of length, need separate 1,2,3 integers.
i have tried far:
class cube{ public static void main(string args[]){ int i,j=0,sum=0; int arr[]=new int[10]; scanner scan=new scanner(system.in); string s=scan.nextline(); int len =s.length(); for(i=0;i<=len;i++){ string[] str=s.split(" "); int i=str[j]; sum+=math.pow(3,i); j++; } } }
you not parsing string int. need use integer.parseint()
. also, use of bufferedreader
preferred.
class cube{ public static void main(string args[]){ int i,j=0,sum=0; int arr[]=new int[10]; scanner scan=new scanner(system.in); string s=scan.nextline(); string[] str=s.split(" "); for(i=0;i<=str.length;i++){ int temp = integer.parseint(str[i]); sum+=math.pow(3,temp); } } }
Comments
Post a Comment