Skip to main content

Posts

Showing posts with the label java-util-scanner

Scanner vs. StringTokenizer vs. String.Split

I just learned about Java's Scanner class and now I'm wondering how it compares/competes with the StringTokenizer and String.Split. I know that the StringTokenizer and String.Split only work on Strings, so why would I want to use the Scanner for a String? Is Scanner just intended to be one-stop-shopping for spliting?

Updating array in java

I'm facing a problem with this code,please anyone can help me? import java.util.Scanner; class A{ int p[]; int m,i; A(){ p=new int[][10,20,30,40,50]; } int sum(int p,int m){ p+=m; return p; } } class B{ int i,m; public static void main(String args[]){ A object=new A(); Scanner input=new input(System.in); m=input nextInt(); for(i=o;i<object.p.length;i++){ if(m==object.p[i]){ n=object.sum(object.p[i],m); object.p[i]=n; } } for(i=0;i<object.p.length;i++){ System.out.println(p[i]);} } When i am taking 10 as input then it matches with the first array value and then adds and becomes 20....But my problem is that when next time i will run the program again,the value will not be changed(with adding results)....... how can i update the value of p array after the method for further calculation:? ``