当前位置:编程学习 > JAVA >>

java 根据Sting数组元素中前2个字符,来求和后面的数字,然后返回String类型。

如:String[] str 有以下元素str[ 0 ]=”调入100“ ,
                           str[ 1 ]= “甲控200” ,
                           str[2 ]=“ 调入100”, 
                           str[ 3 ]=“调入10”
,共4个元素:
我现在想要得到以下结果:
返回结果:String  str1="调入210,甲控200"
请问这个方法要怎么写?请各位大侠帮忙 --------------------编程问答-------------------- public class StringProc {

/**
 * @param args
 */
public static void main(String[] args) {

String[] str = new String[4];

HashMap<String, Integer> d = new HashMap<String, Integer>();

HashMap<String, Integer> j = new HashMap<String, Integer>();

str[0] = "调入100";
str[1] = "甲控200";
str[2] = "调入100";
str[3] = "调入10";
int now;
int original;
d.put("调入", 0);
d.put("甲控", 0);
for (int i = 0; i < str.length; i++) {
if (str[i].contains("调入")) {
now = Integer.valueOf(str[i].replaceAll("调入", ""));
original = d.get("调入");
now = original + now;
d.put("调入", now);
}
if (str[i].contains("甲控")) {
now = Integer.valueOf(str[i].replaceAll("甲控", ""));
original = d.get("甲控");
now = original + now;
d.put("甲控", now);
}
}
String result = "调入" + d.get("调入")+"," + "甲控" + d.get("甲控");
System.out.println(result);
}


} --------------------编程问答-------------------- --------------------编程问答-------------------- 用map做
将数组的前两个字符作为key,后面的值作为value,
如果map中不包含key,则将当前的key,value加入map
否则更新当前key所在的value。
最后输出map即可。 --------------------编程问答--------------------


package a;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Test4 {

/**
 * @param args
 * @author sunstar
 * @date 2012-7-18 下午5:29:53
 */
public static void main(String[] args) {
String[] str = new String[4];
str[0] = "调入100";
str[1] = "甲控200";
str[2] = "调入100";
str[3] = "调入10";
Map<String, Integer> map = new HashMap<String, Integer>() ;

String tmp = "" ;
int num = 0 ;
for (int i = 0; i < str.length; i++){
tmp = str[i] ;
num = Integer.valueOf(tmp.substring(2)).intValue();
tmp = tmp.substring(0,2) ;
if (map.get(tmp) != null){
num = map.get(tmp)+ num ;
}
map.remove(tmp) ;
map.put(tmp, num) ;


}

Iterator it = map.keySet().iterator() ;
StringBuffer sb = new StringBuffer() ;
while(it.hasNext()){
tmp = (String) it.next() ;
sb.append(tmp  + map.get(tmp)) ;
sb.append(",") ;
System.out.println(tmp + " --" + map.get(tmp)) ;
}

System.out.println(sb.toString().substring(0,sb.toString().length()-1)) ;

}

}



甲控 --200
调入 --210
甲控200,调入210
--------------------编程问答--------------------

package a;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Test4 {

/**
 * @param args
 * @author sunstar
 * @date 2012-7-18 下午5:29:53
 */
public static void main(String[] args) {
String[] str = new String[6];
str[0] = "调入100";
str[1] = "甲控200";
str[2] = "调入100";
str[3] = "调入10";
str[4] = "XX500";
str[5] = "YY500";
Map<String, Integer> map = new HashMap<String, Integer>() ;

String tmp = "" ;
int num = 0 ;
for (int i = 0; i < str.length; i++){
tmp = str[i] ;
num = Integer.valueOf(tmp.substring(2)).intValue();
tmp = tmp.substring(0,2) ;
if (map.get(tmp) != null){
num = map.get(tmp)+ num ;
}
map.remove(tmp) ;
map.put(tmp, num) ;


}

Iterator it = map.keySet().iterator() ;
StringBuffer sb = new StringBuffer() ;
while(it.hasNext()){
tmp = (String) it.next() ;
sb.append(tmp  + map.get(tmp)) ;
sb.append(",") ;
System.out.println(tmp + " --" + map.get(tmp)) ;
}

System.out.println(sb.toString().substring(0,sb.toString().length()-1)) ;

}

}



甲控 --200
调入 --210
YY --500
XX --500
甲控200,调入210,YY500,XX500 --------------------编程问答--------------------

public class test {
private static String a;
private static String c="";
private static String d="";
private static int b;
private static int e;

public static void main(String[] args) {
String[] str=new String[4];
String[] mstr = new String[4];
str[ 0 ]="调入100";
str[ 1 ]= "甲控200" ;
str[2 ]="调入100";
str[ 3 ]="调入10";

    for (int i = 0; i < str.length; i++) {;
        mstr[i]= str[i].substring(0, 2)+","+str[i].substring(2);
}

    for (int i = 0; i < mstr.length; i++) {
      a = mstr[i].split(",")[1];
if(mstr[i].split(",")[0].equals("调入") ) {
     b+=Integer.parseInt(a);
     c+="调入"+b;
     c.split("调入", 2);
}else {
e+=Integer.parseInt(a);
d+="甲控"+e;
}

}
    if (c.contains("调入")) {
System.out.println(c.substring(c.lastIndexOf("调入")));
}
    if(d.contains("甲控"))
    {
System.out.println(d.substring(d.lastIndexOf("甲控")));
}

}
}
调入210
甲控200 --------------------编程问答-------------------- public static void main(String[] args) throws ParseException{
String str[]={"调入100","甲控200","调入100","调入10"};
int num0=0;
int num1=0;
for(String str1:str){
if(str1.substring(0,2).trim().equals("调入")){
num0+=Integer.valueOf(str1.substring(2)).intValue();
}else if(str1.substring(0,2).trim().equals("甲控")){
num1+=Integer.valueOf(str1.substring(2)).intValue();
}
}
System.out.println("调入"+num0);
System.out.println("甲控"+num1);
} --------------------编程问答-------------------- public static void main(String[] args) throws ParseException{
String str[]={"调入100","甲控200","调入100","调入10"};
int num0=0;
int num1=0;
for(String str1:str){
if(str1.substring(0,2).trim().equals("调入")){
num0+=Integer.valueOf(str1.substring(2)).intValue();
}else if(str1.substring(0,2).trim().equals("甲控")){
num1+=Integer.valueOf(str1.substring(2)).intValue();
}
}
System.out.println("调入"+num0);
System.out.println("甲控"+num1);
} --------------------编程问答-------------------- import java.util.HashMap;
public class StringProc {

/**
 * @param args
 */
public static void main(String[] args) {


String[] str = new String[100];

String[] out = new String[100];

HashMap<String, Integer> d = new HashMap<String, Integer>();

HashMap<String, Integer> j = new HashMap<String, Integer>();

str[0] = "调入100";
str[1] = "甲控200";
str[2] = "调入100";
str[3] = "调入10";



int now;
int original;
for (int i = 0; i < str.length; i++) {
if (str[i] != null){
if (d.get(str[i].substring(0, 2)) == null) {
d.put(str[i].substring(0, 2),
Integer.valueOf(str[i].substring(2)));
out[i] = str[i].substring(0, 2);
} else {
now = Integer.valueOf(str[i].replaceAll(
str[i].substring(0, 2), ""));
original = d.get(str[i].substring(0, 2));
now = original + now;
d.put(str[i].substring(0, 2), now);
}
}

}
String result = null;

for(String key : d.keySet()){

System.out.print(key + d.get(key) + " ");
}

}


}
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,