0

I'm trying to figure out how to write the data like this : {"Temperature" = 19.7, "Humidity" = 11, "day"=1, "month" = 3, "year"=1995}

Where i'm struggling is how split the data and use it, should i make a new arrayList, or i can split it inside the same array?

data from txt file:

19.7|11|1/1/1995

12.0|12|2/1/1995

14.6|11|3/1/1995

10.1|10|4/1/1995

My program:

 ArrayList<String> weather = new ArrayList<String>();
    while (scanner.hasNextLine()){
        String line = scanner.nextLine();
        String [] splittedString = line.split("|");
        System.out.print(" \"Tempreture\" = "+splittedString[0]);
        System.out.print(", \"Humidity\" = "+splittedString[1]);


     String [] splitDate = line.split("/");

        System.out.print(" \"day\"= "+splitDate[2]);
        System.out.print(" \"month\"= "+splitDate[3]);
        System.out.print(" \"year\"= "+splitDate[4]);
        weather.add(splittedString[3]);
        for (int i=4;i<splittedString.length;i++){
            System.out.print(splittedString[i]+", ");
SamiaYM
  • 27
  • 1
  • 8
  • I have a solution to this, but it seems to be marked as a duplicate. Please check other solutions before asking a question. – drew Jan 31 '16 at 18:52
  • I looked into the other question and answeres, my question has slightly a diffrenece which is dates, how can i split them? – SamiaYM Jan 31 '16 at 19:42
  • Well it won't let me post a solution now since it is marked.. – drew Jan 31 '16 at 19:45
  • public static void main(String[] args){ String[] weather = {"19.7|11|1/1/1995", "12.0|12|2/1/1995"}; splitData(weather); } public static void splitData(String[] weather){ for(int i = 0; i < weather.length; i++){ String line = weather[i]; String[] split = line.split("\\|"); System.out.println("Temperature = " + split[0]); System.out.println("Humidity " + split[1]); //Get Date String[] date = split[2].split("\\/"); System.out.println("Day = " + date[0]); System.out.println("Month = " + date[1]); System.out.println("Year = " + date[2]); } } – drew Jan 31 '16 at 19:47
  • I don't see how it is different than duplicate. Split on `|` (see duplicate to learn how to do it properly) and you will get each parts, then take last part and split it on `/`. – Pshemo Jan 31 '16 at 19:49
  • @drew you don't need to escape `/` in `split`. `/` may have special meaning in JavaScript but in Java it doesn't. ``\`` has. – Pshemo Jan 31 '16 at 19:57

0 Answers0