skip to main content

kiesler.at

JAVAs StringTokenizer
updated by rck, 2006-12-31

Oft genug hat man einen String, in dem nach gewissen Zeichen getrennte Dinge drinnen stehen. Sei es ein Datum, durch . getrennt. Oder auch ein zu berechnender Term mit Operationen der Art +, -, *, etc. Der JAVA StringTokenizer zerlegt solche Zeichenketten mühelos und spart viel Arbeit.

1 | 2 | 3 | 4 | 5 | 6 | 7

EinfacherRechner.java

1 /*      EinfacherRechner.java
2  *
3  *      ein einfacher String-Berechner, der den Umgang mit
4  *      dem JAVA StringTokenizer zeigt. Beherrscht die
5  *      Addition und Subtraktion.
6  *
7  *      Details: http://www.kiesler.at/
8  *
9  */
10 
11 
12 import java.io.*;
13 import java.util.*;
14 
15 
16 public class EinfacherRechner {
17 
18 
19         public static int berechne(String s)
20                 throws Exception
21         {
22                 int result=0, zahl=0;
23                 String lastop="";
24                 boolean op=true;
25 
26                 StringTokenizer st=new
27                         StringTokenizer(s, "+-", true);
28 
29                 if((st.countTokens() % 2) != 1)
30                         throw new Exception("Anzahl der "+
31                                 "Tokens muß ungerade sein!");
32 
33                 while(st.hasMoreTokens()) {
34 
35                         if(op) {
36                                 zahl=Integer.parseInt(
37                                         st.nextToken());
38 
39                                 if(lastop.equals(""))
40                                         result=zahl;
41                                 else
42                                 if(lastop.equals("+"))
43                                         result+=zahl;
44                                 else
45                                 if(lastop.equals("-"))
46                                         result*=zahl;
47                                 else
48                                 throw new Exception
49                                         ("Unbekannte Operation '"+
50                                          lastop+"'");
51 
52                         } else
53 
54                                 lastop=st.nextToken();
55 
56                         op=!op;
57                 }
58 
59                 return(result);
60         }
61 
62 
63         public static void main(String args[])
64                 throws Exception {
65 
66                 InputStreamReader ins=new InputStreamReader(System.in);
67                 BufferedReader reader=new BufferedReader(ins);
68 
69                 try {
70 
71                         String eingabe=reader.readLine();
72                         System.out.println(berechne(eingabe));
73 
74                 } catch(Exception e) {
75 
76                         System.err.println("Problem: "+e);
77 
78                 }
79 
80         }
81 }

1 | 2 | 3 | 4 | 5 | 6 | 7



RSSComments - Make a comment
The comments are owned by the poster. We are not responsible for its content.
RSSAll Articles
2008, 2007, 2006, 2005, 2004

What's Related

Documents

Übersetzerbau

Link Manager

Übersetzerbau
EPROG
JAVA

Article Manager

EPROG
Übersetzerbau
JAVA

FAQ

Übersetzerbau

RSS News Feeds

JAVA

Latest Updates

AdministrativeTexts
updated by freddiemac1993, 2013-06-14
wiki

Re: adventures
created by brittdavis10, 2012-02-23 (1 rply, 3 views)
thread

Re: how to run phpwebsite...
created by alexander, 2011-08-25 (2 rpls, 3607 views)
thread

Re: Forum tags
created by HaroldFaragher, 2011-08-22 (3 rpls, 8488 views)
thread


Zu den KO2100 Foren