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 7488071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:43:45+00:00 2026-05-29T14:43:45+00:00

I’m a student starting out on my first programming module. The textbook I’m working

  • 0

I’m a student starting out on my first programming module. The textbook I’m working from is ‘Java for Everyone’, Cay Horstmann. I’m on chapter 2.4 – Constants. I have copied and checked (over 15 times!) the code directly from the book, however every time I compile it produces the compile error in is already defined in main(java.lang.String[]).

It highlights Scanner in = new Scanner(System.in); as being the problem. I have written another extremely simple program which also includes Scanner in = new Scanner(System.in); and I had exactly the same problem.

I have tried to find the solution, I’ve reinstalled both Java JDK and BlueJ (compiler), but nothing has worked. I have Googled the problem, but I’m unable to find anything which matches my problem. I am a Java virgin, so to be honest it’s pretty difficult to work out where I should be looking.

Any help would be greatly appreciated. I’m behind a couple of weeks already, and this is holding my progress up considerably.

Thanks, in advance, for any advice offered.

All the best,

Vicky

import java.util.Scanner;

public class Volume2 
{

    public static void main(String[] args)
    {

        final double BOTTLE_VOLUME = 2;  
        final double LITRES_PER_OUNCE = 0.0296; 
        final double CAN_VOLUME = 12 * LITRES_PER_OUNCE; 

        System.out.print("Please enter quantity of bottles: ");

        Scanner in = new Scanner(System.in);

        int bottles = in.nextInt();

        double totalVolume = totalVolume * BOTTLE_VOLUME;

        System.out.print("Please enter the number of cans: ");

        **Scanner in = new Scanner(System.in);** // This is highlighted as being the problem

        int cans = in.nextInt();

        double additionalVolume = cans * CAN_VOLUME;

        totalVolume = totalVolume + additionalVolume;

        System.out.print("The total volume is: ");
        System.out.println(totalVolume);

    }
}

Error Message: in is already defined in main(java.lang.String[])


  • 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-29T14:43:47+00:00Added an answer on May 29, 2026 at 2:43 pm
    Scanner in = new Scanner(System.in);   // FIRST ONE HERE
    
    int bottles = in.nextInt();
    double totalVolume = totalVolume * BOTTLE_VOLUME;
    System.out.print("Please enter the number of cans: ");
    
    Scanner in = new Scanner(System.in);   // SECOND ONE HERE
    

    Have a look at that code segment. You are indeed trying to create two copies of in, exactly as the error message suggests.

    In a normal situation where you wanted to reuse a variable, you would simply assign to it the second time, such as with:

    int xyzzy = 42;             // Create it here.
    doSomethingWith (xyzzy);
    xyzzy = 314159;             // NOTE: no "int" here, simple reuse.
    doSomethingWith (xyzzy);
    

    But, in your case, this does not apply, you’ve simply declared the variable twice.

    So, subscribing to the “create variables as late as you possibly can” philosophy, I would get rid of the first one, it’s not needed. You could get rid of the second but that afore-mentioned philosophy has served me well, ensuring that creation and use of objects are localised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to

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.