Ok, yes, I am very new to Java. I am having difficulty getting a program started. The scenerio is, I have a file that I need imported into a program. The file includes and item number, description and price. My difficulty is whether to load the file in as a parallel array, as a matrix, or line by line. The reason for my confusion is that I will need to do a sort and binary search. Can anyone offer suggestions on which method would probably be the most benificial? Thanks!!!
4 1194
Assuming it is a text file I would suggest the easiest way to deal with it is to read it line by line. You can then split each line into words and store each word in a structure (you could use a BinaryTree, which would cover your requirements for sorting and binary searching).
If part of the requirement is that you have to implement the methods for sorting and binary searching yourself, you may like to think of a different structure (you could implement one yourself), or use an ArrayList which is a linked list, but can fairly easily be manipulated for binary searches and sorts.
Hmm, ok. I am going to play with the code some more and will post later if I get stuck. Thanks.
Ok, i am stuck. The problem I am having now, is that I want to import a text file and store the values in the class. Here is my class; - public class groceryInv
-
-
{
-
int itemNo;
-
String itemDesc;
-
double itemPr;
-
int itemInv;
-
-
public groceryInv()
-
-
{
-
setInv(itemNo, itemDesc, itemPr, itemInv);
-
}
-
-
public groceryInv(int itemNo, String itemDesc, double itemPr, int itemInv)
-
-
{
-
setInv(itemNo, itemDesc, itemPr, itemInv);
-
}
-
-
public void setInv(int itemNo, String itemDesc, double itemPr, int itemInv)
-
{
-
itemNo = 0;
-
itemDesc = "";
-
itemPr = 0;
-
itemInv = 0;
-
}
-
-
public int getitemNo()
-
{
-
return itemNo;
-
}
-
-
public String getitemDesc()
-
{
-
return itemDesc;
-
}
-
-
public double getitemPr()
-
{
-
return itemPr;
-
}
-
-
public int getitemInv()
-
{
-
return itemInv;
-
}
-
-
}
and here is my program that I have started; - import java.io.*;
-
import java.util.*;
-
-
-
//Name of the public class
-
public class InvProgram
-
-
{
-
//The method main and file not found exception
-
public static void main(String[] args) throws
-
FileNotFoundException, IOException
-
-
-
-
{
-
//Declare the variables
-
int index;
-
int itemCount;
-
int itemNo;
-
int itemStock;
-
int itemInv ;
-
int i = 0;
-
double itemPr;
-
String itemDesc ;
-
-
String itemSearch;
-
String inputString;
-
-
-
groceryInv gStock = new groceryInv();
-
-
//Create and Associate the stream objects
-
Scanner inFile = new Scanner(new FileReader("groceryinv.txt"));
-
-
while (inFile.hasNext() )
-
{
-
itemNo = inFile.nextInt();
-
itemDesc = inFile.next();
-
itemPr = inFile.nextDouble();
-
itemInv = inFile.nextInt();
-
i++ }
-
-
itemCount = i;
-
}
how do i tell the import to save a new instance in this class??? Please help. Thanks.
I think (if I am interpreting you right) that you want to do something like this.... -
gStock.setInv(itemNo, itemDesc, itemPtr, itemInv);
-
rather than calling
before rading the values, you could also use the other form of constructor instead that is (having still declared 'groceryInv gStock;', read in all the values then call -
gStock=new groceryInv(itemNo, itemDesc, itemPtr, itemInv);
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Martin |
last post by:
Hi All,
I am a relative newbie to Java / Applets, but am despirately after
some help !
I have got the following code, which is basically a listing with
button items along the sides, allowing...
|
by: Alan Brown |
last post by:
As a newbie I would like to ask what the difference in application (not
details) is between C++ and Java. In what situations would you use C++ in
preference to Java and vice versa?
I am not...
|
by: Maurice LING |
last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic
typing, is there any other real reasons that Python is slower than Java?
maurice
|
by: Hassan Naqvi |
last post by:
Hi,
Basically, I am Java developer. In past I have played with Oracle
using Java (JDBC). But this is the time to play with IBM DB2 using
Java (JDBC). So kindly help this DB2 newbie.
I have a...
|
by: David Van D |
last post by:
Hi there,
A few weeks until I begin my journey towards a degree in Computer
Science at Canterbury University in New Zealand,
Anyway the course tutors are going to be teaching us JAVA wth bluej...
|
by: mayershome |
last post by:
Hi!
I'dont have any experiences in programming... what language should I
start learning????
C? C++ or Java`?
greetz
|
by: Rhino |
last post by:
I'm trying to debug a simple Java UDF written in the DB2General style within
Eclipse.
I'm getting a java.lang.UnsatisfiedLinkError when I execute the set() method
in the UDF. I know that the...
|
by: s0suk3 |
last post by:
Hello, I was hoping to get some opinions on a subject. I've been
programming Python for almost two years now. Recently I learned Perl,
but frankly I'm not very comfortable with it. Now I want to...
|
by: markthecheats |
last post by:
hi to all....
i need help...
Im so stuck creating my simple calc using java....
it would help me if i just have my own pc but its not...
ive been dealing with my machine problem to its...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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...
|
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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |