开发者

Java declaration/ variable scope issues

开发者 https://www.devze.com 2023-02-18 08:38 出处:网络
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

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();
    }
}


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.


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).


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.


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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消