Connecting Tech Pros Worldwide Forums | Help | Site Map

output problems

Newbie
 
Join Date: Jun 2009
Posts: 1
#1: Jun 18 '09
I created a program to read in a file, transform the given roman numeral, double the numeral, and then output the new roman numeral.

I am not even sure yet if my Roman decrpyt and encrpytion is right because nothing is outputting into the roman.out file??

Expand|Select|Wrap|Line Numbers
  1. public class Roman {
  2.  
  3.         public void decryptEncrypt (String inputFileName, String outputFileName)
  4.         {
  5.             try {
  6.                 FileReader reader = new FileReader(inputFileName) ;
  7.                 Scanner in = new Scanner(reader) ;
  8.                 PrintWriter out = new PrintWriter(outputFileName) ;
  9.  
  10.  
  11.                 String romanout = " ";
  12.                 while (in.hasNext( ) ){
  13.                     String roman = in.nextLine( ) ;
  14.                     //out.print(roman);
  15.                     for(int i = 0; i < roman.length(); i++) {
  16.                         int num = 0;
  17.                         char digit = ' ';
  18.                         digit = roman.charAt(i);
  19.                         while(digit != -1){
  20.                             if(digit == 'I'){
  21.                                 num++;
  22.                                 if(digit == 'V'){
  23.                                     num = num + 3;
  24.                                 }
  25.                                 else if(digit == 'X'){
  26.                                     num = num + 8;
  27.                                 }
  28.                             }
  29.                             if(digit == 'V'){
  30.                                 num = num + 5;
  31.                             }
  32.                             if(digit == 'X'){
  33.                                 num = num + 10;
  34.                                 if(digit == 'L'){
  35.                                     num = num + 30;
  36.                                 }
  37.                                 else if(digit == 'C'){
  38.                                     num = num + 80;
  39.                                 }
  40.                             }
  41.                             if(digit == 'L'){
  42.                                 num = num + 50;
  43.                             }
  44.                             if(digit == 'C'){
  45.                                 num = num + 100;
  46.                             }
  47.  
  48.                         }
  49.  
  50.             System.out.println(num);    
  51.                             num = num * 2;
  52.             //System.out.println(num);
  53.  
  54.                     while(num > 0){
  55.                             if(num > 100){
  56.                                 num = num - 100;
  57.                                 romanout = romanout + "C";
  58.                             }
  59.                             if(num == 90){
  60.                                 num = num - 90;
  61.                                 romanout = romanout + "XC";
  62.                             }
  63.                             if(num > 50){
  64.                                 num = num - 50;
  65.                                 romanout = romanout + "L";
  66.                             }
  67.                             if(num == 40){
  68.                                 num = num - 40;
  69.                                 romanout = romanout + "XL";
  70.                             }
  71.                             if(num > 10){
  72.                                 num = num - 10;
  73.                                 romanout = romanout + "X";
  74.                             }
  75.                             if(num == 9){
  76.                                 num = num - 9;
  77.                                 romanout = romanout + "IX";
  78.                             }
  79.                             if(num > 5){
  80.                                 num = num - 5;
  81.                                 romanout = romanout + "V";
  82.                             }
  83.                             if(num == 4){
  84.                                 num = num - 4;
  85.                                 romanout = romanout + "IV";
  86.                             }
  87.                             if(num > 1){
  88.                                 num = num - 1;
  89.                                 romanout = romanout + "I";
  90.                             }
  91.                     }
  92.                     out.print(roman);
  93.                     out.print(romanout);
  94.                     }
  95.  
  96.                 }
  97.                 in.close( ) ;
  98.                 out.close( ) ;    
  99.                 }
  100.             catch(IOException e)
  101.             {
  102.                 System.out.println(e) ;
  103.             }
  104.         }
  105.  
  106.         public static void main(String [] args){    
  107.             Roman reader = new Roman( ) ;
  108.             reader.decryptEncrypt("roman.in", "roman.out") ;    
  109.         }
  110. }

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 18 '09

re: output problems


In the body of your while loop (starting at line #19) you never re-assign a value to variable digit so if it wasn't equal to -1 to start with that loop never terminates.

kind regards,

Jos
Reply