what symbol to use for integer in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ikkyizzy
    New Member
    • Nov 2016
    • 16

    what symbol to use for integer in Java

    Code:
    public static FoodTruck getTruckByTruckId(int truckId){
           FoodTruck course=new FoodTruck("","","","","");
           fillCourseListFromFile();
           for(int i=0;i<foodTruckList.size();i++){
               if(foodTruckList.get(i).getTruckId()==(truckId)){
                   course=foodTruckList.get(i);
                   break;
               }
           }
           return course;
       }
    I know this is a question of a complete newbie.

    I have an error in the following line string cannot be converted to int

    The underlined value is an integer , how do I represent
    an int in Java

    FoodTruck course=new FoodTruck("","","","","" );

    String represent " " Int represent ??
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    To which number do you want to convert the empty string? To 0? To -1 ? To Integer.MAX? To String.hashCode ()?
    All is reasonable depending on the functon specification.
    Either your task is to extract a number from a string and print an error if not given. Then you can use Integer.parseIn t().
    Or your task is to map strings to unique Ids. Then you have 2 possibilities. If the srrings are short then you can convert the string to hexcode, but if the strings can have an arbitrary length, then you must use an internal map, where you store all strings and their assigned unique Ids derived from a global counter.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      Dera ikkyizzy, please review all your other threads, read my answer and provide requested details.
      Why do you start so many questions in parallel and do not continue on one to solve it? If they are solved for you, please write down your solution to help others with the same problem.

      Comment

      • ikkyizzy
        New Member
        • Nov 2016
        • 16

        #4
        above method is used to get truck from a list of trucks.
        When the user entered in the form the truck Id is searched through this method..
        FoodTruck course=new FoodTruck("","" ,"","","");

        above is the datatype of the attributes first attribute is an int so when I write " " its getting an error because that should
        be an integer. I want to know if its an integer how would I write
        eg: if I write like below there is no error but I don't know
        whether its correct or not

        FoodTruck course=new FoodTruck( 0,"","","","" );

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          The constructor of the class FoodTruck is written in a way that it only accepts integers as truck-IDs, but not strings! So you can give an integer directly (as in your example) or you can try to extract it from a string:

          If you want to convert a number to a String, use following code:
          Code:
          String idAsString="123";
          int id;
          try {
                id=Integer.parseInt(idAsString);
          } catch (NumberFormatException e) {
                id=0; // this is the default value in case the string did not contain a number;
          }
          FoodTruck course=new FoodTruck( id,"","","","");

          Comment

          Working...