java problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeremyhilg08
    New Member
    • Feb 2008
    • 5

    java problem

    Hey guys, I hope somebody can help. I'm writing a program to reverse a number, and I'm getting an error code and can't figure out why. Hope someone can help. Thanks

    import java.util.*;
    class Assign4B
    {
    public static void main(String[]args)
    {
    int number;
    int x;
    int reversenumber;
    Scanner input = new Scanner(System. in);
    System.out.prin tln("Please type a four-digit number for x");
    number = input.nextInt() ;
    number = x;
    int ones;
    int tens;
    int hundreds;
    int thousands;
    thousands = x / 1000;
    x -= thousands * 1000;
    hundreds = x / 100;
    x -= hundreds * 100;
    tens = x / 10;
    x -= tens * 10;
    ones = x;
    x = ones*1000 + tens*100 + hundreds*10 + thousands;
    System.out.prin tf("Reverse:%8. 2f",x);
    }
    }


    out:variable x has not been initialized??
  • jimhawkss
    New Member
    • Feb 2008
    • 10

    #2
    Your taking in a string and trying to make it backwards right?
    try a simple for loop.
    Let's pretend your input string is called input and it contains 1234.

    tempInt = input.length(); //might not need the () can't remember atm.
    string outPut = ""
    for (int c = 0; c <= tempInt; tempInt--)
    {
    char tempChar = input.getChar(t empInt);
    outPut += tempChar
    }

    then just printline your outPut string.... and you should get 4321.

    Hopefully that helps

    Comment

    • kedmotsoko
      New Member
      • Feb 2008
      • 8

      #3
      import java.util.*;
      class Assign4B
      {
      public static void main(String[]args)
      {
      int number;
      int x;
      int reversenumber;
      Scanner input = new Scanner(System. in);
      System.out.prin tln("Please type a four-digit number for x");
      number = input.nextInt() ;
      //number = x;..THE OTHER WAY ROUND!!
      x= number;
      int ones;
      int tens;
      int hundreds;
      int thousands;
      thousands = x / 1000;
      x -= thousands * 1000;
      hundreds = x / 100;
      x -= hundreds * 100;
      tens = x / 10;
      x -= tens * 10;
      ones = x;
      x = ones*1000 + tens*100 + hundreds*10 + thousands;
      //System.out.prin tf("Reverse:%8. 2f",x);..INTERG ER CAN"T BE FORMATED TO 2 DECIMAL PLACE
      System.out.prin tln(x);
      }
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by jimhawkss
        Your taking in a string and trying to make it backwards right?
        try a simple for loop.
        Let's pretend your input string is called input and it contains 1234.

        tempInt = input.length(); //might not need the () can't remember atm.
        string outPut = ""
        for (int c = 0; c <= tempInt; tempInt--)
        {
        char tempChar = input.getChar(t empInt);
        outPut += tempChar
        }

        then just printline your outPut string.... and you should get 4321.

        Hopefully that helps
        If you want to reverse a String (that might accidentally look like a number) please
        let the core classes do the dirty work. Have a look at the StringBuilder.r everse()
        method.

        kind regards,

        Jos

        Comment

        • jeremyhilg08
          New Member
          • Feb 2008
          • 5

          #5
          Originally posted by kedmotsoko
          import java.util.*;
          class Assign4B
          {
          public static void main(String[]args)
          {
          int number;
          int x;
          int reversenumber;
          Scanner input = new Scanner(System. in);
          System.out.prin tln("Please type a four-digit number for x");
          number = input.nextInt() ;
          //number = x;..THE OTHER WAY ROUND!!
          x= number;
          int ones;
          int tens;
          int hundreds;
          int thousands;
          thousands = x / 1000;
          x -= thousands * 1000;
          hundreds = x / 100;
          x -= hundreds * 100;
          tens = x / 10;
          x -= tens * 10;
          ones = x;
          x = ones*1000 + tens*100 + hundreds*10 + thousands;
          //System.out.prin tf("Reverse:%8. 2f",x);..INTERG ER CAN"T BE FORMATED TO 2 DECIMAL PLACE
          System.out.prin tln(x);
          }
          }
          Thanks alot for the help. It worked out fine.

          Comment

          Working...