Results 1 to 2 of 2
Thread: How delete "+" from String?
- 10-20-2011, 03:06 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
How delete "+" from String?
Output in console: summ = 5.1+3.14+0+=8.24
How delete last "+" from String? Wich better way? I know one, but musst use if. In programm to much if. And how can without use if?
Code Example:
Java Code:import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Runner { public static void main(String[] args) { String path = "in.csv"; String line = ""; int n = 0; //lenght of array StringBuilder stringForOutput = new StringBuilder(); int error = 0; BufferedReader br = null; try { n = checkArrayLenght(path); br = new BufferedReader(new FileReader(path)); String[][] data = new String[n][]; int i = 0; while((line = br.readLine()) != null) { String[] theLine = line.split(";"); data[i] = theLine; i++; } double summ = 0; stringForOutput.append("Сумма = "); for(int x = 0; x < data.length; x++) { if(checkString(data[x][0])) { int index = Integer.parseInt(data[x][0]); if(index < ((data.length)) && index >= 0) { summ = summ + Double.parseDouble(data[x][index]); stringForOutput.append(data[x][index]); stringForOutput.append("+"); } else { ++error; } } else { ++error; } } stringForOutput.append("=" +summ); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { System.out.println(e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(e); } finally { try { br.close(); } catch(IOException ex) { ex.printStackTrace(); } System.out.println(stringForOutput); System.out.println("errors -lines = " + error); } } //check if number public static boolean checkString(String string) { try { Integer.parseInt(string); } catch (Exception e) { return false; } return true; } //check lenght of array public static int checkArrayLenght(String path) throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader(path)); String line; int lenght = 0; while((line = br.readLine()) != null) { lenght++; } return lenght; } }Last edited by Dimitri; 10-20-2011 at 03:09 PM.
- 10-20-2011, 03:26 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Re: How delete "+" from String?
A simple way is to use regular expressions
Java Code:String sample1 = "Sample String + in middle"; String sample2 = "Sample String + at end +"; String s1 = sample1.replaceAll("\\+$", ""); String s2 = sample2.replaceAll("\\+$", ""); System.out.println(s1); System.out.println(s2)
The replaceAll method looks for string 1 and replaces it with string 2 (nothing in this case)
The +$ means look for a + at the end ($) of the line. The \\ escapes the +, ie it looks for a literal + sign.
Search for regexes for more information
Similar Threads
-
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
string comparison with "=" and ".equal"
By guavajuice in forum New To JavaReplies: 9Last Post: 04-22-2010, 09:01 PM -
final String currentWorld = "Java Forums"; String.format("Hello %s", currentWorld);
By mcfrog in forum IntroductionsReplies: 0Last Post: 04-02-2009, 07:02 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
How do we "Loop" through subfolders to delete files?
By Zrob in forum New To JavaReplies: 3Last Post: 09-26-2008, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks