如何从此行中提取数字和字符串并打印字符串数次?

时间:2015-11-07 02:25:00

标签: java string

我必须使用substring,indexOf和parseInt从这个形式的字符串中提取整数和字符串:4是2否3 tada 那么输出应该是这样的: yesyesyesyes 不,不 tadatadatada

我在识别空间字符时遇到问题。 请告诉我如何修复我的代码。

public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int[]count=new int[6];
    System.out.println("Enter a form");
    String form=s.nextLine();
    int spacenbr=0;
    for(int i=0;i<form.length();i++){

        if(form.charAt(i)==' '){ //here is where i think the problem is
            count[spacenbr]=i;
            spacenbr++;      
        }   
    }
    int nbr1=Integer.parseInt(form.substring(0, count[0]+1)); //extracting numbers and strings
    int nbr2=Integer.parseInt(form.substring(count[1], count[2]+1));
    int nbr3=Integer.parseInt(form.substring(count[3], count[4]+1));
    String word1=form.substring(count[0], count[1]+1);
    String word2=form.substring(count[2], count[3]+1);
    String word3=form.substring(count[4]);
    for(int c=0;c<nbr1;c++){
        System.out.print(word1);
    }
    System.out.println();
    for(int d=0;d<nbr2;d++){
        System.out.print(word2);
    }
    System.out.println();
    for(int e=0;e<nbr3;e++){
        System.out.println(word3);
    }

}

`

4 个答案:

答案 0 :(得分:1)

我会做那样的事情:

public static void main(String[] args) {
    final String result = parse("4 yes 2 no 3 tada");
    System.out.println(result);
}

public static String parse(final String source) {
    // split source string to {"4", "yes", "2", "no" ...}
    final String[] tokens = source.split(" "); 
    final StringBuilder builder = new StringBuilder();
    // every even element contains word(like "yes") and every odd element
    // before it contains multiplicator like "4".
    for (int i = 0; i < tokens.length; i++) {
        if ((((i + 1) % 2) == 0)) {
            for(int j = 0; j < Integer.parseInt(tokens[i - 1]); j++) {
                builder.append(tokens[i]);
            }
            builder.append(" ");
        }
    }
    return builder.toString().trim();
}

输出是:“yesyesyesyes nono tadatadatada”

答案 1 :(得分:0)

您可以在原始split()上使用String,然后在生成的数组中运行循环。

for(int i = 0; i < resultingArray.length; i++){
    if(isNumeric(i)){
         int num = Integer.parseInt(i);
         while(num != 0){
             System.out.print(i+1);
             num--;
         }
    System.out.print(" ");
    }
}

您可以使用此功能来帮助您检查正在阅读的String是否可以解析为int

public static boolean isNumeric(String s){
    try{
        Integer i = Integer.parseInt(s);
    }catch (NumberFormatException e){
        return false;
    }
    return true;
}

答案 2 :(得分:0)

正如我所指定的,这是使用split方法的一种方法。

public static void main(String[] args) throws IOException {
      Scanner s=new Scanner(System.in);
        System.out.println("Enter a form");
        String form=s.nextLine();

        if(form!=null ){
            String[] src= form.trim().split(" "); 
            for(int i=0; i<src.length; i=i+2){
                for(int j=0; j<Integer.parseInt(src[i]);j++)
                System.out.print(src[i+1]);
                System.out.print(" ");
            }

        }
        s.close();
}

答案 3 :(得分:0)

import java.util.*;
import java.lang.*;
import java.io.*;
public class test {
public static void main(String[] args) {
    final String result = parse("4 yes 2 no 3 tada");
    System.out.println(result);
}

public static String parse(final String source) {
    // split source string to {"4", "yes", "2", "no" ...}
    final String[] tokens = source.split(" "); 
    final StringBuilder builder = new StringBuilder();
    for (int i = 0; i < tokens.length; i++) {
     if(!isNumeric(tokens[i]))
             {
         for(int j = 0; j < Integer.parseInt(tokens[i-1]); j++) {
                builder.append(tokens[i]);
            }
            builder.append(" ");

             }

    }
    return builder.toString().trim();
}

public static boolean isNumeric(String s){
    try{
        Integer i = Integer.parseInt(s);
    }catch (NumberFormatException e){
        return false;
    }
    return true;
}

}

输出: yesyesyesyes nono tadatadatada