By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Java Newbie needs help

P: 3
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
Share this Question
Share on Google+
4 Replies


DeMan
100+
P: 1,806
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

P: 3
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

P: 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
100+
P: 1,806
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

Post your reply

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