System.out.printf("\n"
+ "\nEnter a question: " + question=stdin.nextLine()
+ "\nEnter a guess: " + Guess=stdin.nextLine());
Produces error "The left-hand side of an assignment must be a variable".
Also,
do
{
...
} while(System.out.printf("\nWould you? "), stdin.nextLine().equalsIgnoreCase("y"))
produces "Syntax error on token ",", . expected"
I want printf to ignore the stuff that doesn't print anything. How to do so?
System.out.printf() does not allow assigning variables. So your attempt on the following code is wrong.
System.out.printf("\n"
+ "\nEnter a question: " + question=stdin.nextLine()
+ "\nEnter a guess: " + Guess=stdin.nextLine());
Try to change the above to multiple lines of code, after declaring all properly.
question=stdin.nextLine();
Guess=stdin.nextLine();
System.out.printf("\n"
+ "\nEnter a question: " + question
+ "\nEnter a guess: " + Guess);
The following do .. while() statement also have the same problem.
do
{
...
} while(System.out.printf("\nWould you? "), stdin.nextLine().equalsIgnoreCase("y"))
try the following
String answer = "n";
do
{
...
System.out.printf("\nWould you? ");
answer = <-- Get the user input using System.in -->;
} while (!answer.equalsIgnoreCase("y"))
you look like having syntax problem and following C++ way. Try to get hands on some java basics books and you are all set to go ..
Happy coding
Either question or Guess is not variable. Perhaps Guess is a class, not a variable.
The assignment is not automatically what gets assigned:
question = stdin.nextLine ();
assigns the result of stdin.nextLine () to question, but the result of the assignment is void, not question.
So your only solution is to work in 2 steps: do the assignment, and then, since nextLine has side effects, print the assigned values with println.
Related
I've made changes to the code and was able to successfully I hope use the switch case with a String. Now I just need help declaring two variables for the string getDirection. I need it to be declared as "Right" and "Left"
String quit= "exit";
String choice;
String getDirection= "Right";//I need it to be declared as both left and right
quit.equalsIgnoreCase("exit");
do
{
System.out.println("would you like the bug to move or turn around?");
System.out.println();
System.out.println("Or enter 'exit' to quit");
choice= scan.nextLine();
option options=option.MOVE;
switch(options)
{
case MOVE:
b.move();
if (getDirection.equals("Right"))
System.out.println("The bug moved 1 unit towards " + b.getDirection("Right ") + " and is now at position " + b.getPosition());
else if (getDirection.equals("Left"))
System.out.println("The bug moved 1 unit towards " + b.getDirection("Left ") + " and is now at position " + b.getPosition());
if (b.getDirection.equals("Right"))//this getDirection is showing up as an error
That's because there's no member variable called getDirection in class Bug.
You have defined a local variable called getDirection, which is unrelated to class Bug.
Probably you intended a method call, b.getDirection(), by analogy with your similar calls in the same section of code.
Incidentally, IMO variables should have noun phrases for names, not verb phrases. So, method getDirection() is well-named, variable getDirection is not.
i converted my java code into a python code and how to decrement the variable inside of the for loop in the python? I try to decrease the index by 1 if it is inside the if statement, but apparently I can't do that. Is there any other way that I can decrease i in a for loop?
Java Code:
for(int i = 1; i <= 3; i++)
{
System.out.print("Enter Movie " + i + " of " + 3 + " : ");
String inputMovie = sc.nextLine();
if (inputMovie.equals(""))
{
System.out.println("Please input a movie name.");
System.out.println("");
i--;
}
else
movies.offer("'"+inputMovie+"'");
}
Python Code:
for i in range(1,4):
inputMovie=input("Enter Movie " + str(i) + " of " + str(3) + " : ")
if inputMovie=="":
print("Please input a movie name")
print("")
i-=1
pass
else:
movies.append(inputMovie)
pass
Output: well if we look at the output it is still incrementing not decrementing the i
Enter Movie 1 of 3 :
Please input a movie name
Enter Movie 2 of 3 :
Please input a movie name
Enter Movie 3 of 3 :
Please input a movie name
Python doesn't let you alter the iterator in a for loop. As soon as the next iteration of the loop comes by, the iterator will be the next value of the iterable.
This is also because range doesn't behave like an actual Java-like for loop. Instead, it keeps generating numbers within the range (you can see this by typing list(range(10)) in a Python interpreter, it will make a list of numbers from 0 to 9.
If you want to modify the iterator, you should go old-school with a while loop instead:
i = 1
while i <= 3:
inputMovie=input("Enter Movie " + str(i) + " of " + str(3) + " : ")
if inputMovie=="":
print("Please input a movie name")
print("")
i-=1
else:
movies.append(inputMovie)
i = i + 1
This should do the same as your Java code, as I'm just moving the three instructions from the Java for loop to their places. Notice pass is not required as it is a statement with no effect.
For the sake of optimization, let me say you don't really need to decrement the iterator, just avoid incrementing it instead. I keep this solution separate from the original answer since it is a significant deviation from your original design:
i = 1
while i <= 3:
inputMovie=input("Enter Movie " + str(i) + " of " + str(3) + " : ")
if inputMovie=="":
print("Please input a movie name")
print("")
else:
movies.append(inputMovie)
i = i + 1
All I've done is remove the decrement and push the increment to the else block so it is only run if a movie name has been input.
The for loop in Python is more like for-each. So the loop value(i) will get updated to the next value regardless of the changes/updates in the loop.
A better way to do this would be to use a while loop.
i = 1
while i <= 3:
inputMovie = input("Enter Movie " + str(i) + " of " + str(3) + " : ")
if inputMovie=="":
print("Please input a movie name")
print("")
i-=1
pass
else:
movies.append(inputMovie)
i+=1
pass
you should use a while statement
"Unfortunately" the for loop will keep "memory" and reassign to the next value at each iteration
i = 1
while i < 4:
inputMovie = input("Enter Movie " + str(i) + " of " + str(3) + " : ")
if inputMovie == "":
print("Please input a movie name")
print("")
i-=1
else:
movies.append(inputMovie)
i+=1
the pass instruction is irrelevant, you can omit that
pass statement
range(low,high) generates a sequence consisting of elements starting from low and ending at high-1. That's why your i-=1 doesn't work, since I is iterating in that list.
The easiest alternative here would be to use a while loop.
while i<target:
if something:
#do something
i += 1
You have to set your range() function correctly. In order to decrement the loop you can use while loop or you can change your algorithm and set the for loop but now what you can do is if you can select the range functions step value to -1. Please try it to check the code coz i also have the same question in mind like you.
I am very new to coding, have only completed a few hours of YouTube videos to learn thus far. I am trying to complete a practice code and am facing some trouble.
I have attached a part of the code below. When I am entering value in (10,12,14, and 16) the code is still responding with "Wrong Response". In addition to this the following line is not properly functioning. It is not giving me the option to select a crust type. Please let me know if anyone has any suggestions.
Crust problem:
System.out.println("What type of crust would you like? ");
System.out.print("(H)and-tossed, (T)hin-crust, or (D)eep-dish: ");
crust = keyboard.nextLine();
Int Value problem:
if ( size.equals(" 10 ")) {
pizzaPrice = SM_Price;
} else if ( size.equals(" 12 ")) {
pizzaPrice = MED_Price;
} else if ( size.equals(" 14 ")) {
pizzaPrice = LG_Price;
} else if (size.equals(" 16 ")) {
pizzaPrice = XL_Price;
}
else { System.out.println("Wrong repsonse. ");
Thank you.
So, keep in mind that the constants 16, "16", " 16 " are all different things and non-equal to each other.
The other thing is that you need to show us what your types are. As Java is statically typed, that type information can actually help determine the behavior that you get.
if size is an int, then using size against string will not work. i.e size.equals(" 10 ") will not be the same as size.equals(10).
Also will not be the same as size.equals("10") which is different from " 10 "
You haven't defined size anywhere, so it's impossible to say.
I suspect your problem however is either that size is a different type that what you're comparing against using equals, or it's because you have spaces before and after your string numbers: " 12 ".
Please refer to User Input not working with keyboard.nextLine() and String (Java) for your nextLine() problem.
As to your other question one needs to know of what type you declared size to be.
If you declared it as String using String size = "10"; for example, then you are quite close:
Just change " 10 " in line 1 to "10" (without whitespace, as "10".equals(" 10 ") == false) and go for the small pizza.
Hey guys I have the following if condition...
//Add a space if necessary
if (i!=0 && spaceIn>0 && i%spaceIn==0) {
System.out.println("Converted Letter : " + curLtr + " at position " + i + " Needs a space");
curLtr = curLtr + " ";
};
The 1st 2 conditions are always true with the test input i give it.
Whats happening is the modula condition i%spaceIn==0 is not reporting as true when it is.
Example when i is 3 and spaceIn is 3 i%spaceIn=0 if condition works.
When i gets to 6 even tho i can see (from system.out further along) that the answer is 0 its not triggering the if condition.
Sometimes it wasn't doing it when i=12 either!
So weird.
Im printing out the answer to i%spaceIn throughout the loop and even tho the answer is 0 every multiple of 3 comes it sometimes wont trigger the if condition.
Same thing if spaceIn is 5. It skips 10. What ever number it is it seems to just skip sometimes for no reason.
What am i missing?
Use the following code...
You have terminated the if block... It can cause a problem...
if (i!=0 && spaceIn>0 && i%spaceIn==0) {
System.out.println("Converted Letter : " + curLtr + " at position " + i + " Needs a space");
curLtr = curLtr + " "; }
I hope it might work for you as it runs fine on my machine....
final Material b = event.getClickedBlock().getType();
p.sendMessage(ChatColor.GRAY + "This rock contains " + b.toString().toLowerCase().replaceAll("_", " ") + ".");
Alright, so lets say b is equal to CLAY and I want to print out "clay" in the message. What I've done here works. But lets say b is equal to LAPIS_ORE and I want to print out "copper" in the message. In this case it won't work because it would print out "lapis ore" instead of "copper". In the API that I am using, copper is not a Material type, so therefore I cannot declare a variable Material LAPIS_ORE = COPPER;. How else would I be able to print out "copper" from Material type LAPIS_ORE?
I tried:
String LAPIS_ORE = "copper";
p.sendMessage("This rock contains " + b.toString() + ".");
But that still didn't work out, probably because LAPIS_ORE is not a string, so how else would I do it? If needed, how could I declare a new Material type and set it equal to LAPIS_ORE?
Edit: I can do this with a switch statement but that is very inefficient.
Edit: Fixed it! Was simpler than I thought and kind of embarassing.
b.toString().toLowerCase().replaceAll("_", " ").replaceAll("GLOWING_REDSTONE_ORE", "copper ore").replaceAll("REDSTONE_ORE", "copper ore")
Not that way i am sure :) you can easy get it work ... :) see example one way to do it:
http://forums.bukkit.org/threads/solved-checking-sign-text.57905/
Enjoy it...