• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

More Jars needed - loop to create objects

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!!

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: loop
 
Peter Powers
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ask yourself why you feel a need to "change the object name" in the loop. In fact, what do you even mean by that?
 
Ranch Hand
Posts: 88
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the working code ... thanks to Joe.....


reply
    Bookmark Topic Watch Topic
  • New Topic