0

I'm relatively new to java, and after much searching, I just can't pair up any solutions of related issues to mine. I'm trying to implement a very simple method to write to/ read from an array, and it's not being recognized by the compiler. "Keyboard" is a "variable not recognized" either. Here's a declaration of the array, with the method a bit further down that works on it... (first time long time btw :) Many thanks in advance...

private static void loadMakeModelYear()
import java.util.Scanner;

String [][] makeModelYear = {{"Make", "Model", "Year"},{"Blank", "Blank", "Blank"}};

private static void loadMakeModelYear()
{
    for (int i = 0; i < 3; i++)
    {
        System.out.println("Please enter a " + makeModelYear[i][0]);
        makeModelYear [i][1] = keyboard.nextLine();
    }
}
6
  • 2
    is your keyboard variable declared anywhere? Commented Mar 23, 2011 at 3:35
  • 2
    import statements should be at the beginning of the file. You cannot include "import java.util.Scanner" within the method Commented Mar 23, 2011 at 3:36
  • Please paste your entire class so we can review it properly. Thanks! Commented Mar 23, 2011 at 3:37
  • @Suresh it's not inside the method, he's declaring the method before he uses it, which no one does in Java. But yes, they should always be at the top. Commented Mar 23, 2011 at 3:41
  • I'm pretty sure this won't compile... this almost look like you have a C background (?) Commented Mar 23, 2011 at 3:44

5 Answers 5

1

This is just a guess, but your code appears to use keyboard with a lowercase k, while your error message uses Keyboard with a capital K. Check the case of your variables.

Sign up to request clarification or add additional context in comments.

4 Comments

I opted for brevity for the sake of all generous enough to help. I'll post the entire class next time. Keyboard variable is declared. Import statement is at the beginning of the file. Thanks again...
Yes folks, I haven't posted the entire class file. Would never expect it to compile as I've posted... I submitted here what I saw as the "offending" lines.
@Yanick: C#, which is what makes this so frustrating! lol I'm used to a more robust debug environment in VS2010.
@user I find the debug environment in Eclipse to be very robust, personally.
0

I juste rewrote your example as it may explain things better here.

import java.util.Scanner;

class SomeClass

    public static void main(String...args) {
        loadMakeModelyear();
    }

    static String[][] makeModelYear = new String[][] {
        {"Make", "Model", "Year"},
        {"Blank", "Blank", "Blank"}
    };

    private static void loadMakeModelYear() {
        Scanner keyboard = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("Please enter a " + makeModelYear[0][i]);
            makeModelYear [1][i] = keyboard.nextLine();
        }
    }
}

There are a lot more resources for Java than there is for C#. One site that is often very useful (to me at least) is Real's howto (check out the Java index).

2 Comments

Thanks man. Again, sorry I didn't post my whole class, but it looks exactly like yours, with some additional variables, and another method, etc. I've got what you've got there. Thanks for the offering tho, and I'm looking at that site now...
Wait, I was too quick on my last response... applying a couple of your changes now... thanks again...
0

What IDE are you using for this? NetBeans does a decent job of providing most VS2010 functionality.

I do not see keyboard declared. Do you declare it elsewhere?

"keyboard" is not a special object in Java giving you access to the real life keyboard, if that helps.

2 Comments

I'm not using an IDE per- se. Textpad. Data Structures class. :) I guess my struggle is all part of the idea. I've looked at NetBeans. That's what I was really referring to when I commented on robust environments in my limited experience...
I don't have much experience with Visual Studio, but as far as I can tell, in VS, I need to compile to see/clear errors. Whereas in Netbeans/Eclipse, this is done "live". And I'm not talking about the neet plugins for both Java IDEs to help debug/test/etc. (ie. code coverage).
0

My My My ..... Oh my dear, you're grossly confused in the way Java language operates. Lets look at your code more closely.

1.) Firstly, import statement should be the first statement in your file. The only statement which can come before import is the package statement.

  • but the glaring mistake which you're doing is by declaring methods like this. In java the scope of any method is bound to a class. This is not declarative style programming, where you can declare a stand-alone method. The same argument holds for your array as well, this array and method must be part of some class, even if they are static.

3.) Secondly, you are using a variable keyboard, but you have not declared it anywhere.

I hope you do realize that you're just using the wrong paradigm. Say this after me, "Java is purely OO "

Regards AViD

1 Comment

@ Vaid: Hi, yes, we discussed how I posted a very reduced version of my code, thanks for the feedback.
0

I think I see your problem. This is just a guess, and I'm not sure if you have already done this. In the case that you haven't you might want to set your reference variable keyboard to the Scanner class. This can be done by:

Scanner keyboard = new Scanner(System.in);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.