Paul Skinner
Paul Skinner

Reputation: 43

Java Billboard Array list

I am at the end with this Java console. I can place one word when I add new text but when I try to add a sentence, I get errors! If someone can see something I missed or the problem I would appreciate it very much. It will work for one word just not two or more words.

This is two classes

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

Upvotes: 0

Views: 944

Answers (1)

JHDev
JHDev

Reputation: 995

define another Scanner

Scanner console1 = new Scanner(System.in);

and replace: String newText = console.next(); with: String newText = console1.nextLine();

it should work

Upvotes: 2

Related Questions