Reputation: 19
I have following code:
public static void main(String[] args) {
convert13to7(7);
}
public static int convert13to7(int grade) {
int newGrade = 0;
int[] scale13 = {00, 03, 5, 6, 7, 8, 9, 10, 11, 13};
int[] scale7 = {-3, 00, 00, 02, 4, 7, 7, 10, 12, 12};
for (int i = 0; i < scale13.length; i++) {
if (grade == scale13[i]) {
newGrade = scale7[i];
}
}
return newGrade;
}
The point of this code is to convert an old danish grade scale to a new one but the problem is that I don't get any output when using the convert13to7
function. Does anyone see the issue here?
Upvotes: 0
Views: 96
Reputation: 1025
Hei,
You need to save the output in main or just to print it if you don't need to use it.
public static void main(String[] args) {
convert13to7(7);
}
Replace with
public static void main(String[] args) {
//if you want just to check the output
System.out.println(convert13to7(7));
//or if you want to use it further
int holdValue = convert13to7(7);
}
You also should handle exceptions ( at least ArrayIndexOutOfBoundsException). Here is an example using lambda (almost the same thing)
interface lambda{
int getGrades(int[] scale13, int[] scale7, int grade);
}
public class Grades{
public static void main(String[] args) {
lambda output = (int[] scale13, int[] scale7, int grade) ->{
int newGrade = 0;
try {
for (int i = 0; i < scale13.length; i++)
if (grade == scale13[i])
newGrade = scale7[i];
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException Exeption.");
}
return newGrade;
};
int[] scale13 = {00, 03, 5, 6, 7, 8, 9, 10, 11, 13};
int[] scale7 = {-3, 00, 00, 02, 4, 7, 7, 10, 12, 12};
System.out.println(output.getGrades(scale13, scale7, 7));
}
}
Upvotes: 0
Reputation: 18255
To print the result, use System.out.println()
:
int grade = convert13to7(7);
System.out.println(grade);
I would recommend you to use Map
, not to iterate every time over an array:
public final class GradeConverter {
private final Map<Integer, Integer> map13to7 = new HashMap<>();
{
map13to7.put(0, -3);
map13to7.put(3, 0);
map13to7.put(5, 0);
map13to7.put(6, 2);
map13to7.put(7, 4);
map13to7.put(8, 7);
map13to7.put(9, 7);
map13to7.put(10, 10);
map13to7.put(11, 12);
map13to7.put(13, 12);
}
public int convert10to7(int grade) {
return map13to7.getOrDefault(grade, 0);
}
}
Next point, pay attention to writing 03
instead of 3
because in Java 0
prefix is a marker of the octal system. E.g. 077
is a decimal value 63
.
Upvotes: 1
Reputation: 49646
convert13to7
produces a converted value, it doesn't print it.
System.out.println(convert13to7(7));
Other small issues:
Think about validation. Returning a 0
if you've failed to convert the input is not a good option.
Think about List.indexOf
. It's simpler and more intuitive.
Upvotes: 4
Reputation: 271
You need to collect the output where you have called the method.
package com;
public class Test{
public static void main(String[] args) {
// collect output here
int newGrade = convert13to7(7);
System.out.println(newGrade);
}
public static int convert13to7(int grade) {
int newGrade = 0;
int[] scale13 = {00, 03, 5, 6, 7, 8, 9, 10, 11, 13};
int[] scale7 = {-3, 00, 00, 02, 4, 7, 7, 10, 12, 12};
for (int i = 0; i < scale13.length; i++) {
if (grade == scale13[i]) {
newGrade = scale7[i];
}
}
// alternatively you can print output here
System.out.println(newGrade);
return newGrade;
}
}
Upvotes: 0