Click here to Skip to main content
16,019,983 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am trying to convert this code to c#

Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class TestModulo {	
	static HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
	static int modulo = 10;
	static String fileListEquipment = "C:\\equipments.txt";
	
	public static void main(String[] args) throws IOException {
	int nbEquipments = 0;
		int max = 0;
		int min = 1000000;
		
		BufferedReader br = new BufferedReader(new FileReader(fileListEquipment));
		String equipment = null;
		while ((equipment = br.readLine()) != null) {
			nbEquipments++;
			int asciiSum = 0;
			if (equipment != null) {
				for (int i=0;i<equipment.length();i++) {
					asciiSum  += equipment.charAt(i);
				}
			}
			int index = asciiSum % modulo;
			if (hashMap.get(index) != null) {
				int val = hashMap.get(index);
				val++;
				hashMap.put(index, val);
			} else hashMap.put(index, 1);
		}
	
	System.out.println("Nb Equipments : " + nbEquipments);
	
	  for (int j=0; j < hashMap.size();j++) {
		  System.out.println("INDEX:" + j + " nb:" + hashMap.get(j));
	  }
	  for (int j=0; j < hashMap.size();j++) {
		  int val = hashMap.get(j);
		 // System.out.print(val + " ");
		  if (val > max) max = val;
		  if (val < min ) min = val;
	  }
	  System.out.println();
	  float idelaDelta =  nbEquipments/modulo;
	  float mindelta = 100 - ((100/idelaDelta)*min);
	  float maxdelta = 100 - ((100/idelaDelta)*max);
	  System.out.println("Delta Equipments min:" +  mindelta + " max:" + maxdelta);
	  
	}

}


I have tried the following code, however I cannot find a replacement that satisfies the get

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestModulo
{
    class Program
    {
        public static Dictionary<int, int> hashMap = new Dictionary<int, int>();
        public static int modulo = 9;
        public static string fileListEquipment = "C:\\equipments.txt";


        static void Main(string[] args)
        {
            int nbEquipments = 0;
            int max = 0;
            int min = 1000000;

            System.IO.StreamReader file = new System.IO.StreamReader(fileListEquipment);
            String equipment = null;
            while ((equipment = file.ReadLine()) != null)
            {
                nbEquipments++;
                int asciiSum = 0;
                if (equipment != null)
                {
                    for (int i = 0; i < equipment.Length; i++)
                    {
                        asciiSum += equipment[i];
                    }
                }
                int index = asciiSum % modulo;
                if (hashMap[index] != null)
                {
                    int val = hashMap.TryGetValue[index];
                    
                    val++;
                    hashMap.Add(index, val);
                }
                else hashMap.Add(index, 1);
            }

            Console.WriteLine("Nb Equipments : " + nbEquipments);

            for (int j = 0; j < hashMap.Count(); j++)
            {
                Console.WriteLine("INDEX:" + j + " nb:" + hashMap.ContainsValue(j));
            }
            for (int j = 0; j < hashMap.Count(); j++)
            {
                int val = hashMap[j];
                
                if (val > max) max = val;
                if (val < min) min = val;
            }
            Console.WriteLine();
            float idelaDelta = nbEquipments / modulo;
            float mindelta = 100 - ((100 / idelaDelta) * min);
            float maxdelta = 100 - ((100 / idelaDelta) * max);
            Console.WriteLine("Delta Equipments min:" + mindelta + " max:" + maxdelta);
        }
    }
}


Any help please of what I can use. Thanks
Posted

Either
C#
if(hashMap.ContainsKey(index))
{
   int val = hashMap[index];
   val++;
   hashMap[index] = val; // can't use hashMap.Add(index, val) if index is already contained

   // or the short version for the 3 lines above:
   // hashMap[index]++;
}

or
C#
int val;
if(hashMap.TryGetValue(index, out val))
{
   val++;
   hashMap[index] = val;
}


Edit: The first version (using the method ContainsKey) has worse performance than the second because you're asking the Dictionary three times to look up the key (instead of two times).
Richards solution above (omitting the if-check) is the most concise one because it makes the else-part obsolete. But it's only safe to use/not ambiguous because the default value of your value type of your Dictionary (in this case 0 for int) can't be a valid entry.
 
Share this answer
 
v2
Comments
Maciej Los 8-Jan-16 10:56am    
5!
Sascha Lefèvre 8-Jan-16 11:56am    
Thank you, Maciej! :)
Matt T Heffron 8-Jan-16 13:27pm    
+5
Sascha Lefèvre 8-Jan-16 13:43pm    
Thanks, Matt! :)
Try something like this:
C#
int index = asciiSum % modulo;

int val;
hashMap.TryGetValue(index, out val);

// TryGetValue returns a bool indicating whether the index was found, but we don't care:
// * If the index is in the dictionary, val will be the current value.
// * If the index is missing, val will be set to 0.

val++;
hashMap[index] = val;

Dictionary(TKey, TValue).TryGetValue Method (TKey, TValue) (System.Collections.Generic)[^]
 
Share this answer
 
v2
Comments
datt265 8-Jan-16 10:19am    
Thanks Richard, the solution worked, however I included the if statment as Sascha suggested.
Maciej Los 8-Jan-16 10:55am    
5!
Matt T Heffron 8-Jan-16 13:26pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900