Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Int from a String 1

Status
Not open for further replies.

ascotta

Vendor
Sep 15, 2003
7,394
AU
I have a file tokenized by various things and I have anteger field in this. This integer may = 1 or 10 or 15 or whatever, you get the picture. I am obviously being very thick here but I need to add these valuse up. I can't add a string(doh) how on earth do I do it. I cannot use anything else but String,StringBuffer,StringTokenizer, bufferedreader,inputStreamReader and Printwriter.

I know I could do it with new Integer(string().intvalue etc

Any bright ideas guys & guyesses


Cheers
Scott
 
i'd assume u can use the static method Integer.parseInt(str) before simply adding them up using the operator '+'...
but then i might not have got ur problem right.
 
I am not sure qwhether I can or not. Course stuff y'know.
I will check, thanks anyway

Cheers
Scott
 
In order to perform mathematical functions, you need to convert the String object to a primitive, or primitive wrapper object. To do this, you must use a method such as Integer.parseInt() (or similar) - its just impossible to mathematically calculate String objects values.
 
If you are truly restricted, you could do this the tedious way with something like this (if they are just ints):
String s = "121310,4546430,-567898570,2034300";

System.out.println("String:"+s);
System.out.println("Numbers:");

StringTokenizer toke = new StringTokenizer(s,",");

int total = 0;

while(toke.hasMoreTokens()) {

int num = 0;
int sign = 1;
String str = toke.nextToken();
int len = str.length();


for(int i=0;i<len;i++) {

int factor = 1;
for(int x=0;x<(len-i)-1;x++) {
factor = factor*10;
}
System.out.println(&quot;Char:&quot;+str.charAt(i));
switch(str.charAt(i)) {
case '-':
sign = -1;
break;
case '0':
break;
case '1':
System.out.println(&quot;Num[&quot;+num+&quot;]+1*&quot;+factor);

num += 1*factor;
break;
case '2':
num += 2*factor;
break;
case '3':
num += 3*factor;
break;
case '4':
num += 4*factor;
break;
case '5':
num += 5*factor;
break;
case '6':
num += 6*factor;
break;
case '7':
num += 7*factor;
break;
case '8':
num += 8*factor;
break;
case '9':
num += 9*factor;
break;
default:
throw new RuntimeException(&quot;No thank you!&quot;);
}
}
num = num*sign;
System.out.println(&quot;&quot;+num);

total+=num;
}

System.out.println(&quot;Total:&quot;+total);
 
I think that either you use this somewhere or you have too much time on your hands to have worked it out.......

Thank you for spending the time to help me it is much appreciated.

I can do real fancy stuff with GUI's but hey the basics need to be learned.

As an ex-COBOL programmer it would have taken me about ten mins to do, but I spent a whole weekend playing with variuos methods and the factor one I did think of but dismissed as too much work for the benefit derived.

Thanks again to all who replied.



Cheers
Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top