• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do while error

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I've done: I have created a GUI using NetBeans (8.0.2) to store details about Car. The GUI has a jTable where the details of the cars are stored such as; CarReg Number, Car Make and Car price. It works by entering all the details of the Car in three separate jTextFields, once the details are entered the "Add to table" button is selected and it's added to the jTable. There is another jTextfield which the user uses to search for a car according to the price entered. E.g. If price £2000 is entered into the jTextfield and "Search Car" button is pressed then it will return a jOptionPane will the matched price and the Car Make ("Yes, a Ford at price £2000 has been found") otherwise if it did not find the price an error message will appear.

The Problem, If there are two rows in the jTable with different car details E.g:

ROW 1 DETAILS CAR REG: BD51SMR CAR MAKE: FORD PRICE: £2000

ROW 2 DETAILS CAR REG: FS45BKY CAR MAKE: AUDI PRICE: £1000

And you want to search for a car at price £2000 (Row 1) and select "Search Cars" button it will display the following in a jOptionpane: ("Yes, a Ford at price £2000 has been found"). Now if you want to search for a car at price £1000 (Row 2) and select "Search Cars" button it will display the following in a jOptionpane: ("Yes, a Ford at price £1000 has been found"). The above is the error, it will find the matched price but the make of the car still stays as the first column value "Ford" and should have displayed ("Yes, a AUDI at price £2000 has been found").

I'm not quite sure what's wrong with this.

My code:

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable cmake doesn't seen to have any relationship to the price sort. It's just the last make the second do/while loop sees.
 
Kaim Akai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:The variable cmake doesn't seen to have any relationship to the price sort. It's just the last make the second do/while loop sees.



Could you please explain more, i'm a bit confused. Thank you.
 
Ranch Hand
Posts: 165
12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The data fields associated with each car are not being stored together (reg, make, price). So when you look for a price your program doesn't know which car that relates to. Instead it just displays the last car make it happened to have in the cmake variable (from the second while loop) instead of the make that is associated with the selected price.
 
Kaim Akai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steffe Wilson wrote:The data fields associated with each car are not being stored together (reg, make, price). So when you look for a price your program doesn't know which car that relates to. Instead it just displays the last car make it happened to have in the cmake variable (from the second while loop) instead of the make that is associated with the selected price.



What can i do to correct this?
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to define a class. This is fundamental to Java so if this is new to you then you ought to do some reading first, eg online tutorials or a book.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Steffe Wilson is right. You need to create a class. Unfortunately you have gone about the exercise the wrong way; the GUI is the last thing you should create. You may end up throwing away all your work there. You should also stop using NetBeans until you are more experienced, because it has too steep a learning curve. Use a decent text editor; we have an FAQ. My favourite if using Windows would be NotePad++.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic