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
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 PointsHelp needed for a Java programming (Beginner)
I am doing this program for my school project and i am getting this error that I was unable to figure it out, here is my code:
import java.util.Scanner;
public class groupProject
{
public static void main(String[] args)
{
//initialize variables
double labFee=0.5;
double courseCost = 1200;
double totalCourseCost = 0;
double totalCost = 0;
Scanner in = new Scanner(System.in);
System.out.println("Pleaes enter your student ID?");
int studentID = in.nextInt();
System.out.println("How many courses are you taking? ");
double numCourse = in.nextDouble();
if(numCourse >3)
{
System.out.println("Please notice that you are only required to enter 3 courses for this semester!!");
}
if (numCourse<=3)
{
for (double count=0; count<=3; count++)
{
System.out.println("What is the name of the course?: ");
String courseName = in.next();
System.out.println("What is the course Prefix?: ");
int coursePrefix = in.nextInt();
}
//calculate the courses cost
totalCost = (numCourse * courseCost);
totalCourseCost = totalCost + (totalCost * labFee);
}
// output the values we calculate to the user
System.out.println("The Total Cost for this Semester is : " + totalCourseCost);
System.out.println("The Course Name: " + courseName + coursePrefix);
} }
here is the 2 errors i am getting:
groupProject.java:46: error: cannot find symbol System.out.println("The Course Name: " + courseName + coursePrefix); ^ Please help me if you can look at it with fresh eyes and give me some suggestion I will really appreciate it.
Thank you in advance
17 Answers
samiff
31,206 PointsI can't compile Java on the system I'm currently on, but that error you're getting is due to variable scope. You defined the variable "courseName" within your for loop, and it only exists within that loop's context. You then tried to access that variable outside of that scope, which is why you get that error. There are some videos here on Treehouse that deal with variable scope if you need help understanding that concept.
Also, not sure if you've covered it yet, but it's a good habit to get into to start closing any resources you open. You should close out your scanner object with "in.close();" when you are finally done with it.
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 PointsThank you so much Same for your help, I will go ahead and watch those videos about variable scope.
samiff
31,206 PointsHere is the link for the Javascript scope video, but the concept is the same: http://teamtreehouse.com/library/websites/javascript-foundations/variables/scope
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 PointsHey Sam,
thanks again for the link for the java scope video but just want to tell you that i fixed and it is running but it won't really give me a chance to enter the class prefix nor the course name and it goes straight to calculate the total cost.
here is the code again modified:
import java.util.Scanner;
public class groupProject
{
public static void main(String[] args)
{
//initialize variables
double labFee=0.5;
double courseCost = 1200;
double totalCourseCost = 0;
double totalCost = 0;
String courseName=("");
int coursePrefix=0;
Scanner in = new Scanner(System.in);
System.out.println("Pleaes enter your student ID?");
int studentID = in.nextInt();
System.out.println("How many courses are you taking? ");
double numCourse = in.nextDouble();
if(numCourse >3)
{
System.out.println("Please notice that you are only required to enter 3 courses for this semester!!");
}
if (numCourse<=3)
{
for (double count=0; count<=3; count++)
{
System.out.println("What is the name of the course?: ");
System.out.println("What is the course Prefix?: ");
}
//calculate the courses cost
totalCost = (numCourse * courseCost);
totalCourseCost = totalCost + (totalCost * labFee);
}
//Output the total cost course name and prefix
System.out.println("The Total Cost for this Semester is : " + totalCourseCost);
System.out.println("The Course Name: " + courseName + coursePrefix);
} }
samiff
31,206 PointsYour modified code doesn't call your scanner object to set courseName or coursePrefix in your for loop, could that be the problem?
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 PointsI am not sure I wish you had some sort of software like jGrasp or eclipse that might help but I do appreciate your help a lot.
Thanks
samiff
31,206 PointsI was able to get on my machine and try the code. It "works", but there are a few concerns. It seems like you are only doing calculations when 3 or less courses are being taken, but in your for loop, you start count at 0 and use <= 3, which will actually run four times. Also, I'm not sure what your last print statement is for as it will only print out the very last courseName/coursePrefix entered in.
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 PointsMy last statement consist of printing the course name for example MTH and its prefix such as 271 so it will print out MTH 271. My biggest concern is about the fact that I am unable to enter the course name neither the prefix I am certain that after you run it it won't give you a chance to enter these two things. However, you are able to put your student ID and the number of courses you are trying to register. Yes, you are right about the count I just changed to 1 instead of 0 I didn't pay attention to that thank you. However, I am still not getting why it won't allow me to enter the courseName and the coursePrefix?
samiff
31,206 PointsLike I said, it looks like you removed the calls to the Scanner object to gather input.
System.out.println("What is the name of the course?: ");
System.out.println("What is the course Prefix?: ");
vs
System.out.println("What is the name of the course?: ");
courseName = in.next();
System.out.println("What is the course Prefix?: ");
coursePrefix = in.nextInt();
And the way you have it setup, you're only going to be printing the name/prefix for one course. Is that intentional?
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 Pointsyes I did that because when i had the calls to the Scanner object it won't run and instead I initialized these two variable after the
String courseName=(""); int coursePrefix=0;
and obviously I can't keep both because it will say that i has being initialized already and it will give me errors. So if I keep the second part it will give me an error.
samiff
31,206 PointsTry adding what I last put. You've declared/initialized your variables outside of your loops, and inside of them, you're just reassigning values to the variables.
Also, when you get it running, what happens when someone is taking just one course? (it will still gather input three times)
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 PointsSam I can't thank you enough it works perfect!!!!!! awesome!!!
samiff
31,206 PointsCool, was thinking it was time for some screensharing Also, try putting in something like "100 abc" when it asks for the prefix. You'll notice something weird. (in.nextline could help with this issue).
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 Pointscause the prefix it is only integer you can't put characters in it and one more thing if you don't mind i'm trying to print out all the courses that i entered for example let's say he asks how many courses would you put and i will put 2 course names with 2 prefixes related to them can i print/list all the courses names because it only print our the last course entered?
samiff
31,206 PointsActually, numbers followed by a space and characters will work just fine, try it! That's why I pointed out next line, because sometimes it may be useful to simply call in.nextline() just to clear things out if a user has accidentally/intentionally entered extra input.
The easiest fix will be to print the course name and prefix inside of the for loop right after the user has entered the inputs. Or you could use a simple array to hold that information to print later. Your for statement should probably look like
for (double count = 0; count < numCourses; count++)
Also, it's good habit to start using the appropriate variable types in Java. For a simple iterator, you don't need to be using a double type, you should be using an int type.
If you're interested in Java, I really liked Cay Horstmann's books such as Big Java which is also on Safari Books Online. It's great to learn Java, but it also teaches you how to think like a programmer more importantly.
Ahmed Ben Salem
Full Stack JavaScript Techdegree Student 1,246 PointsYeah I have that book great book just bought it couple of weeks ago, thanks again Sam.
samiff
31,206 PointsCool! This video series is also good to learn about object oriented approaches.