5 Replies - 322 Views - Last Post: 03 April 2012 - 07:43 AM Rate Topic: -----

#1 kirubreezy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 12

Writing Objects to An Array and a Search Method - Won't Compile

Posted 03 April 2012 - 06:42 AM

Not sure why this won't compile....an error keeps popping up at the start of the error handling 'try'

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class AccountArrays
{
   public static void main(String [] args)
     {
         int newId; string newName;
         Customer newCust;
         System.out.println("Please enter filename");
         Scanner scan = new Scanner(System.in);
         String fileName = scan.next();
        }
        Customer [] custlist = Customer[10];
        try{
            
            Scanner filescan = new Scanner (fileName);
            while (filescan.hasNext())
            {
                newID = filescan.nextInt();
                newName = filescan.nextLine();
                newCust = new Customer(newID, newName);
                custlist [ndx] = newCust;
                ndx ++;
            }
        }
        catch(Exception e)
        {
        System.out.println("File Error")
        }
}
            
     public void searchcut (String searchname, Customer[]csutlist[])
     {
         int i=0
         boolean isfound = false;
         while(i<custlist.length())&&(!isfound))
         {
             if (searchname.equals(custliast[i].getname()))
             isfound = true;
             else
             i++
            if (isfound)
            System.out.println("Id is" + custlist[i].getID();
            else
            System.out.println("Not found");
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Writing Objects to An Array and a Search Method - Won't Compile

#2 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Writing Objects to An Array and a Search Method - Won't Compile

Posted 03 April 2012 - 06:56 AM

I can't follow the logic of this. Your main method gets a file name and then just exits the program.

Edit - take out the bracket above your array, and see if that works. Right now it think's you're trying to make a method called try.

This post has been edited by nunc: 03 April 2012 - 06:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Writing Objects to An Array and a Search Method - Won't Compile

Posted 03 April 2012 - 07:02 AM

On line 14 you have an extra curly brace.

Edit - ninja'd

This post has been edited by Ryano121: 03 April 2012 - 07:02 AM

Was This Post Helpful? 1
  • +
  • -

#4 kirubreezy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 12

Re: Writing Objects to An Array and a Search Method - Won't Compile

Posted 03 April 2012 - 07:23 AM

Thank You...it worked...

Can u say why the search method isn't working?

    public void searchcut (String searchname, Customer[]csutlist[])
     {
         int i=0
         boolean isfound = false;
         while(i<custlist.length()&&(!isfound))
         {
             if (searchname.equals(custliast[i].getname()))
             isfound = true;
             else
             i++
            if (isfound)
            System.out.println("Id is" + custlist[i].getID();
            else
            System.out.println("Not found");
        }
    }

Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Writing Objects to An Array and a Search Method - Won't Compile

Posted 03 April 2012 - 07:35 AM

This:

public void searchcut (String searchname, Customer[]csutlist[])

is not how you pass an array to a method. Change to:

public void searchcut (String searchname, Customer[] csutlist )
Was This Post Helpful? 1
  • +
  • -

#6 kirubreezy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 12

Re: Writing Objects to An Array and a Search Method - Won't Compile

Posted 03 April 2012 - 07:43 AM

Thank you....it worked...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1