Java,在其他地方使用数组中的字符串?

时间:2014-10-29 19:30:41

标签: java arrays object text

我是编码新手。我正在做一个项目,但我被卡住了。我的代码从文本文件中读取一个句子。它将句子分开并得到第二个单词并将其记录在一个数组中。我需要在另一个地方使用这个词,但我不能这样做。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Classroom {
    private String classname;

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public void classroomreader(String filename) {
        // This method gets the name for Classroom
        File text = new File("C:/classlists/" + filename + ".txt");
        Scanner scan;
        try {
            scan = new Scanner(text);
            String line = scan.nextLine();
            String classroomarray[] = line.split("\t");
            // ** right now classroomarray[1] has the word which I want.**

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }
}

这是我的主要课程:

public class ProjectMain {
    public static void main(String[] args) {
        // I created an array with 3 Classroom object inside it.
        Classroom[] classarray = new Classroom[3];

        //I hope I did this correct.

        // I used classroomreader method on my first object.
        classarray[0].classroomreader("class1"); 

        // Now I need to use the word in classroomarray[1].
        classarray[0].setClassname(????)
    }
}

我试过了:classarray[0].setClassname(classroomarray[1]);但是它出错了。如何设置第一个对象的名称?

5 个答案:

答案 0 :(得分:0)

我有点难以理解你的顶级代码在做什么......据说我相信如果你的顶级类中有一个私有变量然后有一个像

这样的访问方法
public String getPrivateFieldValue()
{
     return somePrivateFieldName;
}

然后,当您找到它的值时,您可以在主类中设置私有变量。在您的主要课程中,您可以说:

Object.getPrivateFieldValue() to get that value

或在你的情况下:

classarray[0].setClassname(classroomreader.getPrivateFieldValue())

答案 1 :(得分:0)

嗨,我建议你宣布

classroomarray[]

作为成员变量。然后生成getter和setter。

稍后您可以通过设置

来执行您想要的操作
classarray[0].setClassname(classarray[0].getClassroomarray[1]);

我正在努力帮助你完成你希望它完成的方式,但我不明白你为什么要这样做。

编辑:以下是我在下面的评论中讨论的代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Classroom {
 private String classname;

 public String getClassname() {
    return classname;
 }

 public void setClassname(String classname) {
    this.classname = classname;
 }

 public void classroomreader(String filename) {
    // This method gets the name for Classroom
    File text = new File("C:/classlists/" + filename + ".txt");
    Scanner scan;
    try {
        scan = new Scanner(text);
        String line = scan.nextLine();
        String classroomarray[] = line.split("\t");
        // ** right now classroomarray[1] has the word which I want.**
        this.classname = classroomarray[1];

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
  }
}

最后你的主要方法应该是

public class ProjectMain {
  public static void main(String[] args) {
    // I created an array with 3 Classroom object inside it.
    Classroom[] classarray = new Classroom[3];
    classarray[0]=new Classroom();
    //I hope I did this correct.

    // I used classroomreader method on my first object.
    classarray[0].classroomreader("class1"); 

    // Now classname is already set
    System.out.println(classarray[0].getClassname());
  }
}

答案 2 :(得分:0)

我认为你的问题在于范围的概念。无论何时创建变量(如classroomarray),java都会注册该变量的名称,然后表示您为其赋值的值(简单来说)。

现在Scope意味着并非所有变量都可以从每个地方访问。 Classroomarray是在classroomReader()中创建的,但是一旦完成就不会离开该函数。在某种程度上“生活”在该函数内部,这就是为什么你不能在main()方法中使用它。

如果要在main()方法中使用classroomarray,则需要通过某种方式将其传输到那里。有多种方法可以做到这一点:

  1. 在ClassRoom中创建一个字段,例如public String[] classroomarray
  2. 从classroomreader()函数返回您从文件中读取的教室数组。返回一个值意味着您首先将一个值“发回”给任何称为该函数的代码。例如,add(a,b)将返回a和b的总和。您可以通过更改:

    来完成此操作
    public void classroomreader(String filename)
    

    要:

    public String[] classroomreader(String filename)
    

    并改变:

    String classroomarray[] = line.split("\t");
    

    要:

    return line.split("\t");
    

    然后,在main()方法中,您可以使用:

    String[] classroomFile = classarray[0].classroomreader("class1");
    

    随意使用内容。

答案 3 :(得分:0)

这是一个例子,希望你不要介意硬解释。请将返回添加到您的方法中。

     public String[] demo()  //Added ()
     {
        String[] xs = new String[] {"a","b","c","d"}; //added []
        String[] ret = new String[4];
        ret[0]=xs[0];
        ret[1]=xs[1];
        ret[2]=xs[2];
        ret[3]=xs[3];
        return ret;
     }


 So your new code will be like this

 public String[] classroomreader(String filename) {
    // This method gets the name for Classroom
    File text = new File("C:/classlists/" + filename + ".txt");
    String[] classroomarray;
    Scanner scan;
    try {
        scan = new Scanner(text);
        String line = scan.nextLine();
        classroomarray = new String[] {line.split("\t")};//Change this
        // ** right now classroomarray[1] has the word which I want.**

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
  return classroomarray;
}

在Main方法中更改:

 String [] strArr = demo();//Calling method which returns value clasroomarray
 for ( int i = 0; i < strArr.length; i++) {
   System.out.println(strArr[3]);//Printing whatever index you want

//Set Values here like this
Classroom classroom=new Classroom();//Initialize your object class
classroom.set(strArr[3]); //Set value

答案 4 :(得分:0)

我在你的代码中只进行了一些更改。试试这个......绝对是这样的工作

    class Classroom {
private String classname;
String classroomarray[]=null;//declare  classroomarray[] here 

public String getClassname() {
    return classname;
}

public void setClassname(String classname) {
    this.classname = classname;
}

public void classroomreader(String filename) {

    File text = new File("C:/classlists/" + filename + ".txt");
    Scanner scan;
    try {
        scan = new Scanner(text);
        String line = scan.nextLine();
         classroomarray = line.split("\t");


    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
}

}

类ProjectMain {         public static void main(String [] args){

        Classroom[] classarray = new Classroom[3];


        //here you need to initialize all elements of the array
        classarray[0]=new Classroom();
        classarray[1]=new Classroom();
        classarray[2]=new Classroom();

        classarray[0].classroomreader("class1"); 


        classarray[0].setClassname(classarray[0].classroomarray[1]);

        System.out.println(classarray[0].getClassname());// here you'll surely get the the desired results
    }
}