Results 1 to 2 of 2
  1. #1
    Dimitri is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    ballantony is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default 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

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. string comparison with "=" and ".equal"
    By guavajuice in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 09:01 PM
  3. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 3
    Last Post: 09-26-2008, 06:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •