The Artima Developer Community
Sponsored Link

Java Answers Forum
Adding an object as an array

2 replies on 1 page. Most recent reply: Dec 13, 2004 10:06 PM by Gunasekhar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Christina Lee

Posts: 2
Nickname: christy
Registered: Dec, 2004

Adding an object as an array Posted: Dec 11, 2004 7:30 PM
Reply to this message Reply
Advertisement
I have no idea how to add the object into the array. The part with the //s on the left hand side are my errors. I have created another Book class. Apparently I get the errors:

javac wtf.java
wtf.java:13: cannot resolve symbol
symbol : variable iISBN
location: class wtf
Book book = new Book(iISBN, sTitle, iQuantity);
^
wtf.java:13: cannot resolve symbol
symbol : variable sTitle
location: class wtf
Book book = new Book(iISBN, sTitle, iQuantity);
^
wtf.java:13: cannot resolve symbol
symbol : variable iQuantity
location: class wtf
Book book = new Book(iISBN, sTitle, iQuantity);

  public static void main (String [] arg) throws Exception {
 
    final int iSize = 200;
    int i = 0;
    Book [] arrBook = new Book[iSize]; //array of books
//    Book book = new Book(iISBN, sTitle, iQuantity);
    String sPick = "";
    int iPick = 1000;
 
      while(iPick != 0) { //while selection is not zero
 
        printMenu ();
        sPick = br.readLine();
        sPick = sPick.trim();
 
          try {
 
           iPick = Integer.parseInt(sPick);
             //the selection menu
             switch(iPick) {
 
              case 0: System.out.println("Thank you. Come again.");
                      break;
              case 1: System.out.println("Adding a Book");
                      addBook();
                      break;
              default: System.out.println("Invalid selection. Try again.");
                       break;
    
            } //close switch
    
          }//close try
       
    
            catch(NumberFormatException nfe) {  //checking whether the person enters a number
              System.out.println("You did not enter a number, try again.");
            }//close catch
       }//close while
 
//addnig a book
  public static void addBook() throws Exception {
 
   //getting the ISBN number
    boolean bISBN = false;
 
    try {
      while(!bISBN) {
        System.out.print("Enter the ISBN number: ");
        String sISBN = br.readLine(); //string to read user input
        sISBN = sISBN.trim();
 
        int iOtherISBN = Integer.parseInt(sISBN); //parsing it to see if it's a number
        if(iOtherISBN >= 0 && iOtherISBN <= 199) { //if number is between 0 to 199
          bISBN = true;
//   	  for(int i =0; i <arrBook.length; i++) {
//            if(iOtherISBN == iISBN) {
//  	      bISBN = true;
//  	    }//close if
//	    else {
//	      System.out.println("ISBN Number already in use, try again.");
//	    }//close else
//          }//close for
        }//end if
        else {
          System.out.println("Number not between 0-199, try again (Type a letter to quit).");
        }//close else
      }//end while
    }//end try
 
    catch (NumberFormatException nfe) {
      System.out.println("It's not even a number, stupid, back to the menu.");
      bISBN = false;
    }//end catch
 
   //Getting the title
      System.out.print("Enter your Title: ");
      String sOtherTitle = br.readLine();
      sOtherTitle = sOtherTitle.trim();
 
      if(sOtherTitle.length() !=0) {
      }//end if
      else {
        System.out.println("Your title is blank; enter something next time! Go back to the menu");
      }//end else
 
   //getting the quantity of books
    System.out.print("Quantity of books: ");
    String sQty = br.readLine();
    sQty = sQty.trim();
    
    try {     
      int iOtherQuantity = Integer.parseInt(sQty);
      if (iOtherQuantity >=0) {
      }//end if
      else {
        System.out.println("Your number is negavtive, go back to menu.");
      }//end else
    }//end try
    catch (NumberFormatException nfe) {
      System.out.println("You did not enter a number, back to the menu you go.");
    }//end catch


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Adding an object as an array Posted: Dec 13, 2004 2:31 AM
Reply to this message Reply
The error messages you posted have nothing to do with an array.

wtf.java:13: cannot resolve symbol
symbol : variable iISBN 

This means that the compiler can't find a variable called iISBN.


For your array question:

int size = 5;
Book[] bookArray = new bookArray [size];
for (int i = 0; i < size; i++)
    bookArray[i] = new Book (...);


This is a very pürimitive example, but it shows, how to use arrays.
Another way to create an array:

Book b1 = new Book(...);
Book b2 = new Book(...);
Book b3 = new Book(...);
Book[] bookArray = new bookArray []{b1, b2, b3};

Gunasekhar

Posts: 7
Nickname: dguna
Registered: Nov, 2004

Re: Adding an object as an array Posted: Dec 13, 2004 10:06 PM
Reply to this message Reply
Hi
May be you can also try this code for adding & displaying:


import java.io.*;
import java.util.*;

public class BookStore {
static InputStreamReader isr = new InputStreamReader(System.in);
static BufferedReader br = new BufferedReader(isr);
static Vector arrayBook = new Vector();
public static void main (String [] arg) throws Exception {

final int iSize = 200;
int choice = 5;
//Book book = new Book (iISBN, sTitle, iQuantity);
//arrayBook.add(book);
do {
System.out.println("Select your menu option: ");
System.out.println("1. Add Book");
System.out.println("2. Remove Book");
System.out.println("3. Search Book");
System.out.println("4. Show All");
System.out.println("5. Exit");
System.out.println("Choice[1,2,3,4,5]: ");
choice = Integer.parseInt(br.readLine());
switch(choice) {
case 1:
addBook();
break;
case 2:
//removeBook();
break;
case 3:
//searchBook();

break;
case 4:
showBooks();
break;
case 5:
break;
default:
System.out.println("Invalid Entry!! Please try again ...");
}
}while(choice!=5);
}//close main

public static void addBook() throws Exception {
int iISBN = 0, iQuantity = 0;
String iTitle = "";
//getting the ISBN number
boolean bISBN = false;
while(!bISBN) {

System.out.print("Enter the ISBN number: ");
String sISBN = br.readLine();
sISBN = sISBN.trim();

try {
int iOtherISBN = Integer.parseInt(sISBN);
if(iOtherISBN >= 0 && iOtherISBN <= 199) {
iISBN = iOtherISBN;
bISBN = true;
}//end if
}//end try
catch (NumberFormatException nfe) {
System.out.println("Number not within 0-199, back to the menu.");
bISBN = false;
}//end catch
}//end while

//getting the Title
boolean bTitle = false;
while(!bTitle) {

System.out.print("Enter your Title: ");
String sOtherTitle = br.readLine();
sOtherTitle = sOtherTitle.trim();

if(sOtherTitle.length() !=0) {
iTitle = sOtherTitle;
bTitle = true;
}//end if
else {
System.out.println("Your title is blank, go back to the menu");
bTitle = false;
}//end else
}//end while

//getting the quantity
boolean bQty = false;
while(!bQty) {

System.out.print("Quantity of books: ");
String sQty = br.readLine();
sQty = sQty.trim();

try {
int iOtherQuantity = Integer.parseInt(sQty);
if (iOtherQuantity >=0) {
iQuantity = iOtherQuantity;
bQty = true;
}//end if
else {
System.out.println("Your number is negavtive, go back to menu.");
bQty = false;
}//end else
}//end try
catch (NumberFormatException nfe) {
System.out.println("You did not enter a number, back to the menu you go.");
bQty = false;
}//end catch
}//end while
//if all entries are available...
if(bISBN && bTitle & bQty) {
int flag = 1;
for(int i=0;i<arrayBook.size();i++) {
Book b = (Book)arrayBook.elementAt(i);
if(b.sTitle.equals(iTitle)) {
flag = 0;
break;
}
}
if(flag == 1) {
Book b = new Book(iISBN,iTitle,iQuantity);
arrayBook.add(b);
}
else {
System.out.println("Book exists in there, cannot add, try again.");
}
}

}//close addBook

public static void showBooks() {
for(int i=0;i < arrayBook.size(); i++) {
Book b = (Book)arrayBook.elementAt(i);
System.out.println("Details of Book" + (i+1) + "...");
System.out.println("iISBN: " + b.iISBN);
System.out.println("Title: " + b.sTitle);
System.out.println("Quantity: " + b.iQuantity);
}
}
}//close class

class Book {
String sTitle;
int iISBN;
int iQuantity;
Book(int iISBN, String sTitle, int iQuantity) {
this.iISBN = iISBN;
this.sTitle = sTitle;
this.iQuantity = iQuantity;
}
}

Flat View: This topic has 2 replies on 1 page
Topic: Java Capability Previous Topic   Next Topic Topic: Web applicaiton modeling

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use