I am working with a fairly basic programming task in Java. We are asked to create a chat-robot, where the robot is to answer randomly from a set of given strings until the user writes "Bye!", where the robot will simply reply with "Bye!" and end the program. I've written the following code:
import java.util.Scanner;
import java.util.Random;
public class Robot {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random random = new Random();
String[] answer = new String[6];
answer[0] = "blabla1";
answer[1] = "blabla2";
answer[2] = "blabla3";
answer[3] = "blabla4";
answer[4] = "blabla5";
answer[5] = "blabla6";
boolean keepGoing = true;
System.out.println("Hello, how can I help you?");
while (keepGoing) {
String input = in.next();
int output = random.nextInt(6);
System.out.println(answer[output]);
if (input.equals("Bye!")){
keepGoing = false;
System.out.println("Bye!");
}
}
}
I have two issues with the program:
At times, seemingly at random, the program will answer with multiple strings.
When writing "Bye!", the program throws in another string before writing "Bye!". I do know that this may be resolved by adding "break;", but I'd consider this bad practice seeing as I am already using a boolean. (I'd like to keep using it.)
I have no idea as to why these errors occur.
Check the condition for exit before printing anything. That will resolve the 2nd issue
while (true) {
String input = in.next();
if (input.equals("Bye!")){
System.out.println("Bye!");
break;
}
int output = random.nextInt(6);
System.out.println(answer[output]);
}
Change your program like this
import java.util.Scanner;
import java.util.Random;
public class Robot {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random random = new Random();
String[] answer = new String[6];
answer[0] = "blabla1";
answer[1] = "blabla2";
answer[2] = "blabla3";
answer[3] = "blabla4";
answer[4] = "blabla5";
answer[5] = "blabla6";
boolean continue1 = true;
System.out.println("Hello, how can I help you?");
while (continue1) {
String input = in.next();
int output = random.nextInt(6);
if (input.equals("Bye!")){
continue1 = false;
System.out.println("Bye!");
}else
{
System.out.println(answer[output]);
}
}
}
}
Use below code snappet to solve Issue2:
while (keepGoing) {
String input = in.next();
int output = random.nextInt(6);
if(!input.equals("Bye!"))
System.out.println(answer[output]);
if (input.equals("Bye!")){
keepGoing = false;
System.out.println("Bye!");
}
}
Scanner in = new Scanner(System.in);
Random random = new Random();
String[] answer = new String[6];
answer[0] = "blabla1";
answer[1] = "blabla2";
answer[2] = "blabla3";
answer[3] = "blabla4";
answer[4] = "blabla5";
answer[5] = "blabla6";
boolean keepGoing = true;
System.out.println("Hello, how can I help you?");
while (keepGoing) {
String input = in.next();
if ("Bye!".equals(input)) {
keepGoing = false;
System.out.println("Bye!");
} else {
int output = random.nextInt(6);
System.out.println(answer[output]);
}
}
import java.util.Scanner;
import java.util.Random;
public class Robot
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random random = new Random();
String[] answer = new String[6];
answer[0] = "blabla1";
answer[1] = "blabla2";
answer[2] = "blabla3";
answer[3] = "blabla4";
answer[4] = "blabla5";
answer[5] = "blabla6";
boolean keepGoing = true;
System.out.println("Hello, how can I help you?");
while (keepGoing)
{
String input = in.next();
int output = random.nextInt(6);
System.out.println(answer[output]);
if (input.equals("Bye!"))
{
keepGoing = false;
System.out.println("Bye!");
} //This bracket is the only thing missing in your code.
}// End of while loop
} // End of main method
}// End of class
Related
I am trying to use Linear Search using java to find the name in the 2d array and print the details related to it, but the code is directly going to last loop without searching
the code is
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Source {
String customerDetails[][]=new String[5][3];
Source()
{
customerDetails[0][0]="1001";
customerDetails[0][1]="Raj";
customerDetails[0][2]="Chennai";
customerDetails[1][0]="1008";
customerDetails[1][1]="Akshay";
customerDetails[1][0]="Pune";
customerDetails[2][0]="1002";
customerDetails[2][1]="Simrath";
customerDetails[2][2]="Amristar";
customerDetails[3][0]="1204";
customerDetails[3][1]="Gaurav";
customerDetails[3][2]="Delhi";
customerDetails[4][0]="1005";
customerDetails[4][1]="Ganesh";
customerDetails[4][2]="Chennai";
}
public static void main(String args[] ) throws Exception {
Source nc = new Source();
Scanner sc = new Scanner(System.in);
String key = sc.nextLine();
boolean found = false;
for(int i=0;i<5;i++){
for(int j=0;j<3;j++){
if(nc.customerDetails[i][j].equals(key)){
found = true;
System.out.println(nc.customerDetails[i][0] + '\n' + nc.customerDetails[i][1] + '\n' + nc.customerDetails[i][2]);
break;
}
}
if(!found){
System.out.println("No Record Found");
break;
}
}
}
}
i want to find Gaurav in the array and print
1204
Gaurav
Delhi
public class test
{
String customerDetails[][] = new String[5][3];
test()
{
customerDetails[0][0] = "1001";
customerDetails[0][1] = "Raj";
customerDetails[0][2] = "Chennai";
customerDetails[1][0] = "1008";
customerDetails[1][1] = "Akshay";
customerDetails[1][2] = "Pune";
customerDetails[2][0] = "1002";
customerDetails[2][1] = "Simrath";
customerDetails[2][2] = "Amristar";
customerDetails[3][0] = "1204";
customerDetails[3][1] = "Gaurav";
customerDetails[3][2] = "Delhi";
customerDetails[4][0] = "1005";
customerDetails[4][1] = "Ganesh";
customerDetails[4][2] = "Chennai";
}
public static void main(String args[]) throws Exception
{
test nc = new test();
Scanner sc = new Scanner(System.in);
String key = sc.nextLine();
boolean found = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
if (nc.customerDetails[i][j].equals(key))
{
found = true;
System.out.println(nc.customerDetails[i][0] + '\n' + nc.customerDetails[i][1] + '\n' + nc.customerDetails[i][2]);
break;
}
}
}
if (!found)
{
System.out.println("No Record Found");
// break;
}
}
}
This should work. if(!found) should be outside the loop to check all records.
I have no idea how to properly write line of code adding class field to an ArrayList
public class Main {
public static void main(String[] args) throws IOException
{
zapisz("BazaDanych.txt");
Scanner scanner = new Scanner(System.in);
FilmExtended filmExtended = new FilmExtended();
ArrayList<FilmExtended> bazaFilmow = new ArrayList<>();
int i = 0;
while(scanner.nextInt()!= 0)
{
boolean check = true;
do
{
System.out.println("Podaj tytuł fimu: ");
String temp = scanner.nextLine();
if (temp.matches("[a-zA-Z]{2,}"));
{
bazaFilmow.add(i,filmExtended.setTytul(temp));
check = false;
}
}while (check);
}
}
You don't increment your index i. It's always 0. You have to put i++; in your do while loop
public class Main {
public static void main(String[] args) throws IOException
{
zapisz("BazaDanych.txt");
Scanner scanner = new Scanner(System.in);
List<FilmExtended> bazaFilmow = new ArrayList<>();
//remove index
while(scanner.nextInt() != 0)
{
boolean check = true;
do
{
System.out.println("Podaj tytuł fimu: ");
String temp = scanner.nextLine();
if (temp.matches("[a-zA-Z]{2,}"));
{
FilmExtended filmExtended = new FilmExtended(); //create new instance
filmExtended.setTytul(temp);
bazaFilmow.add(filmExtended); //use add without index or else need to increment your index
check = false;
}
} while (check);
} }
You have to add i++; in the do while loop
Two ways to fix it:
filmExtended.setTytul(temp) should return this.
i++ or remove i
You give your guess and programm compare the result with random word from the list. If the the programm could find equal symbouls, there will be shown, if not you will see "#"-symbol. Now, I can't understand why "#" didn't dysplayed. Here is a full code.
Example:
apple – random word
apricot - answer of customer
ap############# (15 syllables, because of customer don't have to know
the lenght of word)
import java.util.Random;
import java.util.Scanner;
public class Main {
private static Scanner scan = new Scanner(System.in);
public static final int N = 30;
public static void main(String[] args) {
String ranWord;
Random rand = new Random();
String[] words = new String[N];
words[0] = "appricot";
words[1] = "orange";
words[2] = "cucumber";
words[3] = "potato";
words[4] = "tomato";
words[5] = "cherry";
words[6] = "banana";
words[7] = "carrot";
words[8] = "were";
words[10] = "very";
words[11] = "tasty";
words[12] = "as";
words[13] = "usual";
words[14] = "and";
words[15] = "fresh";
words[16] = "and";
words[17] = "tasty";
words[18] = "passed";
words[19] = "for";
words[20] = "cooking";
words[21] = "a";
words[22] = "chicken";
words[23] = "it";
words[24] = "isn't";
words[25] = "necessary";
words[26] = "cook";
words[27] = "chicken";
words[28] = "every";
words[29] = "day";
System.out.println("Try to guess the word, call Your variant?" + "\n");
ranWord = words[rand.nextInt(N)];
System.out.println("Computer guess the word: " + ranWord);
Computer computer = new Computer(ranWord);
String customWord = scan.nextLine();
Customer customer = new Customer(customWord);
boolean finish = true;
while (!finish) {
//customWord = scan.nextLine();
if (customer.word.equals(computer.ranWord)) {
System.out.println("Succsesful prompt!");
finish = true;
} else {
checkIsFinish(customWord, ranWord);
finish = false;
}
}
}
static void checkIsFinish(String customWord, String ranWord) {
int minLenghtWord = customWord.length() < ranWord.length() ? customWord.length() : ranWord.length();
for (int i = 0; i < minLenghtWord; i++) {
if (customWord.charAt(i) == ranWord.charAt(i)) {
System.out.print(ranWord.charAt(i));
} else {
System.out.print("#");
}
}
for (int i = 0; i < 15 - minLenghtWord; i++) {
System.out.print("#");
}
System.out.println(customWord.length());
}
}
It is a silly mistake you made. You never enter while because finish = true at the start.
Do this,
finish = true;
while (finish) {
//customWord = scan.nextLine();
if (customer.word.equals(computer.ranWord)) {
System.out.println("Succsesful prompt!");
} else {
checkIsFinish(customWord, ranWord);
}
finish = false;
}
Or,
finish = false;
while (!finish) {
//customWord = scan.nextLine();
if (customWord.equals(ranWord)) {
System.out.println("Succsesful prompt!");
} else {
checkIsFinish(customWord, ranWord);
}
finish = true;
}
I'm working on a method that goes through all of my array messages and displays the selected message. I'm new to Java so I'm still trying to figure out methods and Arrays. When I run this, it says that it's successful but it doesn't display anything. Can someone help me figure this out.
package chatbox;
import java.util.Scanner;
public class ChatBox
{
public static void main(String[] args)
{
String chatMessages[] = new String[10];
//declare arrays
chatMessages[0]= "Pepperoni";
chatMessages[1]= "Olives";
chatMessages[2]= "Cheese";
chatMessages[3]= "Onions";
chatMessages[4]= "Bacon";
chatMessages[5]= "Tomato sauce";
chatMessages[6]= "Bell peppers";
chatMessages[7]= "Mushrooms";
chatMessages[8]= "Sausage";
chatMessages[9]= "Beef";
}
Scanner scan = new Scanner (System.in);
public Scanner chatCannedMessage(String chatMessages)
{
for (int i=0;i<chatMessages.length;i++){
System.out.println(chatMessages[i]); //Prints Message
}
System.out.println("Select a message");
String chatMessage = scan.next();
scan.nextLine();
return scan;
}
}
As PM 77-1 said, you never invoked your shoutOutCannedMessage method and also never print out or return any string.
I tried to minimize changes to your code, and I think this might be what you intended to do.
import java.util.Scanner;
public class ShoutBox {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
String messages[] = new String[10];
//declare 10 arrays
messages[0] = "Pepperoni";
messages[1] = "Olives";
messages[2] = "Cheese";
messages[3] = "Onions";
messages[4] = "Bacon";
messages[5] = "Tomato sauce";
messages[6] = "Bell peppers";
messages[7] = "Mushrooms";
messages[8] = "Sausage";
messages[9] = "Beef";
String m = new ShoutBox().shoutOutCannedMessage(messages);
System.out.println(m);
}
public String shoutOutCannedMessage(String[] messages) {
for (int i = 0; i < messages.length; i++) {
System.out.println(i+". "+messages[i]); //Should print the messages
}
System.out.println("Select a message");
int idx = scan.nextInt();
String message = messages[idx];
return message;
}
}
My code is supposed to reverse a domain. For example, if the input is www.google.com.uk, it should print uk.com.google.www
My problem is that my code prints uk.com..google.www
I can't figure where the extra . comes from or how to remove it. I did some tracing and found that www.stack.com.edu.uk would return uk.edu..com..stack.www
import java.util.*;
public class Domainreverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String domain = sc.nextLine();
int x;
int start = 0;
String reversed_domain = "";
boolean flag = false;
do{
x = domain.indexOf(".",start+1);
if(x==-1){
flag = true;
break;
}
reversed_domain = domain.substring(start,x+1) +reversed_domain;
start = x;
} while(flag==false);
int length = domain.length();
int z = domain.lastIndexOf(".");
String enddomain = domain.substring(z+1,length);
reversed_domain = enddomain+reversed_domain;
int p = reversed_domain.lastIndexOf(".");
reversed_domain = reversed_domain.substring(0,p);
System.out.print(reversed_domain);
}
}