473,385 Members | 1,676 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,385 software developers and data experts.

Java Newbie needs help

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!!!
Dec 10 '06 #1
4 1194
DeMan
1,806 1GB
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.
Dec 10 '06 #2
Hmm, ok. I am going to play with the code some more and will post later if I get stuck. Thanks.
Dec 10 '06 #3
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;

Expand|Select|Wrap|Line Numbers
  1. public class groceryInv
  2.  
  3.   int itemNo;
  4.   String itemDesc;
  5.   double itemPr;
  6.   int itemInv;
  7.  
  8.   public groceryInv()
  9.  
  10.   {
  11.    setInv(itemNo, itemDesc, itemPr, itemInv);
  12.     }
  13.  
  14.   public groceryInv(int itemNo, String itemDesc, double itemPr, int itemInv)
  15.  
  16.   {
  17.    setInv(itemNo, itemDesc, itemPr, itemInv);
  18.     }
  19.  
  20.   public void setInv(int itemNo, String itemDesc, double itemPr, int itemInv)
  21.   {
  22.    itemNo = 0;
  23.     itemDesc = "";
  24.     itemPr = 0;
  25.     itemInv = 0;
  26.     }
  27.  
  28.     public int getitemNo()
  29.     {
  30.      return itemNo;
  31.      }
  32.  
  33.     public String getitemDesc()
  34.     {
  35.      return itemDesc;
  36.      }
  37.  
  38.      public double getitemPr()
  39.      {
  40.       return itemPr;
  41.       }
  42.  
  43.       public int getitemInv()
  44.       {
  45.        return itemInv;
  46.         }
  47.  
  48.     }
and here is my program that I have started;

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5.      //Name of the public class
  6.      public class InvProgram
  7.  
  8.     {
  9.     //The method main and file not found exception
  10.     public static void main(String[] args) throws
  11.                                            FileNotFoundException, IOException
  12.  
  13.  
  14.  
  15.           {
  16.          //Declare the variables
  17.          int index;
  18.          int itemCount;
  19.          int itemNo;
  20.          int itemStock;
  21.          int itemInv ;
  22.          int i = 0; 
  23.          double itemPr;
  24.          String itemDesc ;
  25.  
  26.          String itemSearch;
  27.          String inputString;
  28.  
  29.  
  30.          groceryInv gStock = new groceryInv();     
  31.  
  32.              //Create and Associate the stream objects            
  33.         Scanner inFile = new Scanner(new FileReader("groceryinv.txt"));
  34.  
  35.          while (inFile.hasNext() )
  36.               {
  37.                 itemNo = inFile.nextInt();
  38.                  itemDesc = inFile.next();
  39.                  itemPr = inFile.nextDouble();
  40.                  itemInv = inFile.nextInt();   
  41.             i++    }
  42.  
  43.           itemCount = i;
  44.                   }         
how do i tell the import to save a new instance in this class??? Please help. Thanks.
Dec 10 '06 #4
DeMan
1,806 1GB
I think (if I am interpreting you right) that you want to do something like this....

Expand|Select|Wrap|Line Numbers
  1. gStock.setInv(itemNo, itemDesc, itemPtr, itemInv);
  2.  
rather than calling
Expand|Select|Wrap|Line Numbers
  1. new groceryInv() 
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
Expand|Select|Wrap|Line Numbers
  1. gStock=new groceryInv(itemNo, itemDesc, itemPtr, itemInv);
  2.  
Dec 10 '06 #5

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

Similar topics

0
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...
6
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...
114
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
5
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...
1
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...
20
by: mayershome | last post by:
Hi! I'dont have any experiences in programming... what language should I start learning???? C? C++ or Java`? greetz
6
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...
29
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...
2
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
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...
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
jinu1996
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...

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.