Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jean y
Java Web Development Techdegree Student 2,074 PointsError while expected
''''
class HelpSystem {
char menuItemNumber ;
int subject;
boolean isValid (int menuItemNumber) {
if (menuItemNumber < '1' | menuItemNumber > '7' & menuItemNumber != 'q') return false;
else return true;
}
void showMenu() {
System.out.println("Help on: ");
System.out.println(" 1 if");
System.out.println(" 2 switch");
System.out.println(" 3 for");
System.out.println(" 4 while");
System.out.println(" 5 do-while\n");
System.out.println(" 6 Break");
System.out.println(" 7 Continue:\n");
System.out.println("Choose one: (q te quit): ");
}
void helpOn (int subject) {
switch (subject)
{
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
case '3':
System.out.println("The for:\n");
System.out.println("for (for(init; condition; iteration)");
System.out.println(" statement");
break;
case '4':
System.out.println("The while: \n");
System.out.println("The white(condition) statement;");
break;
case '5':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
}
}
}
class HelpClassDemo {
public static void main(String args[])
throws java.io.IOException {
char subject, reject;
HelpSystem helpObject = new HelpSystem();
for (; ; ) {
do {
helpObject.showMenu();
subject = (char) System.in.read();
do {
reject = (char) System.in.read();
} while (!helpObject.isValid(subject));
if (subject == 'q') break;
System.out.println("\n");
helpObject.helpOn(subject);
}
}
}
''''
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsHi, the error is : you forgot to add while for do loop:
do {
helpObject.showMenu();
subject = (char) System.in.read();
do {
reject = (char) System.in.read();
} while (!helpObject.isValid(subject));
if (subject == 'q') break;
System.out.println("\n");
helpObject.helpOn(subject);
} // should be while here like in the do above
If you are willing to take advice about your code:) Here things are not welcomed in Java:
- Separate classes in different files:
HelpClassDemogoes toHelpClassDemo.java, andHelpSystemgoes toHelpSystem.java. - use packages, leaving all in default package is not a good policy in Java. You are probably on Java Track. So keep an eye on Craig Dennis code, he'll provide a lot of examples.
- You have a lot of unneccessary spacing between code structures: Take time to look at this Google Java Guide: https://google.github.io/styleguide/javaguide.html
- Using
for (; ; ) {is a very bad idea, usewhile(true)...doif you want for cycle to be broken inside. - Keep along on Java Track, and come back re-factoring your code here. There are a lot of things to do :)
Jean y
Java Web Development Techdegree Student 2,074 PointsI thought the compiler would do that
Jean y
Java Web Development Techdegree Student 2,074 PointsJean y
Java Web Development Techdegree Student 2,074 PointsWhy does this not compile?