Kevin Trujillo Kevin Trujillo - 1 year ago 61
Java Question

Trying to pass around a String but receiving "null"

This is probably a simple error that I am over looking but my problem is that when I am trying to return the string "roman", it just returns null.
This is the main program:

/* CCTPG-22 // Assignment 08: Roman Numerals
* @author Kevin R. Trujillo
* @version 10/28/2015
* Purpose: Converts numbers into roman numerals format and vice-a-versa
* Status:
*/

import java.util.*; // For Scanner

public class RomanMain
{
public static void main(String[] args)
{

Scanner in = new Scanner(System.in);

RomanYear year1 = new RomanYear(1975);
RomanYear year2 = new RomanYear(1988);
RomanYear year3 = new RomanYear(2007);

System.out.println(year1.toString());
System.out.println(year2.toString());
System.out.println(year3.toString());
int diff21 = year2.getYearDecimal() - year1.getYearDecimal();
int diff32 = year3.getYearDecimal() - year2.getYearDecimal();

RomanYear y2MinusY1 = new RomanYear(diff21);
RomanYear y3MinusY2 = new RomanYear(diff32);
System.out.println("Year2 minus Year1 is: " + y2MinusY1.getYearRoman());
System.out.println("Year3 minus Year2 is: " + y3MinusY2.getYearRoman());


}

// Add new methods here


} // No code can be here (outside the class)


and here is the RomanYear class:

/**
* Auto Generated Java Class.
*/
public class RomanYear {

private int decimal ;
private String roman ;

public RomanYear() // default constructor
{
decimal = 0 ;
roman = "" ;
}
public RomanYear(int newYear)
{
decimal = newYear ;
roman = setYearDecimal(decimal);
}

public RomanYear(String newYear )
{
roman = newYear ;
decimal = setYearRoman(roman) ;
}

public int setYearRoman(String roman)
{

String romanNumeral = roman.toUpperCase();
for(int x = 0;x < romanNumeral.length(); x++)
{
char convertToDecimal = roman.charAt(x);
// first step: Easy stuff
switch (convertToDecimal)
{
case 'M': decimal += 1000; break;
case 'D': decimal += 500; break;
case 'C': decimal += 100; break;
case 'L': decimal += 50; break;
case 'X': decimal += 10; break;
case 'V': decimal += 5; break;
case 'I': decimal += 1; break;
}
}
// Now adapt for specials
if (romanNumeral.contains("IV"))
{
decimal-=2;
}
if (romanNumeral.contains("IX"))
{
decimal-=2;
}
if (romanNumeral.contains("XL"))
{
decimal-=20;
}
if (romanNumeral.contains("XC"))
{
decimal-=20;
}
if (romanNumeral.contains("CD"))
{
decimal-=200;
}
if (romanNumeral.contains("CM"))
{
decimal-=200;
}
return decimal ;
}

public String setYearDecimal(int yr)
{
if (decimal > 3999)
{
System.out.println("Decimal Number: " + decimal + " is over 3999. ") ;
System.out.println("Please enter a new number") ;
System.out.println("Program is ending.............") ;
try {
Thread.sleep(2000); //5000 milliseconds is one second.
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}

System.exit(0) ;
}

else
{
int digit;
String roman = "";

// 1000's column
digit = yr/1000;
for (int i = 0; i < digit; i++)
roman = roman + "M";
yr = yr % 1000; // leaves 0 to 999

// 100s column
String [] hunds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
digit = yr/100;
roman = roman + hunds[digit];
yr = yr % 100; // leaves 0 to 99

// 10s column
String [] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
digit = yr/10;
roman = roman + tens[digit];
yr = yr % 10; // leaves 0 to 9

// Ones column
String [] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
roman = roman + ones[yr];

}
return roman ;
}

public int getYearDecimal()
{
return decimal ;
}

public String getYearRoman()
{
return roman ;
}

public String toString()
{

System.out.print("Decimal: " + decimal + " as Roman Numerals is " ) ;
return roman ;
}

/* ADD YOUR CODE HERE */

}


If someone could just explain what I am doing wrong, that would be much appreciated rather than just posting the "correct way" to do it.

Thank you!

Answer Source

On the top of the Roman class you create a variable called roman.

 public class RomanYear {

  private int decimal ;
  private String roman ;

In setYearDecimal you create a new variable called roman. That local variable hides the global one.

int digit;
String roman = "";

When you do roman = roman + "M"; you are only updating the local roman variable. The global one remains empty.

All you need to do is remove String from in front of the second variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download