Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:07:43+00:00 2026-05-30T19:07:43+00:00

I have 2 scanner objects, scan1 and scan2. scan1 is taking a line from

  • 0

I have 2 scanner objects, scan1 and scan2. scan1 is taking a line from a file and passing it to scan2 which reads the values of the lines individually. My question is this: how do I reset the line that the scan2 object is reading? I have multiple if statements and each one needs to reset the line that scan2 is using. here is the code of the scanning method:

public void start() throws MalformedURLException, IOException
{

/**scan1 takes each line of input as a string,
scan2 breaks up each string into variables and stores in objects with for loop*/

URL data = new URL("sample.txt");
URLConnection url = data.openConnection(); 
BufferedReader buffer = new BufferedReader(newInputStreamReader(url.getInputStream()));
    Scanner scan1 = new Scanner(buffer);
    scan1.nextLine();
    scan1.nextLine();

    for(int i=0;i<customerList.length;i++)
    {   
        String line1 = scan1.nextLine();
        Scanner scan2 = new Scanner(line1);
        scan2.useDelimiter("\t");

        //PersonalInfo
        number=scan2.nextInt();
        gender=scan2.next();
        givenName=scan2.next();
        middleInitial=scan2.next();
        surname=scan2.next();
        //MailingAddress
        streetAddress=scan2.next();
        city=scan2.next();
        state=scan2.next();
        zip=scan2.nextInt();
        emailAddress=scan2.next();
        telephoneNum=scan2.next();
        String nat=scan2.next();
        int natLength = nat.length();
        if(natLength != 11)
        {
            String line2 = scan1.nextLine();
            Scanner scan2 = new Scanner(line2);
            String nat2 = scan2.next();
            nationalId = nat + nat2;
        }
        else if(nat == null)
        {
            String line2 = scan1.nextLine();
            Scanner scan2 = new Scanner(line2);
            nationalId = scan2.next();
        }
        else
        {
            nationalId = nat;
        }

        String birth=scan2.next();

        if(birth==null)
        {
            String line3 = scan1.nextLine();
            Scanner scan2 = new Scanner(line3);
            birthday = scan2.next();
        }
        else
        {
            birthday = birth;
        }

        cctype=scan2.next();

        if(cctype==null)
        {
            String line4 = scan1.nextLine();
            Scanner scan2 = new Scanner(line4);
            String firstVal = scan2.next();
            String checkVal = String.valueOf(i-3);
            if(firstVal == checkVal)
            {
                //creates Customer object in customerList array
                address =new MailingAddress(streetAddress, city, state, zip);
                info = new PersonalInformation(givenName, middleInitial, 
                surname, gender, emailAddress, nationalId,telephoneNum, birthday);

                customerList[i]=new Customer(number, info, address);
            }
        }
        else
        {
            //CreditCard
            String line5 = scan1.nextLine();
            Scanner scan2 = new Scanner(line5);
            ccnumber=scan2.nextLong();
            cvv2=scan2.nextInt();
            ccExpiry=scan2.next();
            //MailingAddress
            ups=scan2.next();

            //creates PurchasingCustomer object in customerList array       
            address =new MailingAddress(streetAddress, city, state, zip);
            info = new PersonalInformation(givenName, middleInitial, surname, gender, 
                                            emailAddress, nationalId,telephoneNum, birthday);
            creditCard = new CreditCard(cctype, ccnumber, cvv2, ccExpiry);
            customerList[i]=new PurchasingCustomer(number, ups, address, info, creditCard );
        }
    }

as you can see, it’s currently set to create a new scan2 object each time it reads a new line, but i actually just want to change the string that scan2 is reading for each if statement. thank you in advance to anyone who can help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:07:44+00:00Added an answer on May 30, 2026 at 7:07 pm

    Why not create a new Scanner object each time? And in fact that is what you should do here. Some objects are made for easy re-use, but a Scanner is not one of them, but they’re cheap and easy to make, and so I advise you to continue doing just what you’re doing and make a new Scanner object when needed, but just be sure to close any Scanner objects once you’re done with them. Otherwise you risk running out of system resources. For instance, you’ll want to close the scan2 object at the bottom of the for loop:

    for(int i=0;i<customerList.length;i++)
    {   
        String line1 = scan1.nextLine();
        Scanner scan2 = new Scanner(line1);
        scan2.useDelimiter("\t");
    
        // ... a bunch of code deleted for brevity's sake
        // ... etc...
    
        scan2.close();
    } // end of for loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a service that monitors a file drop location for files from
// Create a scanner that reads from the input stream passed to us CSLexer
I have a barcode scanner (which acts like a keyboard) and of course I
I have written the following line of code that reads the data in a
I have written a program that reads in a File object (really an XML
I have a barcode scanner and laptop (ofcourse :)), I'm looking for simple event
I have an embedded application where an image scanner sends out a stream of
I have been given two different Microsoft Word document that my virus scanner has
I have been trying to write a bare-bones ping scanner using Perl for internal
If I have the following Java code: int[][] readAPuzzle() { Scanner input = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.