Illegal Start of Type alphabet - java

yes, i have looked at other code, but i have a unique situation, and here it is: okay so my high school teacher is making us do a project to where we have to use IF and ELSE declarations to find out our initials just from YES and NO inputs (0 = no , and 1 = yes) and it has to work with every letter he chooses, but on line 45 it says illegal start of type, but the only thing there is else... anyways here's the code and thank you for the help in advance
/* Objective: practice completing if, if-else,block statements.
* and relational operators.
*/
import java.io.*;
import java.util.*;
public class Alphabet2 {
public static void main(String args[]) {
final int YES = 1;
final int NO = 0;
int answer = 0;
Scanner kbReader = new Scanner(System.in);
System.out.println("Think of a letter from A to Z\n\n");
System.out.println("0 = A-M");
System.out.println("1 = N-Z");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == NO){
System.out.println("\nOK, A thru M\n");
System.out.println("0 = A-G");
System.out.println("1 = H-M");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == YES){
System.out.print("\nOK, H thru M \n");
System.out.print("\n0 = H-J \n");
System.out.print("\n1 = K-M \n");
System.out.print("Enter your choice \t");
answer = kbReader.nextInt();
if(answer == YES){
System.out.print("lol");
}
else {
}
}
//else
// System.out.print("");
}
else {
System.out.println("OK, A thru G\n");
}
}
else {
System.out.println("\nOK, N thru Z\n");
System.out.println("0 = N-S");
System.out.println("1 = T-Z");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == NO){
System.out.println("OK, N thru S\n");
}
else {
System.out.println("OK, T thru Z\n");
}
}
}

At the bottom is your code properly indented.
Note that
else {
System.out.println("\nOK, N thru Z\n");
appears after the brace that closes your main method.
The reason you get that particular error message is that the parser thinks the else that appears after the main method is the type for another method or field declaration, because it is not a keyword modifier that could be part of a member declaration.
One way to think about this problem is to break it down completely before filing things in:
// A-Z
if (...) {
// A-M
} else {
// N-Z
}
then one layer more
// A-Z
if (...) {
// A-M
if (...) {
// A-F
} else {
// G-M
}
} else {
// N-Z
if (...) {
// N-S
} else {
// T-Z
}
}
etc.
/* Objective: practice completing if, if-else,block statements.
* and relational operators.
*/
import java.io.*;
import java.util.*;
public class Alphabet2 {
public static void main(String args[]) {
final int YES = 1;
final int NO = 0;
int answer = 0;
Scanner kbReader = new Scanner(System.in);
System.out.println("Think of a letter from A to Z\n\n");
System.out.println("0 = A-M");
System.out.println("1 = N-Z");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == NO){
System.out.println("\nOK, A thru M\n");
System.out.println("0 = A-G");
System.out.println("1 = H-M");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == YES){
System.out.print("\nOK, H thru M \n");
System.out.print("\n0 = H-J \n");
System.out.print("\n1 = K-M \n");
System.out.print("Enter your choice \t");
answer = kbReader.nextInt();
if(answer == YES){
System.out.print("lol");
}
else {
}
}
//else
// System.out.print("");
}
else {
System.out.println("OK, A thru G\n");
}
}
else {
System.out.println("\nOK, N thru Z\n");
System.out.println("0 = N-S");
System.out.println("1 = T-Z");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == NO){
System.out.println("OK, N thru S\n");
}
else {
System.out.println("OK, T thru Z\n");
}
}
}

/* Objective: practice completing if, if-else,block statements.
* and relational operators.
*/
import java.io.*;
import java.util.*;
public class Alphabet2 {
public static void main(String args[]) {
final int YES = 1;
final int NO = 0;
int answer = 0;
Scanner kbReader = new Scanner(System.in);
System.out.println("Think of a letter from A to Z\n\n");
System.out.println("0 = A-M");
System.out.println("1 = N-Z");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == NO){
System.out.println("\nOK, A thru M\n");
System.out.println("0 = A-G");
System.out.println("1 = H-M");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == YES){
System.out.print("\nOK, H thru M \n");
System.out.print("\n0 = H-J \n");
System.out.print("\n1 = K-M \n");
System.out.print("Enter your choice \t");
answer = kbReader.nextInt();
if(answer == YES){
System.out.print("lol");
}
else{
System.out.print("");
}
}
else {
System.out.println("OK, A thru G\n");
}
}
else {
System.out.println("\nOK, N thru Z\n");
System.out.println("0 = N-S");
System.out.println("1 = T-Z");
System.out.print("Enter your choice\t");
answer = kbReader.nextInt();
if (answer == NO){
System.out.println("OK, N thru S\n");
}
else {
System.out.println("OK, T thru Z\n");
}
}
}
}

Related

I would like put a optional "wrong number. Try again" when is

I have just started learn java codes, that is why might I have a simple question that is not simple for me.
I would like put a optional "wrong number. Try again" when is entered different number than secretNum. May you guys help me out on this code?
// I need learn how put "try again" when the number is != than guess number.
/* I have tried
* 1)Change the signal "==" or "!=".
* 2) do {
System.out.println("Guess what is the number 0 to 10: ");
if (sc.hasNextInt()) {
guess = sc.nextInt();
}
} while(secretNum != guess);{
System.out.println("Well done");
System.out.println();
System.out.println("Are you ready for the next step?");
System.out.println();
}
*/
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name:");
if(sc.hasNextLine()) {
String userName = sc.nextLine();
System.out.println("Hello " + userName + ",");
System.out.println();
}
int secretNum = 5;
int secretNum2 = 15;
int guess = 0;
do {
System.out.println("Guess what is the number 0 to 10: ");
if (sc.hasNextInt()) {
guess = sc.nextInt();
}
} while(secretNum != guess);{
System.out.println("Well done\n");
System.out.println("Are you ready for the next step?\n");
}
// I need learn how put "try again" when the number is != than guess number.
/* I have tried
* 1)Change the signal "==" or "!=".
* 2) do {
System.out.println("Guess what is the number 0 to 10: ");
if (sc.hasNextInt()) {
guess = sc.nextInt();
}
} while(secretNum != guess);{
System.out.println("Well done");
System.out.println();
System.out.println("Are you ready for the next step?");
System.out.println();
}
*/
System.out.println("Enter Yes or No");
while(!sc.next().equals("yes")&& !sc.next().equals("no"));{
System.out.print("Yes");
}
do {
System.out.println("Guess what is the number 11 to 20: ");
if (sc.hasNextInt()) {
guess = sc.nextInt ();
}
}while(secretNum2 != guess);{
System.out.println("Congratulations");
System.out.println();
System.out.println("The End");
}
}
}
````````
You don't need do{} while() for the checks you want to do here, just while(){} loops would be enough.
Please try this code instead:
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name:");
if (sc.hasNextLine()) {
String userName = sc.nextLine();
System.out.println("Hello " + userName + ",");
System.out.println();
}
int secretNum = 5;
int secretNum2 = 15;
int guess = 0;
System.out.println("Guess what is the number 0 to 10: ");
if (sc.hasNextInt()) {
guess = sc.nextInt();
}
while (secretNum != guess) {
System.out.println("Please try again\n");
if (sc.hasNextInt()) {
guess = sc.nextInt();
}
}
System.out.println("Well done\n");
System.out.println("Are you ready for the next step?\n");
System.out.println("Enter Yes or No");
while (!sc.next().equals("yes") && !sc.next().equals("no"))
{
System.out.print("Yes");
}
System.out.println("Guess what is the number 11 to 20: ");
if (sc.hasNextInt()) {
guess = sc.nextInt();
}
while (secretNum2 != guess) {
System.out.println("Please try again\n");
if (sc.hasNextInt()) {
guess = sc.nextInt();
}
}
System.out.println("Congratulations");
System.out.println();
System.out.println("The End");
}
}

Verifying a String for Numbers and Periods/Decimals

I have this short snippet of code where I have to check a string to see if it contains integers and possibly a decimal. The string is an amount of money (12.34) so as well it can not go past the fourth index.
My question is I'm being told to use charAt() (and in my code I used matches() and contains() which is wrong) to check for integers and decimals so that this routine will return a boolean that is true if the string works with those parameters, however I'm confused at how to go about converting this to use charAt() instead of matches() and contains().
As well, I'm sorry if I formatted this wrong, or worded something wrong, or the code looks awful, I'm in my first semester of Java and it's my very first programming class I've ever taken so I'm a bit rough.
import java.util.Scanner;
public class Auction
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String price;
String quantity;
System.out.print("How much money are you willing to bet on this item?: $");
price = keyboard.next();
if(price.matches("[0-9]*") && price.length() <= 5)
{
Float f = Float.parseFloat(price);
System.out.printf("$%5.2f", f);
System.out.println();
}
else if(price.matches("[0-9]*") && price.length() <= 5 && price.contains("."))
{
Float f = Float.parseFloat(price);
System.out.printf("$%5.2f", f);
System.out.println();
}
else
{
System.out.println("Invalid input");
}
System.out.print("What quantity do you want to bid on?: ");
quantity = keyboard.next();
if(quantity.contains("."))
{
System.out.println("Invalid input");
}
}
}
I am typing this from a phone. So excuse the mistakes please. have u been asked by your professor to use charAt instead of regex and matches?
if (inpString!= null && !inpString.isEmpty () && inpString.length() <= 5){
int periodCount = 0;
for (int i=0; i < inpString.length (); i++){
char c = inpString.charAt (i);
if (c == '.'){
periodCount++;
}else if (c >= '0' && c <= '9'){
}else {
System.out.println("invalid output");
break;
}
if(periodCount > 1){
System.out.println("too may periods. Invalid output");
break;
}
}
}else {
System.out.println ("invalid input");
}
Can you comment if u need to check that there are no thousandth digit i.e 1.234? If yes make sure
inpString.substring
(inpString.lastIndexOf
(".")).length < 3
with all the null and indexOutOfBounds checks
How about this way?
import java.util.Scanner;
public class Auction {
private static final String numberRegex = "^\\d*(\\.\\d+)?$";
private static final String integerNumber = "^\\d*$";
private static final int MAX_LENGTH = 5;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String price;
String quantity;
System.out.print("How much money are you willing to bet on this item?: $");
price = keyboard.next();
if (price.length() <= MAX_LENGTH && price.matches(numberRegex)) {
Float f = Float.parseFloat(price);
System.out.printf("$%5.2f\n", f);
} else {
System.out.println("Invalid input");
return;
}
System.out.print("What quantity do you want to bid on?: ");
quantity = keyboard.next();
if (!quantity.matches(integerNumber)) {
System.out.println("Invalid input");
}
}
}

Why is a value not incrementing properly?

I am working on a Java project and am encountering a strange error. I have a menu with multiple options all inside a do while loop but I want the variable pizzasOrdered to keep increasing when the user adds more pizzas to his order. Here is my code:
import java.util.Scanner;
public class PizzaMenu {
public static void main(String[] args) {
int numPlain;
int numPepperoni;
int pizzasOrdered = 0;
boolean flag = true;
System.out.println("Welcome to Pies, Pies, and pis!");
Scanner kbd = new Scanner(System.in);
System.out.println("Is there a customer in line? (1 = yes, 2 = no)");
int isCustomer = kbd.nextInt();
System.out.println("Are you a Pie Card member? (1 = yes, 2 = no)");
boolean isMember;
int pieCardResponse = kbd.nextInt();
if(pieCardResponse == 1)
{
isMember = true;
}
else
{
isMember = false;
}
do{
System.out.println("Please choose an option: \n\t1) Update Pizza Order\n\t2) Update Cherry Pie Order\n\t3) Update Charm Order\n\t4) Check Out");
int option = kbd.nextInt();
if(option == 1)
{
System.out.println("Here is your current order: \n\t" + pizzasOrdered + " pizzas ordered");
System.out.println("How many plain pizzas would you like for $10.00 each?");
numPlain = kbd.nextInt();
System.out.println("How many pepperoni pizzas would you like for $12.00 each?");
numPepperoni = kbd.nextInt();
pizzasOrdered = numPlain + numPepperoni;
}
else if(option == 2)
{
System.out.println("Here is your current order: \n");
}
else if(option == 3)
{
}
else if(option == 4)
{
}
}while(flag);
}
}
You should change this
pizzasOrdered = numPlain + numPepperoni;
to
pizzasOrdered += (numPlain + numPepperoni);
In your case you are not incrementing the variable, you are just assigning it new values in every iteration.

Guessing Game, with a While loop

i'm currently trying to create a while loop for my program, a Guessing game. I've set it up so the user can create a max value i.e 1-500 and then the user can proceed to guess the number. When the number has been guessed, the User can press 1, to close, anything else to continue running the loop again.
My problem, is that the code gives me an error when trying to continue the loop, no compiling errros
This is my Code:
import java.util.Random;
import java.util.Scanner;
public class Gættespil2
{
public static void main(String[] args)
{
Random rand = new Random();
int TAL = rand.nextInt(20) + 1;
int FORSØG = 0;
Scanner input = new Scanner (System.in);
int guess;
int loft;
boolean win = false;
boolean keepPlaying = true;
while ( keepPlaying )
{
Scanner tastatur = new Scanner(System.in);
System.out.print("Indsæt loftets højeste værdi : ");
loft = tastatur.nextInt();
TAL = (int) (Math.random() * loft + 1);
while (win == false)
{
System.out.println(" Gæt et tal mellem 1 og "+ loft + "):: ");
guess = input.nextInt();
FORSØG++;
if (guess == TAL)
{
win = true;
}
else if (guess < TAL)
{
System.out.println("Koldere, gæt igen");
}
else if (guess > TAL) {
System.out.println("Varmere, Gæt igen!!");
}
}
System.out.println(" Tillykke du vandt...endeligt!!! ");
System.out.println(" tallet var" + TAL);
System.out.println(" du brugte " + FORSØG + " forsøg");
System.out.println("Slut spillet? tast 1.");
System.out.println("tryk på hvadsomhelst for at spille videre");
int userInt = input.nextInt();
if( userInt == 1)
{
keepPlaying = false;
}
}
}
}
Simple answer. You didn't initialize all the necessary values within your 'keepPlaying' loop before beginning a second round after the player successfully completed the first round. See annotations to your code, below:
import java.util.Random;
import java.util.Scanner;
public class GuessingGame
{
public static void main(String[] args)
{
Random rand = new Random();
int TAL = rand.nextInt(20) + 1;
int FORSØG = 0;
Scanner input = new Scanner (System.in);
int guess;
int loft;
boolean win = false;
boolean keepPlaying = true;
while ( keepPlaying )
{
Scanner tastatur = new Scanner(System.in);
System.out.print("Enter a maximum limit: ");
loft = tastatur.nextInt();
TAL = (int) (Math.random() * loft + 1);
// *** LOOK HERE ***
// Reset the 'win' flag here, otherwise the player receives an
// automatic win on all subsequent rounds following the first
win = false;
while (win == false)
{
System.out.println("Guess the number between one and "+ loft + "):: ");
guess = input.nextInt();
FORSØG++;
if (guess == TAL)
{
win = true;
}
else if (guess < TAL)
{
System.out.println("Colder, guess again!");
}
else if (guess > TAL) {
System.out.println("Warmer, guess again!");
}
}
System.out.println("You've found the number!");
System.out.println("The number was: " + TAL + ".");
System.out.println("You guessed " + FORSØG + " times.");
System.out.println("To quit, enter 1.");
System.out.println("Provide any other input to play again.");
int userInt = input.nextInt();
if( userInt == 1)
{
keepPlaying = false;
}
}
}
}
Sorry for the translation into English -- I had to make sure I was reading things correctly. You might also want to substitute "higher" and "lower" for "warmer" and "colder." "Warmer" and "colder" tend to suggest a proximity to the correct answer, as opposed to the direction in which that correct answer lies.

How to add for loops to if statements?

In my code below I am not sure what order to put it in to work properly.
I first want it to print out for the user to select an option which it does, then if they select 1 it asks them their name and verifies it with the loop etc.
When I enter a name it starts to just loop the question enter your name and I don't know how to fix it.
Do I need to add more statements to my program, if I do then can I still use if statements for the user to select an option?
import java.util.Scanner;
public class username {
public static void main(String[] args) {
{
int UseLift;
int AuditReport;
int ExitLift;
int a;
UseLift = 1;
AuditReport = 2;
ExitLift = 3;
}
System.out.println("choose an option");
System.out.println("Uselift(1)");
System.out.println("see audit report(2)");
System.out.println("Exit Lift(3)");
Scanner d = new Scanner(System.in);
int a = d.nextInt();
Scanner kb = new Scanner(System.in);
// array containing usernames
String[] name = {"barry", "matty", "olly", "joey"}; // elements in array
if (a == 1) {
System.out.println(" Enter your name ");
}
String name1 = kb.nextLine();
boolean b = true;
int j = 0;// counter will start at 0
outerloop:
while (j < 3) {
System.out.println("Enter your name");
}
for (int i = 0; i < name.length; i++) {
if (name[i].equals(name1)) {
System.out.println("you are verified you may use the lift, calling lift ");
}
break;// to stop loop checking names
}
System.out.println("Username Invalid");
j++;
if (a == 2) {
System.out.println("");
}
if (a == 3) {
System.out.println(" Please Exit Lift ");
}
}
}
here you go:
public static void main(String... args) {
String[] verifiedNames = { "barry", "matty", "olly", "joey" };
System.out.println("choose an option");
System.out.println("Uselift(1)");
System.out.println("see audit report(2)");
System.out.println("Exit Lift(3)");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
scanner.nextLine(); // get '\n' symbol from previous input
int nameAttemptsLeft = 3;
while (nameAttemptsLeft-- > 0) {
System.out.println(" Enter your name ");
String name = scanner.nextLine();
if (Arrays.asList(verifiedNames).contains(name)) {
System.out.println("dear " + name + " you are verified " +
"you may use the lift, calling lift ");
break; // break out of loop
}
}
if (nameAttemptsLeft < 0) {
System.out.println("Username Invalid");
}
break;
case 2:
System.out.println("option 2");
break;
case 3:
System.out.println(" Please Exit Lift ");
break;
}
scanner.close();
}
Your while loop below:
while (j < 3) {
System.out.println("Enter your name");
}
will loop forever since j is not incrementing (j++). I believe you've mis-matched your curly braces at some point.

Categories