How to group all similar Strings seperately in an ArrayList in Android

I have an ArrayList of Strings which would be having a size 1,000 to 30,000 elements.

    ArrayList<String> name= new ArrayList<String>();

    ArrayList<ArrayList<String>> similar_group =new ArrayList<ArrayList<String>>();

    for(int i=0;i<munber;i++)
    {
    name.Add("name");
    }

I want to group the similar Strings and put them to an ArrayList.

For eg there are 10 elements in the name ArrayList with value “a” then all the elements whose value is equal to “a” should be added to an ArrayList and then that ArrayList should be added to similar_group ArrayList.

If there is any element in name ArrayList whose value is unique then even that should be added to a ArrayList and then that ArrayList should be added to similar_group ArrayList.

The size of the ArrayList may be big. So is there any way to the do this?

How can we concat a String at the end of each line in java?

I have some unique ID which have to be appened to the end of each line. I don’t want to create another file, I want to do this in the same file which is used to read line by line and based on that line unique ID is generated.
I using java for this operation and here is my code,

String filename="location.txt";
    Path pathToFile = Paths.get(filename);
    FileWriter fstream = null;
    BufferedWriter bw=null;
    PrintWriter pw=null;
    try {
        fstream = new FileWriter(filename, true);
        bw=new BufferedWriter(fstream);
        pw=new PrintWriter(bw);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) { 
        // read the first line from the text file 
        String line = br.readLine(); 
        // loop until all lines are read 
        while (line!=null) { 
            // use string.split to load a string array with the values from 
            // each line of 
            // the file, using a " " as the delimiter
            String[] attributes = line.split(" "); 

            System.out.print("Sensor["+attributes[0]+"] : ");
            double x=Double.parseDouble(attributes[1]);
            double y=Double.parseDouble(attributes[2]);
            GeoHash geoCode=GeoHash.withCharacterPrecision(x, y, 10);
            System.out.print(geoCode+"n");
            pw.println(line+" "+geoCode);

            line = br.readLine(); 
        } 
        br.close();
        pw.close();
        bw.close();
        fstream.close();
    } catch (IOException ioe) { 
            ioe.printStackTrace(); 
    }
    System.out.println("Generated!!");
} 

Currently by running this code i will get the appended line as new line at the end of my file as below:
OUTPUT:

1 21.5 23
2 24.5 20
3 19.5 19
4 22.5 15
5 24.5 121 21.5 23 seb673undf
2 24.5 20 skq5rh5dcg
3 19.5 19 s7mwbmg7sp
4 22.5 15 sk48j248j2
5 24.5 12 sk2e3h44u7

Is there any way to solve this? or any other simple method to do it?

NoSuchFileException uploading image into spring boot

i am wokring on spring boot, and i have created a folder web and images floders on this path : myApp/src/web/images

And when doing

private String saveFile(MultipartFile file, String fileName) throws IOException {
    byte[] bytes = file.getBytes();
    String imagePath = new String(this.servletContext.getRealPath(this.imagesPath) + "/" + fileName);
    Path path = Paths.get(imagePath);
    Files.write(path, bytes);
    return imagePath;
}

i get this error :

java.nio.file.NoSuchFileException: /private/var/folders/4g/wd_lgz8970sfh64zm38lwfhw0000gn/T/tomcat-docbase.8255351399752894174.8098/images/IMG_2018-01-06 15:18:48.486.jpg

where i should put the images folder in ordre to upload files into it successfuly.

Thanks for the help

Java arrays printing out weird numbers, and text [duplicate]

This question already has an answer here:

I’m new to programming. I’m sure the answer for this question is out there, but I have no idea what to search for.

so um. ok, i’ll go right to it.

Here’s my code:

    int[] arr;
    arr = new int[5];

    arr[0] = 20;
    arr[1] = 50;
    arr[2] = 40;
    arr[3] = 60;
    arr[4] = 100;

    System.out.println(arr);

    }
}

Compiles & works fine. It’s just the output from CMD that i’m dizzy about.

This is the output: [I@3e25a5

I want them to represent the exact same numbers from the list (arr) instead. How do I make that happen?

Recap rows and columns recursively in java

I’m having a hard time to find out what is wrong with my code…
The program is suppose to summing up the rows and columns of a MxM matrix and return true if the sum of every row is equal to every column, and false otherwise.
for example-
{
{3,2,1},
{3,1,2},
{0,3,3}
}
will return true.

this is my code so far:

public static boolean rowsColsEqual(int[][] a, int i, int j)
{
    if(i<a[0].length-2)
    {
        if(sumCol(a,i,j)==sumCol(a,i+1,j)) {
            return rowsColsEqual(a, i + 1, j);
        }
        else
            return false;
    }

    if(j<a.length-2)
    {
        if(sumRow(a,i,j)==sumRow(a,i,j+1)) {
            return rowsColsEqual(a, i , j+1);
        }
        else
            return false;
    }
    return true;
}

public static int sumCol(int[][] a , int i, int j)
{
    if(j<a.length-1)
        return a[i][j]+sumCol(a, i, j+1);
    return a[i][a.length-1];
}

public static int sumRow(int[][] a , int i, int j)
{
    if(i<a[0].length-1)
        return a[i][j]+sumRow(a, i+1, j);
    return a[a[0].length-1][j];
}

i did some testing and the 2 summing methods are working.
it’s just the main method that doesn’t work for some reason.
any suggestion?
tnx.

Java list ALL files in jar file / How is getResource() working

So my question is: how can i list every file/folder that is in my jar file. Normally i use Files.walk(myPath) for this and then i can “walk” through all files e.g. in the folder “/res”.

But when i want to walk through the complete jar file i get nothing. For the Path i use “” (<- empty String) because its the “root path”.
Even when i check if this path exists, it says yes when i execute the code in my IDE, but when i export it to a jar it can’t find the empty path. What am i doing wrong here?

I checked some more Paths e.g. “/”, “..”, “/..”, “../” and i only got null as a resource…

(Checking for existance is done via getResource(myPath) and then just null-checking, i’ve tried the ClassLoader.getSystemClassLoader() and the ClassLoader from the Class i’m working in (Test.class.getResource(myPath) and Test.class.getClassLoader().getResource(myPath)), nothing works…)

Websphere session timeout

My Questions :

1) Does timeout set in Websphere admin console means that after inactivity of exactly that much period, the session will be destroyed or there is no guarantee that session will be destroyed in exactly that much time?

For example: say I have set a timeout of 5 mins in admin console of WebSphere.Can I expect the session to be destroyed exactly after 5 mins or time varies?If it varies then what is the criteria of killing session.

2) what is the difference between setting timeout in Websphere versus setting in web.xml

3) If I have implemented HttpSessionListener.When flow reaches sessionDestroyed() method, is session still exists or already destroyed.

Thanks.

How to identify Bigcommerce app user

when I install an app on BigCommerce store,then
my app displays in iframe of bigcommerce app.But when user click on any hyperlink of my app dashboard then how to recognize in server side which user has clicked the hyperlink ,and then which user related data should i Process.

Thanks.

java: how to avoid upcasting( or how to correctly downcast)

I have this class

class Test4{
  public static void main(String[] args){
    int i;
    Disegnatore disegnatore;
    Gomma gomma;
    disegnatore = new DisegnatoreEsperto();
    gomma = new Gomma();
    disegnatore.correttore = gomma;
    for(i=0; i<11; i++) disegnatore.usaCorrettore(10);
  }
}

where Disegnatore is this:

public class Disegnatore{
  public StrumentoDaDisegno strumento;
  int i;

public Disegnatore(){
  strumento = null;
}

public void usaStrumentoDaDisegno(int n){
 ...
}
}

and DisegnatoreEsperto is this

public class DisegnatoreEsperto extends Disegnatore{
  Correttore correttore;
  int i;

  DisegnatoreEsperto(){
    correttore = null;
  }

  public void usaCorrettore(int n){
    if (correttore != null) for (i=0; i<n; i++) correttore.cancella();
    else System.out.println("Non mi hai dato nessuno correttore!");
  }
}

Problem is, when I try to use the class DisegnatoreEsperto features in object disegnatore in class Test4 looks like the assignment disegnatore = new DisegnatoreEsperto()upcasted the newly created object to Disegnatore type, making it impossible to use the subclass features. I solved the problem by re-defining the disegnatore reference in class Test4 as a DisegnatoreEsperto. Was there a way to solve this without doing that, i.e. to prevent the assignment to do the upcasting?

Print all prime numbers between two given numbers in Java only

everyone, I tried this program but not get the expected answer, Please help me out.

Question is:

Print all prime numbers between two given numbers. For example: Function is Prime(beg,end), for Prime(4,7) it will return {5,7}, for Prime(7,16) it will return {7,11,13}.
My code :

package com.robin.practise;

import java.util.Scanner;

public class PrimebetweenRange 
{

    private static String Prime(int beg, int end)
    {
        String res= " ";
        for(int i= beg; i<= end; i++)
        {
            for(int j=2; j<= end; j++)
            {
                if(i%j!= 0)
                {
                    //System.out.println(i+  " ");
                    res= i+ " ";
                }
            }
        }
        return res;
    }

    public static void main(String[] args) 
    {
        Scanner scn= new Scanner(System.in);
        System.out.println("Enter any two numbers: ");
        int n1= scn.nextInt();
        int n2= scn.nextInt();
        scn.close();
        System.out.println(Prime(n1, n2));
    }
}