output problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • straggler
    New Member
    • Jun 2009
    • 1

    output problems

    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??

    Code:
    public class Roman {
    	
    		public void decryptEncrypt (String inputFileName, String outputFileName)
    		{
    			try {
    				FileReader reader = new FileReader(inputFileName) ;
    				Scanner in = new Scanner(reader) ;
    				PrintWriter out = new PrintWriter(outputFileName) ;
    				
    
    				String romanout = " ";
    				while (in.hasNext( ) ){
    					String roman = in.nextLine( ) ;
    					//out.print(roman);
    					for(int i = 0; i < roman.length(); i++) {
    						int num = 0;
    						char digit = ' ';
    						digit = roman.charAt(i);
    						while(digit != -1){
    							if(digit == 'I'){
    								num++;
    								if(digit == 'V'){
    									num = num + 3;
    								}
    								else if(digit == 'X'){
    									num = num + 8;
    								}
    							}
    							if(digit == 'V'){
    								num = num + 5;
    							}
    							if(digit == 'X'){
    								num = num + 10;
    								if(digit == 'L'){
    									num = num + 30;
    								}
    								else if(digit == 'C'){
    									num = num + 80;
    								}
    							}
    							if(digit == 'L'){
    								num = num + 50;
    							}
    							if(digit == 'C'){
    								num = num + 100;
    							}
    							
    						}
    						
    			System.out.println(num);	
    							num = num * 2;
    			//System.out.println(num);
    			
    					while(num > 0){
    							if(num > 100){
    								num = num - 100;
    								romanout = romanout + "C";
    							}
    							if(num == 90){
    								num = num - 90;
    								romanout = romanout + "XC";
    							}
    							if(num > 50){
    								num = num - 50;
    								romanout = romanout + "L";
    							}
    							if(num == 40){
    								num = num - 40;
    								romanout = romanout + "XL";
    							}
    							if(num > 10){
    								num = num - 10;
    								romanout = romanout + "X";
    							}
    							if(num == 9){
    								num = num - 9;
    								romanout = romanout + "IX";
    							}
    							if(num > 5){
    								num = num - 5;
    								romanout = romanout + "V";
    							}
    							if(num == 4){
    								num = num - 4;
    								romanout = romanout + "IV";
    							}
    							if(num > 1){
    								num = num - 1;
    								romanout = romanout + "I";
    							}
    					}
    					out.print(roman);
    					out.print(romanout);
    					}
    					
    				}
    				in.close( ) ;
    				out.close( ) ;	
    				}
    			catch(IOException e)
    			{
    				System.out.println(e) ;
    			}
    		}
    		
    		public static void main(String [] args){	
    			Roman reader = new Roman( ) ;
    			reader.decryptEncrypt("roman.in", "roman.out") ;	
    		}
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    Working...