+ 2

I'm a bit lost here...

I've been trying different ways of coding the same thing and keep coming up with the same (or more! :( errors), I'm not sure where I've gone wrong, but I am very much a beginner and may be being too ambitious. A little help would be much appreciated. I'm trying to put together a small program to calculate the hours I will be teaching in any given month - this is what I've got with the fewest errors: public class Program { public static void main(String[] args) { String [][] SummerGroups = {{"A50", "A70", "KA2",},{"A50", "A70"},{"A50", "A70", "KA2"}}; String a = SummerGroups [0][0] + SummerGroups [1][0] + SummerGroups [2][0]; String b = SummerGroups [0][1] + SummerGroups [1][1] + SummerGroups [2][1]; String c = SummerGroups [0][3] + SummerGroups [2][2]; int sum = (2 * 2 * 4); int Sum = (2 * 3 * 4); System.out.println("a = Sum"); System.out.println("b = Sum"); System.out.println("c = sum"); } } So far it seems pretty logical to me, but I'm sure I've missed something! Hopefully you can see from the code what I'm trying to do, but if you're not sure, ask away and I'll answer when I can.

29th Jun 2019, 9:54 PM
Simon
8 Réponses
+ 7
I am not sure what you are trying to do? Anyway, the error happens because there's no element with index 3 in your array. String c= SummerGroups [0][3] +... Also note that variables and methods in Java should start with lowercase letters (camelCase), and the first letter for a class name should be uppercase (PascalCase). It's just a naming convention for Java.
29th Jun 2019, 10:30 PM
voja
voja - avatar
+ 2
I'm trying to put a program together to add up the hours that I teach for each group of students - A50 etc. (I know I could do the same in excel, but I thought it'd be a good exercise for me) - I've changed the schoolboy error in the array classification, but I now get an output of: a = Sum b = Sum c = sum So I'm still going wrong somewhere! Also, do you mean my classes should be - summergroups or summerGroups? Or sum and sum1 for example?
29th Jun 2019, 10:44 PM
Simon
+ 2
Thank you for your help, I've made a bit of progress! Something is still not quite right though... public class Program { public static void main(String[] args) { String [][] summerGroups = {{"A50", "A70", "KA2",},{"A50", "A70"},{"A50", "A70", "KA2"}}; String a = summerGroups [0][0] + summerGroups [1][0] + summerGroups [2][0]; String b = summerGroups [0][1] + summerGroups [1][1] + summerGroups [2][1]; String c = summerGroups [0][2] + summerGroups [2][2]; int x = (2 * 2 * 4); int y = (2 * 3 * 4); System.out.println("a = y"); System.out.println("b = y"); System.out.println("c = x"); int z = (x + y + y); System.out.println(z); } } //my output is: a = y b = y c = x 64 and I was hoping to see the individual values...
29th Jun 2019, 11:11 PM
Simon
+ 2
https://code.sololearn.com/cfR0p3V5mbd6/?ref=app this code doesn't throw an error and prints what you calculated... but probably not the output your looking for... Is "A50" 50min of group A or something else? 😅 It probably would be easier to make a class for groups, which handels group related tasks to split the problem up and make it easier to implement logic readable for humans... happy coding! 😄
29th Jun 2019, 11:18 PM
Anton Böhler
Anton Böhler - avatar
+ 2
Aye, I did a lot of the coding right, but it's not the answers I was looking for! A50 and all that are just codes for the classes which is why I thought String is the best place to put them....
29th Jun 2019, 11:22 PM
Simon
+ 2
Ideally, I would like my output to be the group code with their corresponding hours and then the total. I'm just going with baby steps now though 🙂
29th Jun 2019, 11:24 PM
Simon
+ 2
could you explain: -how many hours every group gets -what your output would look like in this exemple
30th Jun 2019, 8:16 AM
Anton Böhler
Anton Böhler - avatar
+ 2
The hours for the groups are x and y: 2 hours*2days*4weeks (x) And I was looking for something like this: a = 24 b = 24 c = 16 64 I've had another look this morning and tweaked the code to give me my desired result: public class Program { public static void main(String[] args) { String [][] summerGroups = {{"A50", "A70", "KA2",},{"A50", "A70"},{"A50", "A70", "KA2"}}; String a = summerGroups [0][0] + summerGroups [1][0] + summerGroups [2][0]; String b = summerGroups [0][1] + summerGroups [1][1] + summerGroups [2][1]; String c = summerGroups [0][2] + summerGroups [2][2]; int x = 2 * 2 * 4; int y = 2 * 3 * 4;{ System.out.println("A50 = " + y); System.out.println("A70 = " + y); System.out.println("KA2 = " + x); } int z = (x + y + y); System.out.println(z); } } I'm not sure if the arrays are entirely necessary now though! Thank you all for your help, it's much appreciated :)
30th Jun 2019, 10:50 AM
Simon