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

  • Home
  • SEARCH
  • 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 7704467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:41:14+00:00 2026-05-31T23:41:14+00:00

(Using Java) I am implementing a generic class which is a B-Tree. When the

  • 0

(Using Java) I am implementing a generic class which is a B-Tree. When the user runs the program they can supply some arguments which will determine the type of the tree (Integer, Character, Double or String).

In my main method I have this code:

// Get user input and split it into tokens
// Tokens[1] = the type specified by the user

if( tokens[1].equals("DOUBLE"))
    BTree<Double> t = new BTree<Double>();

else if( tokens[1].equals("CHARACTER"))
    BTree<Character> t = new BTree<Character>();

else if( tokens[1].equals("INTEGER"))
    BTree<Integer> t = new BTree<Integer>();

else if( tokens[1].equals("STRING"))
    BTree<String> t = new BTree<String>();

But the compiler complains if I have the if statements. If I remove them then it compiles and runs fine :/ how can I fix this? So that the user can supply the type and the tree will be created depending on the type supplied? Thanks.

Here is some of the compiler output:

C:\Users\User\Desktop>javac *.java
Main.java:42: error: cannot find symbol
BTree<Double> t = new BTree<Double>();
symbol:   variable BTree
location: class Main

Main.java:42: error: cannot find symbol
BTree<Double> t = new BTree<Double>();
symbol:   variable Double
location: class Main

Main.java:42: error: cannot find symbol
BTree<Double> t = new BTree<Double>();
symbol:   variable t
location: class Main

.. There is more but it is similar and repeated for each of the types

  • 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-31T23:41:15+00:00Added an answer on May 31, 2026 at 11:41 pm

    The variable t in each of the if statements is only defined in the if‘s context, outside them the compiler won’t recognize them.
    Define your variable t before the if statements.

    BTree<?> t = null;
    if( tokens[1].equals("DOUBLE")) {
        t = new BTree<Double>();
    }
    else if( tokens[1].equals("CHARACTER")) {
        t = new BTree<Character>();
    }
    else if( tokens[1].equals("INTEGER")) {
        t = new BTree<Integer>();
    }
    else if( tokens[1].equals("STRING")) {
        t = new BTree<String>();
    }
    // now you can use your 't'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a little web application using Java and the Wicket framework. Each user
I'm implementing some code using the java.util.concurrency framework. I will be passing a collection
I'm implementing a long poll http connection using java servlet. How can I know
I am implementing an RSA encryption program using Java. Right now I am using
I'm currently implementing a .NET wrapper for a Java library by using JNI to
Using Java, how can I test that a URL is contactable, and returns a
By using java reflection, we can easily know if an object is an array.
I am using Java back end for creating an XML string which is passed
I am using Java 1.4 with Log4J. Some of my code involves serializing and
I'm using Java, and need to generate a simple, standards-compliant RSS feed. How can

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.