aspose file tools*
The moose likes Beginning Java and the fly likes More Jars needed - loop to create objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "More Jars needed - loop to create objects" Watch "More Jars needed - loop to create objects" New topic
Author

More Jars needed - loop to create objects

Peter Powers
Greenhorn

Joined: Jan 10, 2013
Posts: 13
Hello; would appreciate any help, advise and comments. The code below works fine - looking for a way to read the next line of the text file for the next object and so on. Thanks in advance !!!

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Hint: loop


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Peter Powers
Greenhorn

Joined: Jan 10, 2013
Posts: 13
Thanks I get the loop bit - where I am stuck is how do I create a new object i.e. : change the object name in the loop ? - Thanks for the advice on the tags will use them in future
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Ask yourself why you feel a need to "change the object name" in the loop. In fact, what do you even mean by that?
Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 86
Hello,

The algorithm for preforming that operation (specifically for reading lines of text)


String str=" ";//<----be sure to initialize the String var. with a " " so that it is not null.

while( str != null)//<---- loop terminates when theres is no line left
{

str = scannerObj.nextLine();//<---returns a string of in a line.

//code for what your doing with this line of text goes here
output.(str);


}

Nice use of class String methods by the way. Be careful as those will throw you off sometimes (especially at first).

Not sure what you meant by creating a new object IE giving it a new name. Not sure your looking at it correctly. I noticed you created three scanner objects NOTE!!! You are not giving it a new name... your creating a new instance of class scanner. You have three class Scanner objects. You are NOT referring to the same object in the three lines ...

Scanner dataIn = new Scanner (in);
Scanner scan = new Scanner (System.in)
Scanner getInt = new Scanner (toInt);

dataIn does not input the System.in object. It inputs the File object that you constructed into your program. Your not simplay giving it a new name. You have 3 instances from class Scanner instantiated in your program.

Not to be to nosy....not sure what your trying to do. Scanners are input objects They are used to input text sources such as text files and text from user input. Many people start of learning class Formatter for outputting text to various locations.

I do not know what class Jam does... so it is slightly ambiguous from this end.
Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 86
I would say use an array...but in order to use an array it helps to know ahead of time the number for initializing an array. This is possible by determining the length (in bytes ) of the file. This could be tricky. You would have to make each record had the same amount of bytes (char's are 2 bytes a peace) to determine the number of records. Then you could initialize the array.

Jar[] jars = new Jar[ totalBytesInFile / bytesPerRecord ];

Easier way...
java.util.ArrayList<Jar> jars = new java.util.ArrayList();

while( recordsStillLeftInFile )
{

jars.add( new Jar(constructNewJarObject ) );

}
Peter Powers
Greenhorn

Joined: Jan 10, 2013
Posts: 13
Joe, Thanks for the help. To explain; I have a text file each line has name of a jam, canning date. and amount of in jar. For each line I am trying to create an Jam object using the aforementioned for parameters. Its a exercise I found on the net where you required to create a Jam and pantry class with methods to print out the jams , use jam , test for empty etc. Ive got the classes working and tested. The issue is creating a new object for each line of the text file. The 1st run of the loop works fine thereafer no joy, thrown my entire Java arsenal at it ( not that is much ) of while loops. tried counting plus hasnext(). Ive posted my latest attempt below ..... for comic relief. Going to try your solutions now ( otter than the array one not progressed that far yet). Once again thanks.

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File in = new File("jams.txt");
Scanner dataIn = new Scanner (in);
Scanner scan = new Scanner (System.in);


int count = 0;

while ( dataIn.hasNext()) // continue if there are more tokens
{
count++;
String line = dataIn.nextLine(); // read in 1st line of text file
line = line.trim();
int split = line.indexOf(" ");
String typeJar1 = line.substring(0, split); // Extract Jam name
line = line.substring(split);
line = line.trim();
split = line.indexOf(" ");
String dateJar1 = line.substring(0, split); // Extract Jam date
String toInt = line.substring(split);
toInt.trim();
Scanner getInt = new Scanner (toInt);
int amountJar1 = getInt.nextInt(); // Extract amount



if ( count == 1)
{
Jam Jar1 = new Jam (typeJar1,dateJar1,amountJar1);
Jar1.print();
}

if ( count == 2)
{
Jam Jar2 = new Jam (typeJar1,dateJar1,amountJar1);
Jar2.print();
}

if ( count == 3)
{
Jam Jar3 = new Jam (typeJar1,dateJar1,amountJar1);
Jar3.print();
}


}
Peter Powers
Greenhorn

Joined: Jan 10, 2013
Posts: 13
Here is the working code ... thanks to Joe.....


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: More Jars needed - loop to create objects