I am writing a program in java that will generate a set of random characters using numbers and letters, output them one by one, clearing the console after each character, append the character to a string, and ask the user to repeat the sequence.
My problem here is that if the program says, 'a' and asks for input, even if 'a' is entered, it returns incorrect. Here is the code of the generating and testing of the strings:
public void generateSeq() {
try {
Random rand = new Random();
for (int i = 0; i < numChars; i++) {
Robot bot = new Robot();
c = characters.charAt(rand.nextInt(characters.length()));
System.out.print(c);
Thread.sleep(1000);
bot.keyPress(KeyEvent.VK_CONTROL);
bot.keyPress(KeyEvent.VK_L);
bot.keyRelease(KeyEvent.VK_CONTROL);
bot.keyRelease(KeyEvent.VK_L);
full = full + String.valueOf(c);
}
} catch (InterruptedException e) {
System.out.print("Error 1. Email me # xxx#gmail.com.");
} catch (AWTException e) {
System.out.print("Error 2. Email me # xxx#gmail.com.");
}
testSeq();
}
And here is the testing method:
public void testSeq() {
Scanner sc = new Scanner(System.in);
System.out.print("Your attempt: ");
user = sc.nextLine();
if (user == null ? full == null : user.equals(full)) {
System.out.println("Correct! Trying next combo....");
numChars++;
generateSeq();
} else {
System.out.println("Incorrect! Restarting game...");
start();
}
}
In the beginning, when full is null, you attempt to add the first character to it. But this is String Conversion, which converts a null to the String "null", and your full variable now starts with "null".
Initialize it to the empty string ("") first, at the top of generateSeq.
There is nothing wrong with your use of the ternary operator, but now the strings won't be null; they would be empty at worst. Calling equals by itself is now sufficient.
if (user.equals(full))
In addition, you may want to generate your Random object once, as an instance variable, instead of creating a new Random object every time you call generateSeq.
The code appears to be fine and it works.
Do you print what contains user and full when you compare it?
if (user == null ? full == null : user.equals(full)) {
System.out.println("Correct! Trying next combo....");
numChars++;
generateSeq();
} else {
System.out.println("--user:" + user);
System.out.println("--full:" + full);
System.out.println("Incorrect! Restarting game...");
}
Related
Write a program that asks a user to input a string. Then asks a user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value (a negative value or a integer larger than the size of the string) an exception will be thrown. Add the code to catch this exception, output a warning message and then continue with the loop
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
System.out.println("");
String s;
int ind;
Scanner sc=new Scanner(System.in);
while(sc.hasNext())
{
s=sc.next();
if(s.length()==0)
break;
ind=sc.nextInt();
try {
char ch=s.charAt(ind);
System.out.println("Character is "+ch);
}
catch(Exception e) {
System.out.println("Bad index Error!");
}
}
}
}
Yes. You could rely on assignment evaluating to the assigned value. Also, call Scanner.hasNextInt() before calling Scanner.nextInt(). Like,
System.out.println();
String s;
Scanner sc = new Scanner(System.in);
while (sc.hasNext() && !(s = sc.next()).isEmpty()) {
if (sc.hasNextInt()) {
int ind = sc.nextInt();
try {
char ch = s.charAt(ind);
System.out.println("Character is " + ch);
} catch (Exception e) {
System.out.println("Bad index Error!");
}
}
}
There is a bug; sc.next() cannot return an empty string in this code. Try editing it this way:
while(sc.hasNext()) {
s = sc.next();
if(s.length() == 0) {
System.out.println("Woah, Nelly!");
break;
}
// ...
}
See if you can get the program to print "Woah, Nelly!" by entering a blank line, or anything else. I can't, and assuming I understand the documentation correctly, it is impossible for the if condition to ever be true here (emphasis mine):
Depending upon the type of delimiting pattern, empty tokens may be returned. For example, the pattern "\\s+" will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern "\s" could return empty tokens since it only passes one space at a time.
This pattern "\\s+" is the default one, and you haven't set a different one, so your scanner should never return an empty token. So the strict answer to "is there a way to write this program without the break statement?" is: yes, you can just delete the if(...) break; code and it doesn't change the behaviour in any way.
However, that's not really a solution to your problem because it doesn't give the user a way to exit the program. You should use nextLine() instead of next() to allow reading a blank line from the user.
The homework instructs to create a loop requesting for an String input. If the String has less than 20 characters, it displays what was just inputted. If if has more than 20 characters, the catch block will display a message stating that the String has too many characters. The only way to end the program is to input DONE. Otherwise, it continues to ask the user for Strings.
The catch block will display a message from another class.
I've attempted to do both do-while and while loops.
do
{
System.out.println("Enter strings, enter DONE when finished:");
userInputLength = input.nextLine();
try {
while(userInputLength.length() > 20)
{
System.out.println("Please try again:");
userInputLength = input.nextLine();
}
}
catch(StringTooLongException e) //Error here
{
//Not sure how to call the super() in StringTooLongException class.
}
while(userInputLength.length() <= 20)
{
String message = userInputLength;
System.out.println("You entered: " + message);
userInputLength = input.nextLine();
}
}
while(userInputLength.toString() == "DONE");
}
}
public StringTooLongException()
{
super("String has too many characters!");
}
Before I started getting the error on my catch block after adding both try-catch blocks, I was able to output long strings, then short strings. But if I attempt to write long strings after the short strings, the program ends.
it's gonna work. Look at my code and compare with yours.
First: don't compare strings with ==, always choose the equals method.
You do not need 3 whiles block, you need only one while and 2 IF'S one for string > 20 and another for string < 20 (look, if the string contains exactly lenght of 20, the program will output nothing)
And you need to create your own exception, it is very easy.
import java.util.Scanner;
public class ReadString {
public static void main(String[] args) {
String userInputLength;
Scanner input = new Scanner(System.in);
/*
* . If the String has less than 20 characters, it displays what was just
* inputted. If if has more than 20 characters, the catch block will display a
* message stating that the String has many characters. The only way to end the
* program is to input DONE. Otherwise, it continues to ask the user for
* Strings.
*/
do {
System.out.println("Enter strings, enter DONE when finished:");
userInputLength = input.nextLine();
try {
if (userInputLength.length() > 20) {
throw new StringTooLongException("String is too long");
} else {
System.out.println(userInputLength);
}
} catch (StringTooLongException e) // Error here
{
System.out.println(e.getMessage());
}
} while (!userInputLength.toString().equals("DONE"));
}
Exception class
public class StringTooLongException extends RuntimeException{
public StringTooLongException(String message) {
super(message);
}
}
Try to understand it :D !!!
Hi I wanted to know how to write up a try and catch block to stop from getting the below error.
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
I have this method which takes a sentence and splits it into an ArrayList. I then use that to store values into a hashmap, where index 1 is the key and the words after become the value. I use the below method to split the user input into an array.
private Scanner reader;
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
public ArrayList<String> getInput()
{
System.out.print("> "); // print prompt
String inputLine = reader.nextLine().trim().toLowerCase();
String[] wordArray = inputLine.split(" "); // split at spaces
// add words from array into ArrayList
ArrayList<String> words = new ArrayList<String>();
for(String word : wordArray) {
words.add(word);
}
return words;
}
}
and the below method uses the class above to detect user input. So when the user types in write they can write into a hashmap but if they press return before they type in a key and value I get the out of bounds exception. So How can i rewrite the below method to avoid this?
public void start()
{
boolean finished = false;
printWelcome();
while(!finished) {
ArrayList<String> input = reader.getInput();
if(input.contains("shutdown")) {
finished = true;
}
if (input.contains("load")) {
System.out.println();
instruct.readAndFill();
System.out.println();
}
if (input.contains("write")) {
String key = input.get(1);
String value = "";
for(int i=2; i<input.size(); i++) {
value = value + " " + input.get(i);
}
instruct.mapWrite(key, value);
}
}
instructorGoodBye();
}
Sorry if i wasn't clear enough, or if my code is not up to scratch i have only been learning java for about 2 months now.
basically if the user types in write key value on one line it is fine but if they hit return after write then the error happens.
So, fundamentally what you are missing is error checking. Your program is taking input from a user, and assuming it is valid. This is always a bad idea.
Instead, you should validate what you get from the user. One way you can do this, for your "write" block, is to make sure the elements you expect to be there, are actually there.
To start, I would rewrite your loop as follows:
while(!finished) {
List<String> input = reader.getInput();
if(input.size() == 0) {
throw new IllegalArgumentException("Must specify command, one of 'shutdown', 'load', 'write'");
}
final String command = input.remove(0).toLowerCase();
// TODO: Make sure command is one of the valid commands!
Note the changes:
Assigning to List instead of ArrayList is just a good general practice.
Checking the input to make sure it has more than zero elements
Taking the first element, since we don't want to have to do List.contains(). Consider the input garbage garbage garbage write, clearly we don't want this to invoke the "write" command, it should be considered invalid input.
Finally, we use this to rewrite the conditions on executing our commands:
if(command.equals("write")) {
// Make sure the user put the right stuff in here
// Since we removed the command from the input already, just make sure what is left is
if(input.size() <= 1) {
throw new IllegalArgumentException("Must specify correct data");
}
String key = input.remove(0);
String value = String.join(" ", input); // Java 8
instruct.mapWrite(key, value);
}
You are getting the error for below part of the code..
if (input.contains("write")) {
String key = input.get(1);// here is the problem..
String value = "";
for(int i=2; i<input.size(); i++) {
value = value + " " + input.get(i);
}
instruct.mapWrite(key, value);
}
in the line 2 of this code snippet. you are accessing a value by using the index. Now just imagine you just enter a single word in the console. so the arraylist you will get from the getInput() method will have the size of 1. So.. in the arraylist the word will be placed on 0th position.(that is the first position) but you are accessing the value on second position.. Thats gives you a index out of bond exception..
basically the fix was simpler than throwing a new exception and using a try and catch block. All I had to do was slightly change the logic and just use and if else statement.
if (input.contains("write")) {
if(input.size() >=2) {
String key = input.get(1);
String value = "";
for(int i=2; i<input.size(); i++) {
value = value + " " + input.get(i);
}
mapWrite(key, value);
} else {
System.out.println("Please type in the key & value after write all on line");
}
}
From what I have learned from java so far, is that the best solutions are normally always normally the simplest. Thanks for all the help, everyone who commented and tried to help me basically helped me come up with the idea.
... if instead of a number I get a letter, or a symbol, or 2 decimals.
I am making a change maker program in java.
Everything works good, the only thing i am confused about is checking the string to see if is invalid for my use,
I did this for when is left empty;
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "Invalid input! ");
System.exit(0);
}
That works perfect, now how can I do the else to check for letters or dots or symbols, anything that is not a number?
You could use regular expressions.
Here's some sample code to check for digits only (\\d) in your input string.
The code that actually checks is pattern.matcher(in).matches() and it tries to match the regular expression defined by regex
Let me know if you need more explanations
public class HelloWorld{
public static void main(String[] args) {
String regex = "\\d+";
String inputNumber = "2";
String inputDecimal = "2.0";
String inputString = "two";
String[] inputs = {inputDecimal, inputNumber, inputString };
Pattern pattern = Pattern.compile(regex);
for(String in: inputs){
System.out.print( in + " ");
System.out.print( pattern.matcher(in).matches()? "":"does not");
System.out.print( " contain integer numbers" );
System.out.println("---");
}
}
}
If you need to perform all the processing only when the String is integer why not check for integer value in the if clause and let the else clause be common for all the letter, dots, symbols and also empty.
if(s1.isNum){
//all processing here
}
else{
JOptionPane.showMessageDialog(null,"Invalid Input");
System.out.exit(0);
}
Otherwise you could also use try and catch block.
try{
int num= Integer.parseInt(s1);
//rest of the processing
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Invalid Input");
System.out.exit(0);
}
Use either according to your requirement
You could use a regular expression1 and String.matches(String) which Tells whether or not this string matches the given regular expression. \\d+ should match one or more digits. Something like
System.out.println("12".matches("\\d+"));
Prints
true
1Some people, when confronted with a problem, think
“I know, I'll use regular expressions.” Now they have two problems. --jwz
To test whether it is an integer, parse it to an int like this:
Integer.parseInt(s1)
You might also want to make use of the value returned but I don't show it here. Now you can apply try catch blocks around the method call and catch NumberFormatException like this:
try {
Integer.parseInt(s1);
//The code here will run if s1 is an integer.
} catch (NumberFormatException e) {
//this will be executed when s1 is not an integer
}
You can also extract a method from the above code. A method that returns true when the exception is not thrown. However, a disadvantage of try catch is that throwing an exception needs time and thus, it slows down your program.
To test whether the string is a letter, you loop through all the chars in the string and use one of the methods of the Character class.
boolean isLetter = true;
for (int i = 0 ; i < s1.length() ; i++) {
if (!Character.isLetter(s1.charAt(i))) {
isLetter = false;
break;
}
}
If isLetter is true, it is a letter. Again, you can also extract this as a method.
To check whether it is a symbol, use one of the methods of the Character class (again).
boolean isSymb = true;
for (int i = 0 ; i < s1.length() ; i++) {
if (!Character.isJavaIdentifierStart(s1.charAt(i))) {
isSymb = false;
break;
}
}
To check for dots in a string, just use
s1.contains(".")
Isn't that simple?
Ok, I solved the problem the following way... I took a little bit of every idea lol...
if (s1 == null) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
for (int i = 0; i < s1.length(); i = i + 1) {
if (!Character.isDigit(s1.charAt(i))) {
JOptionPane.showMessageDialog(null, "You must enter an integer value");
System.exit(0);
}
}
Language: Java.
Aim:
Boolean Array gridA[] should become true on whatever index is read from input (i.e. if input is "init_start 2 4 5 init_end" then gridA[] indexes 2,4 and 5 should become true). That much I managed to get working but I have two problems:
input:
init_start int int int int int (...) int init_end
for example: init_start 2 6 12 init_end
Problems:
any integer from input that exceeds the value of (instance variable) int L (which determines the index-length of the array) should be ignored, to prevent integers from outside the domain of Array gridA[] from having influence.
Using if(scanner.nextInt != L){} didn't seem to work.
I also need this method, or the body of the method to start when input begins with "init_start" and stop when input ends with "init_end".
How do write code so that it can read both String and integers from the same input?
I meant to do this using
if(scanner.Next=="init_start") followed by
a = scanner.NextInt; which, as I suspected, didn't work.
Attempts at solving:
After googling I tried putting String initialInputStart in a Scanner:
localScanner(initialInputStart);
but I failed to get that working. Other information I found suggested I'd close and reopen the scanner but I need the information to be read from a single line of input so I doubt that will help.
code:
java.util.Arrays.fill(gridA,false);
java.util.Arrays.fill(gridB,false);
String initialInput;
String initialInputStart;
int a;
int i;//only for testing
i = 0;//only for testing
System.out.println("type integers"); //only for testing
while( scanner.hasNextInt() && i<5){ //I can't find a way to make loop stop without missing input so I'm using i temporarily
a = scanner.nextInt();
gridA[a] = true;
System.out.print(a);
System.out.print(gridA[a]+" ");
i++;
}//end while
I wrote a little program which pretty much does what you described as your aim; I read line by line and split each into tokens I further process. The tokens describe what the data means/what state we are in. The actual data is parsed in the default: case in the switch(token) block and branches in behaviour from state to state (which is merely visible here as we only have two states: "init" and "not init", beside the keywords):
public static void main(String[] args) {
int L = 13; // not sure if this is needed
boolean[] gridA = new boolean[L];
Reader source;
/**
* from file:
* source = new FileReader("grid.csv");
*/
/**
* from classpath resource:
* source = new InputStreamReader(MyClass.class.getResourceAsStream("grid.csv"));
*/
/**
* from string:
* source = new StringReader("init_start 2 6 12 init_end");
*/
/**
* from std-in:
* source = new InputStreamReader(System.in);
*/
try(BufferedReader stream = new BufferedReader(source)) {
boolean init = false;
// loop
input_loop:
while(true) {
// read next line
String line = stream.readLine();
if(line == null) {
// end of stream reached
break;
}
if(line.trim().isEmpty()) {
// ignore empty lines
continue;
}
String[] tokens = line.split(" ");
for (String token : tokens) {
switch (token) {
// evaluate keywords
case "init_start":
init = true;
break;
case "init_end":
init = false;
break;
// for input from console
case "exit":
break input_loop;
default:
// parse input, based on state (expand "init" to an enum for more states)
if(init) {
// read init input
int index = Integer.parseInt(token);
if(index >= 0 && index < gridA.length) {
gridA[index] = true;
} else {
throw new RuntimeException("illegal grid index: " + index);
}
} else {
// read undefined input
throw new RuntimeException("unrecognized token: " + token);
}
break;
}
}
}
} catch(IOException ex) {
throw new RuntimeException("an i/o exception has occurred", ex);
}
System.out.println(Arrays.toString(gridA));
}
" How do write code so that it can read both String and integers from the same input?"
do you want to have an Input like this: "123, foo"
if thats the case use:
String input = scanner.nextLine();
String[] parts = input.split(",");//" " to split it at an empty space
String part1 = parts[0]; // 123
int Number = Integer.parseInt(part1) // you could inline it, but i chose this version for better refference
String part2 = parts[1]; //foo
if your Input looks like this "123 or foo"
you have to read the input as String and check the String afterwards if its a Number:
String input = scanner.nextLine();
if (text.contains("[a-zA-Z]+") == false){ //looks if the input does NOT contain any characters
int nummber = Integer.parseInt(input);
} else{
String text = input;
}
afterward you can compare your text:
For the first mentioned case:
if("init_start".equals(parts[1])){ //*
yourMethod();
}
For the other case:
if("init_start".equals(text)){ //*
yourMethod();
}
*Also:
"I meant to do this using if(scanner.Next=="init_start")"
*Very important! To compare Objects, such as String use .equals(). "==" only works on primitive types
Edit: I've read your example. You could go with a combination of my solutions. split the string at space(" ") and check parts[x] if it is an integer. But i wouldnt recommend this method! Why dont you split your input in three parts: init_start would start your function. After that your method would expect an input of Integers like "int int int" after you inserted the Integers your function could automatically stop or wait for the input "init_stop". That seems to me more reasonable. If you want to go with the single line solution you can evaluate the number of your int's by get tingparts[].lenght()-2
use this implementation:
public static void main(String args[]){
try{
Scanner in = new Scanner(System.in);
System.out.println("Enter a line");
String dat = in.readLine();
System.out.println(dat);
}
catch(IOException e){
System.out.println("IO ERROR !!!");
System.exit(-1);
}
}