Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By mousumiy

Thread: Help: Convert String to Integer

  1. #1
    Firer44 is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Help: Convert String to Integer

    Okay, so I have to make a programm where the user gives you the bank sorting code and the account number and you give him the IBAN. That was so far no problem and I was done within minutes except of one thing that I simply can't figure out even though im trying since weeks. At some point I have to convert a string to integer. My research told me its with parseInt() and I dont get a syntax error when I compile my programm (using BlueJ). But when executing the programm stops and gives me some weird bug message. Anywhay without further ado I really hope you guys can help me, because I am growing desperate Here is my code and my bug message:
    Java Code:
    public class IBAN
    {
        
        public IBAN(String Bankleitzahl, String Kontonummer)
        {
           Bankleitzahl=Bankleitzahl.replace(" ",""); // Die Leerzeichen werden entfernt
           int Anzahl=Bankleitzahl.length(); // Auf der Variabel Anzahl wird die Anzahl der Zeichen von der Bankleitzahl gespeichert
           if (Anzahl!=8){
               System.out.println("Eine Bankleitzahl kann nur 8 Zeichen haben. Bitte erneut eingeben und auf Fehler überprüfen.");
           } // Es wird überprüft ob die Bankleitzahl aus 8 Zeichen besteht
           String Laendercode="131400"; // Laendercode von Deutschland
           int kAnzahl=Laendercode.length()+Bankleitzahl.length()+Kontonummer.length(); // Anzahl der Zeichen von BLZ, KN und LC
           if (kAnzahl <24){
               int nullen=24-kAnzahl;
               for (int i=0;i<nullen;i++){
                   Bankleitzahl=Bankleitzahl+"0";
               } // Es werden soviele Nullen zwischen der BLZ und der KN aufgefüllt bis man 24 Zeichen hat
            }
           else if (kAnzahl>24){
               System.out.println("Die Kontonummer ist zu lang. Bitte erneut eingeben und auf Fehler überprüfen.");
           }
           String fIBAN=Bankleitzahl+Kontonummer+Laendercode; // BLZ, KN und LC werden zusammengetan
           int ifIBAN = Integer.parseInt(fIBAN);// Wird in int umgewandelt
           int zahl=ifIBAN%97; // WIrd durch 97 geteilt und der Rest wird gespeichert
           int Pruefziffer=98-zahl; // 98 - dem vorhin ausgerechntetem Rest
           System.out.println("DE"+Pruefziffer+Bankleitzahl+Kontonummer); // IBAN wird ausgegeben
        }
    }
    Help: Convert String to Integer-screenshot-2014-05-21-21.43.45.png

    Ignore the comments if you can't speak German and ty in advance

    Also before someone thinks that I want him to make my homeworks, I really tried my best, but I simply dont know what the mistake is... So please give me useful tips and not some mysterious advice <3


    (Apparently I can not make lines in a code bold or colour them... The line of interest is the fourth from the bottom :) )
    Last edited by Firer44; 05-21-2014 at 10:05 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Help: Convert String to Integer

    gives me some weird bug message.
    Please copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Firer44 is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help: Convert String to Integer

    You can see it on the picture D:

    (java.lang.NumberFormatException

    From what I got that comes when a not-number is there, because that obviously can not be turned into an Integer, but every single character is a number so that approach was another dead end for me :( )
    Last edited by Firer44; 05-21-2014 at 10:10 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Help: Convert String to Integer

    The full text of the error message should show the invalid String that was passed to the parse method.
    What was in the String passed to the parse method? The image is too small to read.
    The String should only contain valid numeric digits like 0 to 9 for decimal numbers.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Firer44 is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help: Convert String to Integer

    The user enters at the start the accout number and the bank sorting code so in the error message it says the numbers the user entered and the 0's I added in between for example if the user said "22222222" and "4444":

    java.lang.NumberFormatException:
    For input string: "222222220000004444131400" (in java.lang.NumerFormatException)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: Help: Convert String to Integer

    That number is too big to fit in an int. Look for another data type that can hold that large a number.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    944
    Rep Power
    2

    Default Re: Help: Convert String to Integer

    The IBAN is too big for an Integer. Integer.MAX_VALUE = 2147483647. Long. parseLong() is not going to fit either. Here's a nice write up to check the IBAN with mod-97 if you don't have access to 220 bit integers: International Bank Account Number - Wikipedia, the free encyclopedia
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  8. #8
    Firer44 is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help: Convert String to Integer

    LOL

    Thats all? It was that easy? Omg thanks bro xD <3333

    Let me try double and wish me luck :D

  9. #9
    Firer44 is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help: Convert String to Integer

    Omg it works. Thanks man, youre the best xD

  10. #10
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: Help: Convert String to Integer

    why is this in advanced java?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Help: Convert String to Integer

    Did you consider using the BigInteger class? It has arbitrary sized integers and a mod method (plus lots of other useful methods).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    Firer44 is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help: Convert String to Integer

    Well apparently Double was to Short as well so I went with float and the program works Perfect <3

    Only had to take (once the Number was smaller) the (int) from it because otherwise for example 22 Would be 22.0 xD

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: Help: Convert String to Integer

    Double is a larger primitive than a float...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,972
    Rep Power
    5

    Default Re: Help: Convert String to Integer

    Its the "just try stuff" effect; multiple things were tried without really reading into what it means, one of those things solved the problem (or rather: made the problem go away), and the last thing tried (switching from double to float) is marked as the winner.

    Exactly the opposite of learning something, you become less informed. Just trying stuff is not the way to go.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    mousumiy is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 6
    Last Post: 09-14-2012, 12:33 PM
  2. How to convert integer to String
    By mCoder in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 04:00 AM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. How to convert Integer[] to int[]
    By Nithya in forum New To Java
    Replies: 26
    Last Post: 02-11-2010, 05:41 PM
  5. how to convert a string to an integer
    By LAW in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 03:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •