简体   繁体   English

如何在Java中传递变量?

[英]How to pass a variable in java?

Consider this code: 考虑以下代码:

private static void colourRead(String s) throws IOException {
    FileReader readhandle = new FileReader("C:\\****\\****");
    BufferedReader br = new BufferedReader(readhandle);
    String line = null;
    while ((line = br.readLine()) != null) {
        ColourInput(); //there's an error here
    }

    br.close();
    readhandle.close();
}

private static void ColourInput(String s) {
    char letter;

    String fullWord;
    Scanner kb = new Scanner(System.in);

    System.out.print("Enter whatever: ");

    fullWord = kb.nextLine();
    System.out.println(fullWord);

    for (int i = 0; i < fullWord.length(); i++) {
        letter = fullWord.charAt(i);
        switch (Character.toUpperCase(letter)) {
        case 'A': {
            Blue();
        }
            break;
        }
    }
}

Is it possible for me to carry the 我可以携带

line

variable from the colourRead method, and somehow assign it to the colourRead方法中的变量,并以某种方式将其分配给

fullWord 

variable in the ColourInput() method? 变量在ColourInput()方法中?

I'm trying to read a text file, and output certain colours associated to each letter. 我正在尝试读取文本文件,并输出与每个字母相关的某些颜色。 I don't want to create a new switch statement in the colourRead method because apparently, this is a bad programming practice. 我不想在colourRead方法中创建新的switch语句,因为显然,这是一种不良的编程习惯。

Any help please? 有什么帮助吗?

If you're still unsure of what I'm asking I'll re-edit 如果您仍然不确定我要问什么,我会重新编辑

EDIT: The problem is that after calling the ColourInput(line) method, the Scanner method comes in to work (original code). 编辑:问题是,调用ColourInput(line)方法后,Scanner方法开始起作用(原始代码)。 I don't want to remove my Scanner method, I want it to 'skip' the scanner method, and continue into the for loop and switch statements. 我不想删除我的Scanner方法,而是希望它“跳过”扫描仪方法,并继续进入for循环和switch语句。

You're not passing the string to your call of ColourInput 您没有将字符串传递给ColourInput的调用

Try 尝试

ColourInput(line);

It is also worth mentioning that your code that reads the file is not safe, you should try to read the file, catch the IOException and close the file in a finally clause, if your code crashes somewhere in the while loop your file might remain open 还值得一提的是,读取文件的代码并不安全,如果代码在while循环中崩溃,则文件应保持打开状态,因此应尝试读取文件,捕获IOException并在finally子句中关闭文件。

If I understand correctly you want to be able to repeat the functionality of the ColourInput method with the results of the the ColourRead method. 如果我理解正确的话,你希望能够重复的功能ColourInput与该结果的方法ColourRead方法。

    private static void colourRead() throws IOException
{
    FileReader readhandle = new FileReader("C:\\****\\****");
    BufferedReader br = new BufferedReader(readhandle);
    String line = null;
    while((line = br.readLine()) != null)
    {
      ColourText(line); //there's an error here
    }

    br.close();
    readhandle.close();
}

private static void ColourInput() 
{

  String fullWord;
  Scanner kb = new Scanner(System.in);

  System.out.print("Enter whatever: ");

  fullWord = kb.nextLine();
  System.out.println(fullWord);
  ColourText(fullWord);
}

private static void ColourText(String text)
{

    char letter;
    for (int i = 0; i < text.length(); i++)
    {
      letter = text.charAt(i);
      switch(Character.toUpperCase(letter))
      {
          case 'A':
          {
             Blue();
          }
          break;
      }
}

This would let you color the text whether it is read from the file or input from the keyboard(using the ColourText method to change the color). 这将使您为文本着色,无论是从文件中读取文本还是从键盘输入文本(使用ColourText方法更改颜色)。 But as other people have mentioned you should add to the file reading code as well. 但是正如其他人提到的那样,您也应该添加文件读取代码。

Edit: You could also remove the String s variables from the first two methods since they are not being used in the methods anywhere. 编辑:您也可以从前两个方法中删除String s变量,因为它们没有在任何方法中使用。

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

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