473,413 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

what symbol to use for integer in Java

Expand|Select|Wrap|Line Numbers
  1. public static FoodTruck getTruckByTruckId(int truckId){
  2.        FoodTruck course=new FoodTruck("","","","","");
  3.        fillCourseListFromFile();
  4.        for(int i=0;i<foodTruckList.size();i++){
  5.            if(foodTruckList.get(i).getTruckId()==(truckId)){
  6.                course=foodTruckList.get(i);
  7.                break;
  8.            }
  9.        }
  10.        return course;
  11.    }
  12.  
  13.  
  14.  
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 ??
Dec 8 '16 #1
4 1250
chaarmann
785 Expert 512MB
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.parseInt().
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.
Dec 8 '16 #2
chaarmann
785 Expert 512MB
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.
Dec 8 '16 #3
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,"","","","");
Dec 8 '16 #4
chaarmann
785 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. String idAsString="123";
  2. int id;
  3. try {
  4.       id=Integer.parseInt(idAsString);
  5. } catch (NumberFormatException e) {
  6.       id=0; // this is the default value in case the string did not contain a number;
  7. }
  8. FoodTruck course=new FoodTruck( id,"","","",""); 
  9.  
Dec 8 '16 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mr_burns | last post by:
hi, if i have a datebase field like so: $username = $row; //after SELECT and mysql_query etc. how do i check what file type it is? i have tried using is_string to check it but it returns...
0
by: Nicky | last post by:
Hi! I just wanted to know - what did Java 1.1 have that Java 1.0 didn't have? Was it JDBC and JavaBeans? When were the ORB APIs added to Java? Hope I'm not being too inquisitive.
2
by: Carlitos | last post by:
Hi there, Does anybody know what would be the counterpart of Java's URL Class in .NET? Also, does anybody know a good web site or reference book that would give this kind of information (like...
1
by: bhaskarguttikonda | last post by:
hi, pl tell me some of the java reporting tools ,which can access data from different databases regards, Bhaskar
2
by: kutzi_lala_yey | last post by:
If You were hired by a gasoline firm to create a program that will tell whether which route is faster and which route is more likely to be fuel efficient separately. A test car was provided for you...
2
by: HaifaCarina | last post by:
i have this unfinished java program and i can't figure out what is the problem.. please help... /** * @(#)Answer3.java * * * @author * @version 1.00 2008/1/17
1
by: JanineXD | last post by:
Hey, I seem to have a problem on a Java Swing Program with ActionListener. I've tried to use getSource in our class but seem don't have an idea why it won't work when I tried to program at...
2
by: rafah | last post by:
I just want to know what is the work of(java.io.Serializable),how can i use it ?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.