# If problems

Gavin Walsh

Greenhorn

Posts: 14

posted 11 years ago

Ok, so i am having problems with this.

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");

}

}

}

}

}

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");

}

}

}

}

}

posted 11 years ago

If you enter a String with less than 2 characters, you will have problems with this line...

numeral2 = input_string.charAt(1);

Basically, you need to ensure that this line is only called

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.*~Joe Strummer*

sscce.org

Gavin Walsh

Greenhorn

Posts: 14

posted 11 years ago

There are different ways to handle this. Using an if statement would be one way.

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.

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.

*~Joe Strummer*

sscce.org

Gavin Walsh

Greenhorn

Posts: 14

posted 11 years ago

marc,

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);

}

}

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);

}

}

posted 11 years ago

Try calling length on your String...

int strLength = input_string.length();

...then instead of an equality comparison (==), which will only return true for

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.*~Joe Strummer*

sscce.org

Gavin Walsh

Greenhorn

Posts: 14

posted 11 years ago

marc,

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

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

posted 11 years ago

What

A few comments...

You forgot to remove (or comment out) the numeral and numeral2 assignments underneath the readLine. When you test the strlength, note that characters in single quotes are interpreted as chars. So '1' is the What does (strlength <= 1) really tell you? You want to use this to avoid exceptions when calling charAt(x). But you

*specific*problems are you having with this? This will help us focus.A few comments...

*char*1 (which happens to have an int value of 49). If you want this as an int, don't use quotes.

*might*have an empty String with length 0, in which case calling charAt(0) would still give an exception.

*~Joe Strummer*

sscce.org

Gavin Walsh

Greenhorn

Posts: 14

posted 11 years ago

Marc,

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");

}

}

}

}

}

}

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");

}

}

}

}

}

}

posted 11 years ago

Well, before you turn this in, I would ask:

What if more than 2 characters are entered? For example, vii? What if the characters are entered in a different case (upper/lower)? Is this really working right for cases in which decimal < decimal2?

Also, please use CODE tags in your post to keep the formatting intact.

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.

*~Joe Strummer*

sscce.org