Java CSV to Array and Array to CSV

This is a little example in Java to convert a CSV String into an Multidimensional Array, after that i convert the Array back into a CSV String, you could choose the delimiter by yourself. In this case i take the “-” to see the different:

public class HelloWorld{

     public static void main(String []args){
        String str="1;2;3\n4;5;6\n7;8;9\n10;11;12";
        System.out.println(str);
        String[][] out = csv2array(str);
        System.out.print(array2csv(out,"-"));
        
     }
     
    public static int count(String s, char c) {
        return s.length()==0 ? 0 : (s.charAt(0)==c ? 1 : 0) + count(s.substring(1),c);
    }
    
    public static String[][] csv2array(String str){
        String[] parts   = str.split("\n");
        String[][] out = new String[count(str,'\n')+1][count(parts[0],';')+1];
        for(int i = 0;i< parts.length;++i){
            if(!parts[i].equals("")){
                out[i] = parts[i].split(";");
            }
        }
        return(out);
     }
     
     public static String array2csv(String[][] in,String delim){
         String out = "";
        for(int x = 0;x < in.length;x++){
        for(int y = 0;y < in[x].length;y++){
            out += (in[x][y]);
            if(y!=in[x].length-1){
                out += delim;
            }
        }
        out += ("\n");
        }
        return(out);
     }
     
}

This is the Output, fist block separated with the “;” is the input and with “-” is the output:

1;2;3
4;5;6 
7;8;9
10;11;12
1-2-3
4-5-6
7-8-9
10-11-12

Java try/catch

This is a little example about try and catch in Java:

public class HelloWorld{
     public static void main(String []args){
        System.out.print("write your number: ");//Info text 
        String str = System.console().readLine();//read input
        int res = 0;
        try{
           res = Integer.parseInt(str); 
           res *= 2;
        }catch (Exception e){
            System.out.println("Error:"+e);
        }
        System.out.println("result:"+res);
     }
}

Output looks like:

sh-4.3# java -Xmx128M -Xms16M test                     
write your number: 2                                         
result:4  

                                                
sh-4.3# java -Xmx128M -Xms16M test                     
write your number: a                                         
Error:java.lang.NumberFormatException: For input string: "a" 
result:0

Java calculate the sum of the given numbers

This program calculates the sum from 10 numbers, you could hit “stop” to break and calculate less than 10 numbers:

public class HelloWorld{
     public static void main(String []args){
        int[] values = new int[10];
        int i = 0;
        int sum = 0;
        System.out.print("write up to 10 numbers !\n");//Info text
        for(i = 0; i <= values.length-1;++i){
           System.out.print("write your number: ");//Info text
           String str = System.console().readLine();
           if(!str.equals("stop")){
           values[i] = Integer.parseInt(str);//read input
           }else{--i;break;}
        }
        System.out.print("your input:\n");//Info text
        for(int x = 0; x <= i;++x){
           System.out.print("your number: "+values[x]+"\n");//Info text
           sum += values[x];
        }
        System.out.println("your result: "+sum);//print result
     }
}

Output looks like this:

write up to 10 numbers !
write your number: 1 
write your number: 2 
write your number: 3 
write your number: stop 
your input:        
your number: 1 
your number: 2 
your number: 3 
your result: 6

Java input output example

This is a little example in Java how to print an info text and then read a number from the terminal, calculate the square and then print the result:

public class HelloWorld{
     public static void main(String []args){
        System.out.print("write your number: ");//Info text 
        String str = System.console().readLine();//read input
        System.out.println("your number: "+str);//print input
        int i = Integer.parseInt(str);//convert to intager
        i = i*i;//calculate
        System.out.println("your result: "+i);//print result
     }
}

To compile your test.java file run this in your terminal:

javac test.java

now we have the file test, to execute run this:

java -Xmx128M -Xms16M test

This is the output:

write your number: 4                                                       
your number: 4                                                               
your result: 16

The same Program in C# is here : C# input output example