1

/* * program can convert decimal to binary and report if an illegal char is * used *program cant convert binary to decimal */ import java.util.Scanner;

/** * This class contains a complete program, with just a main() method, for * converting non-negative decimal integers (ie, base 10 integers) to * positive binary integers (ie, base 2 integers). The values to be * converted are read in from the command line. */ public class BaseConversions2 { public static void main(String[] args) {

    //prints the intro to the program if the user does not give any
    //numbers
    if (args.length == 0)
    {
        printOpening();
        printIntro();

        return;
    }

    //method to check and convert numbers given by the user
    runCovert(args);
}

private static void runCovert(String[] args)
{
    for (int i = 0; i < args.length; ++i)
    {
        if (args[i].startsWith("d"))
        {

            decimalToBinary(args);
            ++i;

        }

        else
        {

            binaryToDecimal(args);
            ++i;
        }

    }
}

//method to check if the decimal number the user gave is valid
//if it is convert it
//if not print an error
//method to check if the binary number the user gave is valid
//if it is convert it
//if not print an error
public static void binaryToDecimal(String[] args)
{

    for (int i = 0; i < args.length; ++i)
    {

        String value = args[i].replaceAll("b", "");
//                String test = value.replaceAll("\\s+", "");

        if (value.matches("[0-1]+"))
        {
            int test2 = Integer.parseInt(value, 2);
            if (test2 >= 0 && test2 <= 11111111111111l)
            {
                System.out.println("The binary value " + args[i]
                    + " is equivalent to the decimal value "
                    + test2);
                pause();
                System.out.println("");
                ++i;
            }
            else
            {
                System.out.println("Error: "
                    + "The decimal value " + args[i]
                    + " is out of range");

                pause();

            }

        }
        else
        {
            System.out.println("Error: The decimal value "
                + args[i]
                + " has an illegal character in it, ex -+/");

            pause();

        }

    }
}

//method that runs the two conversion methods above
private static void decimalToBinary(String[] args)
{

    for (int i = 0; i < args.length; ++i)
    {

        String value = args[i].replaceAll("d", "");
//                String test = value.replaceAll("\\s+", "");

        if (value.matches("[0-9]+"))
        {
            int test2 = Integer.valueOf(value);
            if (test2 >= 0 && test2 <= 65535)
            {
                System.out.println("The decimal value " + args[i]
                    + " is equivalent to the binary value "
                    + Integer.toBinaryString(test2));
                pause();
                System.out.println("");

            }
            else
            {
                System.out.println("Error: "
                    + "The decimal value " + args[i]
                    + " is out of range, "
                    + "or does not start with the "
                    + "letter \"d");

                pause();

            }

        }
        else
        {
            System.out.println("Error: The decimal value "
                + args[i]
                + " has an illegal character in it, ex -+/");

            pause();

        }

    }
}

private static void printIntro()
{

    System.out.print("This program allows the user to convert "
        + "either decimal integer values to their\n"
        + "equivalent binary values, or vice versa. "
        + "In either case, it then displays, on\n"
        + "the standard output, a sentence containing "
        + "both the original and the converted\n"
        + "values. Values to be converted are entered on "
        + "the command line, separated by a\n"
        + "blank space. Decimal values must have a d "
        + "as their first character and binary\n"
        + "values must have a b as their first character.");
    System.out.println("");
    System.out.println("");
    System.out.print("The program may convert any number "
        + "of values of either type on any given run.\n"
        + "A value is valid only if it contains just "
        + "those digits allowed by its base,\n"
        + "and lies within the permitted range. "
        + "Our maximum values are: 1111111111111111\n"
        + "for binary, and 65535 for decimal. As numbers, "
        + "these maximum values are equal.\n"
        + "The minimum value in both cases is 0.");
    System.out.println("");
    System.out.println("");
    System.out.println("If any given value does not satisfy the "
        + "necessary criteria, an error must be\n"
        + "reported and the value must be ignored by "
        + "the program, which simply carries\n"
        + "on after reporting the error. In addition, "
        + "if the initial character of a value\n"
        + "is neither b nor d, the program also reports "
        + "the error and carries on.");
    System.out.println("");
    System.out.println("");
    System.out.println("\t\t\t\t\t\t\t\tScreen 1 of 1");

    pause();
}

//method to print the opening screen
private static void printOpening()
{



    pause();
    System.out.println("");
    System.out.println("");
}

//method to pause the program
private static void pause()
{
    Scanner kbd = new Scanner(System.in);

    System.out.print("\n\n\n\nPress Enter to continue ...");
    kbd.nextLine();

}

}

i need help working out why my code is not working, basically my code take a binary number and converts it to a decimal and vise versa, however when i put a binary and a decimal numbers in at the same time it say one of them has an illegal character in it and i need to work out why is do this.

i know this is a lot of code but help would really be appreciated as im really stuck right now

Ulysses
  • 11
  • 5
  • Change it to `display: block;` and it should work. Or add a `margin-right` to the image instead. – domsson Mar 06 '17 at 09:56
  • Okay, that was quite a drastic edit right there. There is just so much wrong. You have an empty `h2`. You have inline CSS. You don't need the whole `html` and `head` stuff in the jsfiddle. You have `

    ` everywhere when that is something you should do with CSS margins... I would need to substantially change my answer as well, not sure I'll have the time right now. I'll delete my answer for now.
    – domsson Mar 06 '17 at 10:32
  • Possible duplicate of [CSS Float: Floating an image to the left of the text](http://stackoverflow.com/questions/5198392/css-float-floating-an-image-to-the-left-of-the-text) – domsson Mar 06 '17 at 10:36
  • yeah i know that there is a lot wrong here hover i am using a template to make this website and i have been fighting with the other style sheets to change things but sometimes it wont let me do simple stuff – Ulysses Mar 06 '17 at 10:37

4 Answers4

0

If it is not necessary, just remove .contact, or add a class to the <p>. Your code says that change all instance of <p> that is a child of the .contact, but I can't see any element with a class of .contact, that's why it does not affect the <p>

EDIT

Added new css thanks to domdom, and removed !important

img {
  margin-right: 5px;
}
<img src="images/soyuz1.jpg" alt="" width="25px" align="left">
<p>
  The Soyuz TMA-M is the latest version in the series of Russian Soyuz Spacecraft that are currently used to Transport Crew to and from the International Space Station.
</p>
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
  • 1
    Actually, did you run your own code? It shows the exact behavior the OP wants to avoid: the start of the lines isn't aligned. Adding a `margin-right` to the image would work. Also, while you're at it, you could've removed the unnecessary `!important`. – domsson Mar 06 '17 at 10:01
  • 1
    I didn't really understand what the OP wants, I just edited what I saw is wrong. Forgot to remove the `!important` – Carl Binalla Mar 06 '17 at 10:03
  • Also, we both missed the `align` attribute of the image I guess. Anyway, there now is a `contact` class, OP updated his question with (a lot) more code now. – domsson Mar 06 '17 at 10:43
0

assign display:inline css property for both and

tag.Then reduce the image size.You will get what you need.That is beacuse image size is bigger thet ehat line hight.

Mr.Pandya
  • 1,899
  • 1
  • 13
  • 24
0

Add a div outside the p and img elements. Or, just add class contact to your p element.

.contact {
     display: inline !important;
     margin-left: 5px !important;
 }
<img src="images/soyuz1.jpg" alt="" width="25px" align="left"><p class="contact">The Soyuz TMA-M is the latest version in the series of Russian Soyuz Spacecraft that are currently used to Transport Crew to and from the International Space Station.</p>
uvishere
  • 455
  • 6
  • 22
-1
You to use this code

<p><img src="images/soyuz1.jpg" alt="" width="25px" align="left" style="margin-right:20px;">The Soyuz TMA-M is the latest version in the series of Russian Soyuz Spacecraft that are currently used to Transport Crew to and from the International Space Station.</p>
  • No formatting, no fiddle, inline-styles and the `align` attribute... not exactly a helpful answer for someone (probably) still learning HTML/CSS. Plus, OP changed his question drastically since the beginning, you might want to take another look. – domsson Mar 06 '17 at 10:41