Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,322 members, 7,808,081 topics. Date: Thursday, 25 April 2024 at 06:54 AM

Please Help Out With Java Program - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help Out With Java Program (2124 Views)

Learning To Program With Java by the Fulaman / Java Program To Solve Quadratic Equation-dealwap / Import Dll Into Java Program (2) (3) (4)

(1) (Reply) (Go Down)

Please Help Out With Java Program by 2legit2qwt: 5:22am On Dec 11, 2011
Hi guys,
            I am working on this program that will read from a file and store each line in an array. However, I want each line stored in an array of an object which i created. I will really appreciate any help on this. I'm gonna paste the class I created which i want the array to be stored by line and also the text file I want to read into it.

Here's my vehicle class

public class Vehicle {
private String vin;
private String make;
private String model;
private int year;
private String color;
private double mileage;
public Vehicle(String aVin, String amake, String amodel, int yr, String acolor,
double mile){

vin= aVin;
make = amake;
model=amodel;
year=yr;
color= acolor;
mileage= mile;

rentalPrice= arentalPrice;
}
public String getvin(){
return vin;
}
public String getmake(){
return make;
}
public String getmodel(){
return model;
}
public int getyear(){
return year;
}
public String getColor(){
return color;
}
public double getmileage(){
return mileage;
}
public double getPrice(){
return Price;
}

public double getVehiclePrice(){
return lPrice;
}
}

And the text file



Vehicle information
(vin, make, model, year, color, mileage, price)


The objective of the program is to ask the user to enter the vin and then based on the vin, the program will display the vehicle information while reducing the inventory of that car model by 1. I need help with reading each line into the array and identifying each line by the vin.


(hint on how to reduce the inventory will be greatly appreciated as well)
Thank you .
Re: Please Help Out With Java Program by Mobinga: 9:23am On Dec 11, 2011
You need to be clear about your question. Reduce what inventory by 1? Anyhow if you can improve the Q, I would do this.

Anyhow see the StringTokenizer class http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
Re: Please Help Out With Java Program by segsalerty(m): 11:38am On Dec 11, 2011
lemme give u an idea of reading your file, and idea of hadling your user input
use
Scanner input = new Scanner(new File("name of the file.extension"wink);
int id = 1;//keeps track of your line id,  i think u wanted that in your prgram. right?
while(input.hasNextLine()){
//u need a condition to terminate this loop by telling user to type -1 at last line after they have entered the vehicles information finish
//so do this
String theLine = input.nextLine()
if(theLine == -1){
break;
}
//now u are good to go in reading your lines which has users data for each vehicle,  right?
//so say
String []parameters = theLine.split(", "wink;
//according to your Vehicle class constructor, u need the following ("try to add ---- int id", String aVin, String rentType, String amake, String amodel, int yr, String acolor,
//     double mile, double arentalPrice, boolean rentalS )
new Vehicle(id, parameters[0], parameters[1], parameter[2], parameter[3], ,  e.t.c, cast your int like Integer.parseInt(parameter[index]),  blah blah blah);
//u should know what to do with your vehicle object that u just initialize, maybe u have an array of it that u store each instance in,  as u wish
id++;
}//end of while loop

with this, i guess u shuld be able to read from file ,  sure, there are other approaches that works grin
Re: Please Help Out With Java Program by 2legit2qwt: 1:37pm On Dec 11, 2011
@segsalerty,
Thanks for that, I'll be trying that code in just a little bit and will let you know if it works for me.
@mobinga, sesalerty,
I'm reading all the vehicle info from the file. The user only has to enter the vin and then the program should be able to display that particular vehicle info. Where inventory comes in is that, on entering the vin, I'm trying to make it ask the user if they want to check out that car and if the user says yes, the inventory should have decrease by one car (the exact car that the vin was entered into the system by the user)
Thank you so nuch for your help.
Re: Please Help Out With Java Program by Chimanet(m): 4:59pm On Dec 11, 2011
U may try reading, initializing and putting all the vehicle objects into a list, u can remove any object u want from the list,then before the program terminates, u delete the previous file and write the remaining objects in the list into a new file with the same name and extension
Re: Please Help Out With Java Program by Mobinga: 6:55pm On Dec 11, 2011
package scjp;
import java.util.Scanner;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
public static void main(String argv[]){
String s = "1. 01X24, premium, Ford, Mustang, 2011, blue, 2356.03, 40.00, available \n2. 01Y36, premum, Chevrolet, Camero, 2011, yellow, 785.94, 40.00, available \n3. 01Z48, premium, Dodge, Challenger, 2011, black, 1382.28, 40.00, available \n4. 11V48, standard, Honda, Accord, 2010, black, 7383.38, 30.00, available \n5. 11V82, standard, Honda, Accord, 2011, silver, 2100.98, 30.00, available \n6. 01M55, standard, Dodge, Charger, 2010, white, 3966.11, 30.00, available \n7. 01B21, economy, Chevrolet, Aveo, 2010, red, 8291.56, 20.00, available \n8. 01B36, economy, Chevrolet, Aveo, 2010, red, 9382.41, 20.00, available \n9. 11B71, economy, Honda, Fit, 2011, blue, 4090.23, 20.00, available";
StringTokenizer st = new StringTokenizer(s, "\n");
HashMap <String, String> m = new HashMap<String, String>();
while (st.hasMoreElements()){
String v = st.nextToken();
StringTokenizer sts = new StringTokenizer(v, ",");
m.put(sts.nextToken().substring(3), v);
}
Scanner ben = new Scanner(System.in);
String userval = ben.nextLine().toString().trim();
if(m.get(userval) == null || m.get(userval).equals("null") ){
System.out.println("The car vin : " + userval + " does not exist in the inventory.");
} else {
System.out.println(m.get(userval));
m.remove(userval);
}
}
}
Re: Please Help Out With Java Program by Mobinga: 7:08pm On Dec 11, 2011
I used a String Tokenizer to get the separate values using the newline "\n" character as a delimiter, then I used another String Tokenizer to tokenize the tokens of the initial tokenizer using the comma "," as a delimiter, to get the number plus the vin, next to get the actual vin I did a substring of the initial supposed vin.

Next the vin was used as a key in a hashmap, while the entire details of the car was used as the value of that key.

As for[i] removing out of the inventory[/i], all I did was to remove the key the user specifies out of the hashmap.

You should get the logic from here onwards tongue
Re: Please Help Out With Java Program by 2legit2qwt: 9:17pm On Dec 11, 2011
Thanks for the help mobinga. I ran that program and it doesn't do anything. I wanted to check with you to see if there's anything else that needs to be done for it to run.
I'm sorry for asking that many questions, I'm just learning Java and a little confused.
I included the other classes I wrote maybe it has to do with them. I'm also gonna post my code that runs by manually inputting the data but like I initially posted, I'm needing help automating this process. Your code is in line with my reasoning, I just can't get it to run.
Thanks for taking the time out to help a brother.
Re: Please Help Out With Java Program by 2legit2qwt: 9:35pm On Dec 11, 2011
For some reason, i couldn't upload these files as a text file so i ended up pasting each one in MsWord just to attach it here and to show you what i've done so far.
Thanks
Re: Please Help Out With Java Program by Chimanet(m): 10:26pm On Dec 11, 2011
ehen wats the problem? u jst wrote 3 classes with their respective constructors, and 2 of the classes are tied one to one relationship with the whole(Invoice), so wat do u wanna do with the code ?
Re: Please Help Out With Java Program by 2legit2qwt: 12:14am On Dec 12, 2011
I'm trying to use the main method to pull the text file i pasted at the top. Mobinga helped me with the code to hard code the file but for some reason, i can't get that to run in the main  program. I added the other classes i wrote if that will help in troubleshooting the problem. Thanks
Re: Please Help Out With Java Program by Mobinga: 7:24am On Dec 12, 2011
2legit2qwt:

Thanks for the help mobinga. I ran that program and it doesn't do anything. I wanted to check with you to see if there's anything else that needs to be done for it to run.
I'm sorry for asking that many questions, I'm just learning Java and a little confused.
I included the other classes I wrote maybe it has to do with them. I'm also gonna post my code that runs by manually inputting the data but like I initially posted, I'm needing help automating this process. Your code is in line with my reasoning, I just can't get it to run.
Thanks for taking the time out to help a brother.


It waits for you to input a vin number. It does something.
Re: Please Help Out With Java Program by hunter121: 9:00am On Dec 12, 2011
Thanks for this post guys I was going through the same thing
Re: Please Help Out With Java Program by 2legit2qwt: 12:21pm On Dec 12, 2011
Mobinga:


It waits for you to input a vin number. It does something.
Will that be at runtime? Reason I'm asking is because when I run it, nothing happens, the user isn't prompted for anything. I'm sorry just a little confused. Thanks
Re: Please Help Out With Java Program by Mobinga: 9:10am On Dec 13, 2011
When you run the program input a vin and press enter! Damn.
Re: Please Help Out With Java Program by 2legit2qwt: 9:42am On Dec 13, 2011
I understand that but I'm saying program ain't running at all let alone prompt for anything. Nothing is coming up to type vin into.
Thanks
Re: Please Help Out With Java Program by Mobinga: 10:16am On Dec 13, 2011
2legit2qwt:

I understand that but I'm saying program ain't running at all let alone prompt for anything. Nothing is coming up to type vin into.
Thanks

angry angry

Dude, men its CLI, command line, not GUI. Compile and run, then type in anything!!!

Ok. Here is the program using "11V82" as the vin specified

http://ideone.com/czGqm
Re: Please Help Out With Java Program by 2legit2qwt: 2:07am On Dec 16, 2011
@Mobinga
I wanted to say thanks for helping out and being patient with me, i do appreciate your help and most especially your time.
@segsalerty

Thank you so much and i'm grateful that you took our time to help out as well.
Re: Please Help Out With Java Program by hunsu: 9:30pm On Dec 28, 2011
,Pls i just bought a dell
System and have installed jdk 1.7.0 and trying 2 get d directory through command prompt tells me that c,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

(1) (Reply)

Hardware Or Software For Postgraduate Program For A Computer Novice? / School Management Project / Mysql And SQL,,any Difference??

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 116
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.