Skip to content

Commit e8f04f4

Browse files
authored
Merge pull request TheAlgorithms#339 from LeeChungWan/master
Updated OctalToDecimal.java
2 parents 909de56 + 970be08 commit e8f04f4

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Conversions/OctalToDecimal.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,41 @@
77
*
88
*/
99
public class OctalToDecimal {
10-
10+
1111
/**
1212
* Main method
1313
*
14-
* @param args Command line arguments
14+
* @param args
15+
* Command line arguments
1516
*/
1617
public static void main(String args[]) {
1718
Scanner sc = new Scanner(System.in);
18-
int o = sc.nextInt();
19-
System.out.println("Decimal equivalent: " + convertOctalToDecimal(o));
19+
System.out.print("Octal Input: ");
20+
String inputOctal = sc.nextLine();
21+
int result = convertOctalToDecimal(inputOctal);
22+
if (result != -1)
23+
System.out.println("Result convertOctalToDecimal : " + result);
2024
sc.close();
2125
}
22-
26+
2327
/**
24-
* This method converts an octal number to
25-
* a decimal number.
28+
* This method converts an octal number to a decimal number.
2629
*
27-
* @param o The octal number
30+
* @param inputOctal
31+
* The octal number
2832
* @return The decimal number
2933
*/
30-
public static int convertOctalToDecimal(int o) {
31-
System.out.print("Octal Input: ");
32-
// Read the input from the console which we are expecting as an octal number:
33-
Scanner s = new Scanner(System.in);
34-
String inputHex = s.nextLine();
35-
try{
34+
public static int convertOctalToDecimal(String inputOctal) {
35+
36+
try {
3637
// Actual conversion of Octal to Decimal:
37-
Integer outputDecimal = Integer.parseInt(inputHex, 8);
38-
System.out.println("Decimal Equivalent : " + outputDecimal);
39-
}
40-
catch(NumberFormatException ne){
41-
// Printing a warning message if the input is not a valid octal number:
38+
Integer outputDecimal = Integer.parseInt(inputOctal, 8);
39+
return outputDecimal;
40+
} catch (NumberFormatException ne) {
41+
// Printing a warning message if the input is not a valid octal
42+
// number:
4243
System.out.println("Invalid Input, Expecting octal number 0-7");
43-
}
44-
finally{
45-
s.close();
44+
return -1;
4645
}
4746
}
4847
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy