Basically im trying to read one and two characters into a string and output a number. Please see code below. Basically it will let me input two characters but not one.
Any ideas,
Thank you.
Gavin
here is my code:
// Gavin Walsh  BIT  2nd year
// Week3 / Part 2
import java.io.*;
import java.text.*;
public class prac3roman22
{
public static void main(String args[]) throws IOException
{
String input_string;
int decimal = 0;
int decimal2 = 0;
char numeral;
char numeral2;
BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Numeral 1: ");
input_string = myInput.readLine();
numeral = input_string.charAt (0);
numeral2 = input_string.charAt (1);
{
if (numeral == 'i')
decimal = 1;
else if (numeral == 'v')
{
decimal = 5;
}
else if (numeral == 'x')
{
decimal = 10;
}
else if (numeral == 'L')
{
decimal = 50;
}
else if (numeral == 'c')
{
decimal = 100;
}
else if (numeral == 'd')
{
decimal = 500;
}
else if (numeral == 'm')
{
decimal = 1000;
}
}
{
{
if (numeral2 == 'i')
decimal2 = 1;
else if (numeral2 == 'v')
{
decimal2 = 5;
}
else if (numeral2 == 'x')
{
decimal2 = 10;
}
else if (numeral2 == 'l')
{
decimal2 = 50;
}
else if (numeral2 == 'c')
{
decimal2 = 100;
}
else if (numeral2 == 'd')
{
decimal2 = 500;
}
else if (numeral2 == 'm')
{
decimal2 = 1000;
}
}
//int newNumber = decimal + decimal2;
//System.out.println("The decimal equivelant is " + newNumber);
{
if (decimal < decimal2)
{
int newDecimal = (decimal2  decimal);
System.out.println("The decimal equivelant is " + newDecimal);
}
else
{
System.out.println("Not a numeral value");
}
if (decimal >= decimal2)
{
int newDecimal = (decimal2 + decimal);
System.out.println("The decimal equivelant is " + newDecimal);
}
else
{
System.out.println("Not a numeral value");
}
}
}
}
}
numeral2 = input_string.charAt(1);
Basically, you need to ensure that this line is only called if there is actually a char at that index.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Hint: You might find the length() method of the String class to be very useful here. That way, you will know exactly how many characters you're dealing with.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Thankyou.
I have tried something but still no joy, ive been at this for hours, so any help AT ALL would be great:
I added this in before my if statements....

{
int length = myInput.getLength();
{
if (length == 1)
numeral = input_string.charAt (0);
}
}
int strLength = input_string.length();
...then instead of an equality comparison (==), which will only return true for that specific value, try using greater than or less than (<, >, >=, <=, etc.), which are more flexible.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
many many thanks for the help....but again a problem,, understand if you cant write back...but again, any help would be great...getting to grips with this is quite hard, and i know it is something simple...
what i have got is:
System.out.print("Numeral 1: ");
input_string = myInput.readLine();
numeral = input_string.charAt (0);
numeral2 = input_string.charAt (1);
//myInput.length();
{
int strlength = input_string.length();
{
if (strlength <= '1')
numeral = input_string.charAt (0);
}
}
{
if (numeral == 'i')
decimal = 1;
else if (numeral == 'v')
{
decimal = 5;
}
else if (numeral == 'x
 and so on.....
A few comments...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Thank you VERY much for your help, a bit of persistance and I got it...Just bit of fiddling around, dont actually know the tehory of how i fixed it, but it works.
Many Thanks once again...
my solution
// Gavin Walsh  BIT  2nd year
// Week3 / Part 2
import java.io.*;
import java.text.*;
public class prac3roman22
{
public static void main(String args[]) throws IOException
{
String input_string;
int decimal = 0;
int decimal2 = 0;
char numeral;
char numeral2;
BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Numeral 1: ");
input_string = myInput.readLine();
numeral = ' ';
numeral2 = ' ';
//myInput.length();
{
int strlength = input_string.length();
{
if (strlength <= 1)
{
numeral = input_string.charAt (0);
}
else if (strlength >= 2)
{
numeral = input_string.charAt (0);
numeral2 = input_string.charAt (1);
}
}
{
if (numeral == 'i')
decimal = 1;
else if (numeral == 'v')
{
decimal = 5;
}
else if (numeral == 'x')
{
decimal = 10;
}
else if (numeral == 'L')
{
decimal = 50;
}
else if (numeral == 'c')
{
decimal = 100;
}
else if (numeral == 'd')
{
decimal = 500;
}
else if (numeral == 'm')
{
decimal = 1000;
}
}
{
{
if (numeral2 == 'i')
decimal2 = 1;
else if (numeral2 == 'v')
{
decimal2 = 5;
}
else if (numeral2 == 'x')
{
decimal2 = 10;
}
else if (numeral2 == 'l')
{
decimal2 = 50;
}
else if (numeral2 == 'c')
{
decimal2 = 100;
}
else if (numeral2 == 'd')
{
decimal2 = 500;
}
else if (numeral2 == 'm')
{
decimal2 = 1000;
}
}
//int newNumber = decimal + decimal2;
//System.out.println("The decimal equivelant is " + newNumber);
{
if (decimal < decimal2)
{
int newDecimal = (decimal2 + decimal);
System.out.println("The decimal equivelant is " + newDecimal);
}
//else
//{
//System.out.println("Not a numeral value");
//}
if (decimal >= decimal2)
{
int newDecimal = (decimal2 + decimal);
System.out.println("The decimal equivelant is " + newDecimal);
}
else
{
System.out.println("Not a numeral value");
}
}
}
}
}
}
Originally posted by Gavin Walsh:
...dont actually know the tehory of how i fixed it, but it works...
Well, before you turn this in, I would ask:
Also, please use CODE tags in your post to keep the formatting intact.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
