Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If problems

 
Gavin Walsh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}



}


}

}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 if there is actually a char at that index.
 
Gavin Walsh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks Marc.

Im all new to this and Im trying to learn as i go along...so please bare with my ineptness

would i then have to call this in an if statement...im just not sure where i need to delcare this..

any orientation at all would be much apperiacted
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gavin Walsh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}

}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try calling length on your String...

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.
 
Gavin Walsh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specific problems are you having with this? This will help us focus.

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 char 1 (which happens to have an int value of 49). If you want this as an int, don't use quotes.
  • What does (strlength <= 1) really tell you? You want to use this to avoid exceptions when calling charAt(x). But you might have an empty String with length 0, in which case calling charAt(0) would still give an exception.
  •  
    Gavin Walsh
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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");
    }



    }

    }
    }

    }
    }
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
  • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic