如何打印出字符串的特定部分

时间:2014-02-10 02:06:29

标签: java

这是我的代码,我需要帮助的是我不知道如何只打印字符串的某些部分

那么这段代码应该做的是例如输入是&&&&&&&然后输出应该是*** 456,这意味着&&&&&& =打印6 *和多少数字有意义长度,所以* - > *** 456如果输入包含“,”,例如输入是&&&,&&&&&&&&&&&&&&&&&&用“。”将小数点放在放置位置并向上舍入到该点并且“$”只是在数字前放置一个美元符号

所以我的问题是“,”部分,例如我输入1000000000和&&&&&&&&&&&&&&那么打算输出** 1,000,000,000我该怎么做?

所以我要如何创建我的代码是因为我打印出字符串的最后3个所以它将是000然后用* for (int...语句放一个*我需要帮助如何使我的代码只读取字符串的最后3个字母(这是第二个IF语句(else if (X.contains(",") && !X.contains("."))这一个)

但我不认为for (int...会对此有任何建议我会很感激谢谢!

import java.util.Scanner;
public class printFormatting
{

// all the "//"s are to see if the each step works

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);

    System.out.println("Enter the &'s and ,'s");
    String X = in.next();
    //System.out.println(X);

    System.out.println("Enter the numbers");
    String Y = in.next();
    //System.out.println(Y);

    int lengthX = X.length();
    //System.out.println("Print out length of X");
    //System.out.println(lengthX);

    int lengthY = Y.length();
    //System.out.println("Print out length of Y");
    //System.out.println(lengthY);

    if (X.contains(",") && X.contains("."))
    {
        lengthX -= 2;
        for (int z = lengthX-lengthY; z > 0;)
        {
            System.out.print("*");
            z--;
        }


        //System.out.println("Works , && .");

    }
    else if (X.contains(",") && !X.contains("."))
    {
        lengthX -= 1;
        for (int z = lengthX-lengthY; z > 0;)
        {
            System.out.print("*");
            z--;
        }



        System.out.println(X.length());

        //System.out.println("Works ,");
        //This is to see if , can be seen
    }
    else if (X.contains(".") && !X.contains(","))
    {
        lengthX -= 1;
        for (int z = lengthX-lengthY; z > 0;)
        {
            System.out.print("*");
            z--;
        }

        //System.out.println("Works ,");
        //same
    }
    if (X.contains("$"))
    {
        lengthX -= 1;
        for (int z = lengthX-lengthY; z > 0;)
        {
            System.out.print("*");
            z--;
        }
        System.out.print("$");

        //System.out.println("Works &");
    }

    System.out.println(Y);


}

}

1 个答案:

答案 0 :(得分:1)

我对,的含义以及放置位置有点模糊。但我知道你想用*替换数字(从结尾开始),以便:

******   and  123  become ***123

<强> [编辑]

然后我只是猜测,每当字符串x包含任何,.时,您希望拥有标准的小数点分隔符(a或者。除以前的3位数字)数字),所以

   1000 becomes     1,000  
  10000 becomes    10,000
1000000 becomes 1,000,000

等等。我可能误会了,所以如果我错了,请纠正我。但是我会这样做:

    Scanner in = new Scanner(System.in);  

    System.out.println("Enter the &'s and ,'s");
    String x = in.next();

    System.out.println("Enter the numbers");
    String y = in.next();

    // here begins the new part

    String output = x.replace("&", "*");  // first replace all & with *
    String separator = "";                // check what kind of separator to use (, or .)
    if (x.contains(",")) {
        separator = ",";
        output = output.replace(",", ""); // remove all the , (we won't need them anymore)
    } else if (x.contains(".")) {
        separator = "."; 
        output = output.replace(".", ""); // remove the .
    }

    // now get the substring with the * that you keep and remove as many * as you need for the number
    output = output.substring(0, output.length() - y.length());  
    // just append the number to your output String
    output += y;

    System.out.println("output without the "+separator+"'s :\n"+ output);  // this is just to check if all *s where replaced correctly

    int remainingLength = output.length();    // we start at full length
    while (remainingLength / 3 > 0) {         // at every 3rd digit we want a separator (but not if the number has less only 3 or less digits) 
        // first our failsave: if there is a * in the substring we want to separate by a , or . then we are finished– break the loop and stop 
        if(output.substring(remainingLength - 3).contains("*")){
            break;
        }

        // otherwise form a new string of the substring before the separator, append the separator and the just add the other substring from after the seperator        
        output = output.substring(0, remainingLength - 3) + separator + output.substring(remainingLength - 3);
        remainingLength-=3;  // we just put a , after the 3 next digits          
    }
    System.out.println("output with the "+separator+"'s :\n"+ output);

<强> TEST

Enter the &'s and ,'s
&&&&&,&&&&&
Enter the numbers
10000000
output without the ,'s :
**10000000
output with the ,'s :
**10,000,000