Java Billboard 数组列表

标签 java arraylist

我已经结束了这个 Java 控制台。当我添加新文本时,我可以放置一个单词,但当我尝试添加一个句子时,我收到错误!如果有人能看到我错过的东西或问题,我将非常感激。它适用于一个单词,但不适用于两个或更多单词。

这是两个类

第一个类

 package Billboard;

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Paul
 */
import java.util.Scanner;

public class BillboardMain {
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    Billboard billboard = new Billboard();

    while (true) {
        System.out.println();
        System.out.println();
        System.out.println("\t Billboard Menu");
        System.out.println();
        System.out.println("Please select from the following billboard texts");

        for (int i = 0; i < billboard.getMessages().size(); i++) {
            System.out.println((i + 1) + ": "
                    + billboard.getMessages().get(i));
        }
        System.out.println((billboard.getMessages().size() + 1)
                            + ": Add new message.");
        System.out.println((billboard.getMessages().size() + 2)
                + ": Show current text.");
        System.out.println((billboard.getMessages().size() + 3) + ": Exit.");

        System.out.println();
        System.out.print("Choice: ");
        int code = console.nextInt();

        if (code == billboard.getMessages().size()+1) {
            System.out.print("Enter new text here: ");
            String newText = console.next();
            billboard.addNewText(newText);
            System.out.println("The new text message has been set to billboard");
        } else if (code == billboard.getMessages().size() + 2) {
            System.out.println("Current text is: " + billboard.getText());
        } else if (code == billboard.getMessages().size() + 3) {
            System.exit(0);
        } else {
            billboard.setText(code);
            System.out.println("The text message has been set to billboard.");
        }
    }
}
}


The second class

package Billboard;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Paul
 */
import java.util.ArrayList;
import java.util.List;

public class Billboard {
private String text;
private List<String> messages = new ArrayList<String>();

public Billboard() {
    super();
    messages.add("Neutral.");
    messages.add("Enemies.");
    messages.add("Friends.");
}

public List<String> getMessages() {
    return messages;
}

public boolean addNewText(String newText) {
    text = newText;
    return messages.add(newText);
}

public String getText() {
    return text;
}

public void setText(int index) {
    if (index <= messages.size())
        this.text = messages.get(index - 1);
    else
        throw new IndexOutOfBoundsException("Invalid message code.");
}

public String reverse() {
    if (isEmpty())
        return null;
    else {
        char[] chars = text.toCharArray();
        char[] reverse = new char[chars.length];

        for (int i = chars.length - 1, j = 0; i < 0; i--, j++) {
            reverse[j] = chars[i];
        }
        return new String(reverse);
    }
}

public String replace(char oldChar, char newChar) {
    return text.replace(oldChar, newChar);
}

public String substring(int begin, int end) {
    if (begin >= 0 && end < text.length()) {
        return text.substring(begin, end);
    } else
        return null;
}

public boolean isEmpty() {
    return text == null || text.isEmpty();
}
}

最佳答案

定义另一个扫描仪

扫描仪控制台1 = new Scanner(System.in);

并替换: String newText = console.next(); 其中:String newText = console1.nextLine();

应该可以

关于Java Billboard 数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173875/

相关文章:

java - 带有自签名证书的 Flutter https

java - 为什么我的光标在 ListView 中不起作用?

java - 为什么 CompareTo() 不起作用?

java - android - java - 带有 ArrayList 的 WeakReferences?

java - Jython 中隐藏的多线程瓶颈?

java - 在递归 Java 方法中保持计数

Java Socket 连接被我的公共(public) IP 拒绝

Java 从一个 ArrayList 生成两个 ArrayList

java - 从列表到整数。无法转换查询结果

java - 当位置很重要时,Array 与 ArrayList