简体   繁体   中英

Java Formatting String to Remove “0” in front of the variable

Basically I am having some problem when trying to format the string I retrieved from database. Here is my nested for loop:

for (int count = 0; count < monthStr.length; count++) {
        for (int i = 0; i < trans_list.size(); i++) {
            if (count == Integer.parseInt(trans_list.get(i).getDate())) {
                expensesSeries.add(x[count], trans_list.get(i)
                        .getAmount());
            } 
            else{
                expensesSeries.add(x[count], 0);
            }
        }
    }

Basically the getDate() will return string in these format: 01,02,03,04 and so on until 12. What I am trying to do is comparing the count which is 1-12, with the string I retrieved. Is there any way to remove the 0 in front of 01,02 from the string?

Thanks in advance.

EDIT

    boolean setZero = true;
    String[] monthStr = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
            "Aug", "Sep", "Oct", "Nov", "Dec" };

    // Creating an XYSeries for Expenses
    XYSeries expensesSeries = new XYSeries("Expenses");
    for (int count = 1; count < 13; count++) {
        for (int i = 0; i < trans_list.size(); i++) {
            if (count == Integer.parseInt(trans_list.get(i).getDate())) {
                setZero = false;
                expensesSeries.add(count, trans_list.get(i)
                        .getAmount());
            } 
            if(setZero)
            {
                expensesSeries.add(count, 0);
                setZero = true;
            }       
        }
    }

    for (int j = 0; j < monthStr.length; j++) {
        multiRenderer.addXTextLabel(j+1, monthStr[j]);
    }
}

在此处输入图片说明

Java Formatting String to Remove “0” in front of the variable

ParseInt should be able to get an int from a String starting with 0 (as long as it precedes an int value) but if you need it for some other purpose not shown in this code just do a regular expression replaceAll() with the start character ^

string.replaceAll("^0","");

and for multiple 0s, use * as well

string.replaceAll("^0*","");

Specific Problem mentioned here

Why the nested loop? It looks like you want the contents of trans_list to be added as points via expensesSeries , with zero in places where there is no data. Just keep track of totals with an array:-

 int[] monthlyTotals = new int[12];
 for (int i=0; i<trans_list.size(); i++)
 {
   //Add item at index i trans_list at location i in chart
   int chartXIndex =  Integer.parseInt(trans_list.get(i).getdate());
   monthlyTotals[chartXIndex] += trans_list.get(i).getAmount();
 }

You now have an array that represents what you want on the chart so just loop to add them to the chart

for (int i=0; i<monthlyTotals.length; i++)
{
   expensesSeries.add(i, monthlyTotals[i]);
   multiRenderer.addXTextLabel(i+1, monthStr[i]); //combine your last loop as well
}

Nested looping can be confusing, avoid if possible.

Integer.parseInt(...) should do the trick. It looks like that it is already in your code, so I believe it works and maybe something else is causing the problem. What is your problem here? Do you have an exception? Strange results?

I suggest you should change line

expensesSeries.add(x[count], trans_list.get(i).getAmount());

to

expensesSeries.add(Integer.parseInt(trans_list.get(i).getDate()) - 1, trans_list.get(i).getAmount());

(or better assign Integer.parseInt(trans_list.get(i).getDate()) to variable and reuse it in if(...) and add(...)

Not sure if it will help, but x seems pointless and may cause some confusion.

UPDATE

I think, that introducing boolean variable is not necessary. Right now your code won't work properly. Imagine situation, where you do not have data with, lets say April (4). Then first if won't be processed, but second one also, so you won't have data (even with value 0) added to expendesSeries, and it will cause problem you are facing (no values for some of the months). Here I think is what you should do (edited your current code):

    String[] monthStr = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
            "Aug", "Sep", "Oct", "Nov", "Dec" };

    // Creating an XYSeries for Expenses
    XYSeries expensesSeries = new XYSeries("Expenses");
    for (int count = 1; count < 13; count++) {
        for (int i = 0; i < trans_list.size(); i++) {
            if (count == Integer.parseInt(trans_list.get(i).getDate())) {
                expensesSeries.add(count, trans_list.get(i)
                        .getAmount());
            } else {
                expensesSeries.add(count, 0);
            }       
        }
    }

    for (int j = 0; j < monthStr.length; j++) {
        multiRenderer.addXTextLabel(j+1, monthStr[j]);
    }

Now it will fetch all months, then for every month it will fetch all transactions, and add 0 or some specific value to expensesSeries for each month. If that won't work I suggest using @RossDrew solution (it is simplified, more optimal and straightforward).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM