So I wrote this program, the instructions are in the code, and it has the oddest bug that I can't figure out how to fix! When it runs and compiles, when it gets to the isSingle() and isWord() methods, it just prints out "Please enter a single word" and then skips to the next methods!
So I wrote the isSingle() method out as a separate program and ran it, and it works fine!?
Why won't it run correctly in my actual program?
Thanks!
Separated out:
And here it is in the whole program. Note: isWord() method is/does the same thing as isSingle()Code:
import java.util.Scanner;
public class Single
{
Scanner keyboard = new Scanner(System.in);
public void isSingle()
{
String single;
System.out.println("Please enter a single word");
single = keyboard.nextLine();
String[] tokens = single.split("\\s+");
boolean doSingle = true;
while (doSingle == true)
{
if (tokens.length == 1 )
{
System.out.println(single);
doSingle = false;
}
else
{
System.out.println("You must enter only one word.");
System.out.println("Please enter one word.");
single = keyboard.nextLine();
tokens = single.split("\\s+");
}
}
}
public static void main(String[] args)
{
Single single = new Single();
single.isSingle();
}
}
Code:import java.util.Scanner;
public class Echo
{
/**
* Main should:
* - Create a Scanner object to get keyboard input
* - Create an Echo object and pass the constructor the Scanner
* - Call the Echo's "begin" method
* Needs:
* - A field initialized in the constructor
* - A method that:
* - Ask the user for a line of text, echo it back
* - Ask the user for a line of text containing 3 words separated by spaces, echo it back
* with each word on its own line
* - Ask the user for an integer number, echo it back
* - Ask the user for a single word, echo it back
* - Ask the user for a real number (with a decimal point), echo it back
* - Ask the user for a single word, echo it back
*/
Scanner keyboard = new Scanner(System.in); // Create scanner object
public void isPhrase()
{
String phrase; // To hold the user's phrase
System.out.println("Hello, my name is Echo.");
System.out.println("Please enter a sentence longer than three words");
phrase = keyboard.nextLine(); // Saves user input as String phrase
String[] tokens = phrase.split(" ");
boolean doPhrase = true;
while(doPhrase == true)
{
if(tokens.length > 3)
{
for (String p : tokens)
System.out.print(p + " ");
doPhrase = false;
}
else
{
System.out.println("Must be a phrase sentence longer than three words");
System.out.println("Please enter a sentence longer than three words");
phrase = keyboard.nextLine();
tokens = phrase.split(" ");
}
}
}
public void isThree()
{
String three;
System.out.println("\nNow enter 3 words separated by spaces");
three = keyboard.nextLine(); // Saves user's input as String phrase
String[] tokens = three.split(" "); // Split token method prints string one
boolean doWhile = true;
while (doWhile == true)
{
if (tokens.length == 3)
{
for (String t : tokens) // line at a time
System.out.println(t);
doWhile = false;
}
else
{
System.out.println("Must be only 3 words");
System.out.println("Please enter 3 words");
three = keyboard.nextLine();
tokens = three.split(" ");
}
}
}
public void isNumber()
{
int number;
System.out.println("Please enter an integer number.");
boolean doThis = true;
while (doThis == true)
{
if (keyboard.hasNextInt())
{
number = keyboard.nextInt();
System.out.println(number);
doThis = false;
}
else
{
System.out.println("Not an int, try again"); // Echo user's number back if it is an int
System.out.println("Try again");
keyboard.nextLine();
}
}
}
public void isSingle()
{
String single;
System.out.println("Please enter a single word");
single = keyboard.nextLine();
String[] tokens = single.split("\\s+");
boolean doSingle = true;
while (doSingle == true)
{
if (tokens.length == 1 )
{
System.out.println(single);
doSingle = false;
}
else
{
System.out.println("You must enter only one word.");
System.out.println("Please enter one word.");
single = keyboard.nextLine();
tokens = single.split("\\s+");
}
}
}
public void isDecimal()
{
double decimal;
System.out.println("Please enter a real (decimal) number");
boolean doDouble = true;
while (doDouble == true)
{
if (keyboard.hasNextDouble())
{
decimal = keyboard.nextDouble();
System.out.println(decimal);
doDouble = false;
}
else
{
System.out.println("You must enter a decimal number.");
System.out.println("Please enter a real (decimal) number.");
keyboard.nextLine();
}
}
}
public void isWord()
{
String word;
System.out.println("Please enter a single word");
word = keyboard.nextLine(); // Saves user's word in String phrase
String[] tokens = word.split("\\s+");
boolean doWord = true;
while (doWord == true)
{
if (tokens.length == 1)
{
System.out.println(word);
doWord = false;
}
else
{
System.out.println("You must enter only one word.");
System.out.println("Please enter only one word.");
word = keyboard.nextLine();
tokens = word.split("\\s+");
}
}
}
public static void main(String[] args)
{
Echo echo = new Echo();
echo.isPhrase();
echo.isThree();
echo.isNumber();
echo.isSingle();
echo.isDecimal();
echo.isWord();
}
}

