简体   繁体   English

从变量数组java中获取字符串的长度

[英]getting a length of a string from a variable array java

Im a student learning Java and this is part of my program and it is supposed to get the length of a string but the strings are all in an array. 我是一个学习Java的学生,这是我程序的一部分,应该获取字符串的长度,但是字符串都在数组中。 I try to run this in eclipse and it says i get an error where it says length = name[x].length() can someone let me know if there is a way to fix this 我尝试在eclipse中运行它,它说我得到一个错误,它说length = name[x].length()有人可以让我知道是否有解决方法

public class GuessName
{

    Random random = new Random();

    Scanner scan = new Scanner(System.in);
    String[] name = new String[10];
    int x,length;
    char guess1,guess2,guess3;

    public void names()
    {
        name[0] = "MARK";
        name[1] = "CHARLIE";
        name[2] = "MEG";
        name[3] = "KYLE";
        name[4] = "JUSTIN";
        name[5] = "KATARINA";
        name[6] = "JOEL";
        name[7] = "KEVIN";
        name[8] = "MICHAEL";
        name[9] = "JENNA";
        name[10] = "GREG";
    }

    public void start()
    {
        x = random.nextInt(10);
        length = name[x].length();
    }

You have an array, as follows: 您有一个数组,如下所示:

String[] name = new String[10];

The number between the [] represents the size of the array. []之间的数字表示数组的大小。 In your example, your array has a size of 10 meaning your array has 10 indexes which are [0,9] (because indexes start at 0). 在您的示例中,数组的大小为10,这意味着您的数组有10个索引为[0,9] (因为索引从0开始)。 The last line of your names() method is: names()方法的最后一行是:

name[10] = "GREG";

Do you know where I'm getting at? 你知道我要去哪里吗?

Also, what does your main method look like? 另外,您的main方法是什么样的? If you're receiving a NullPointerException it probably means you are calling start() before names() . 如果收到NullPointerException则可能意味着您是在names()之前调用start() names()

I commented out the parts that was problematic. 我注释掉了有问题的部分。 Also, you are trying to initialize 11 names as opposed to 10. Please note that arrays index starts at 0. I don't know why you have scanner object in there but you can use this block to complete your code. 另外,您正在尝试初始化11个名称,而不是10个。请注意,数组索引从0开始。我不知道为什么在那里有扫描仪对象,但是您可以使用此块来完成代码。

import java.util.Random; 
import java.util.Scanner;

  public class GuessName {



 //   Scanner scan = new Scanner(System.in);
    String[] name = new String[10];
    int x,length;
    char guess1,guess2,guess3;

    public GuessName()
    {
        name[0] = "MARK";
        name[1] = "CHARLIE";
        name[2] = "MEG";
        name[3] = "KYLE";
        name[4] = "JUSTIN";
        name[5] = "KATARINA";
        name[6] = "JOEL";
        name[7] = "KEVIN";
        name[8] = "MICHAEL";
        name[9] = "JENNA";
       // name[10] = "GREG";
    }

    public void start()
    {
        Random random = new Random();
        this.x = random.nextInt(10);
        this.length = name[this.x].length();
    }
    public static void main(String[] args) {
        GuessName gn = new GuessName();

        gn.start();

        System.out.println("The name is: "+gn.name[gn.x]+" and the length is: "+ gn.x);
    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM