• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

New to Java and need help

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am nervous about using another forum but I need help so here goes. I am just learning Java this semester and we use JGRASP for our programs. For my program I am supposed to calculate lunch for three with user input. this is my code:import java.io.*;


// @author Vanessa

public class buyingLunch {

public void showValues()
{
//Display prices of items
System.out.println("Apples cost $.50 each " +
"\n Cheese cost $2.05 per pound " +
"\n Bread cost $1.00 per loaf");
}
public static float getValues()
throws IOException
{
//set input stream
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
//get numbers from user input
System.out.print("How many apples do you want? ");
float x = Integer.parseInt(stdin.readLine()) * .50f;

//asks user for 2nd number, than converts it to an integer
System.out.print("How many pounds of cheese do you want? ");
float y = Integer.parseInt(stdin.readLine()) * 2.05f;

//asks user for 2nd number, than converts it to an integer
System.out.print("How many loaves of bread do you want? ");
float z = Integer.parseInt(stdin.readLine()) * 1.00f;
}
public static float calculateTotal()
{
//add x,y,z
float sum = x + y + z;

//Display sum of x,y
System.out.println("Your total is $" + sum);
}
}

my error is in float sum = x + y + z+;
the error says can't find symbol x
I have been trying to figure it out and got rid of my other errors. this is the information I had to go on:
1.)Initialize and display the cost of each item for a lunch for three. Lunch items will consist of apples, cheese, and bread. Apples will be priced individually, cheese by the pound, and bread by the loaf. The price and amount of each item is held by six data members. The constructor initializes the number of each item and the price per pound of each item. An accessor method displays the amount purchased and price of each item. A mutator method changes the amount purchased of each item.
2.)Calculate and display the total price all the items. A final method calculates and displays the total. Include a main() method to test your program.

thanks for any help. It will be greatly appreciated
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my error is in float sum = x + y + z+;
the error says can't find symbol x
I have been trying to figure it out and got rid of my other errors. this is the information I had to go on:



Basically, you are trying to use the variable x, but there isn't a local variable with that name, nor is there a class variable with that name. There is a local variable in another method with that name, but obviously that is out of scope... so the compiler can't resolve the variable.

Henry
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,
Thank you so much. I was afraid it was something very simple. I will remember what you said in the future and thank you for being so nice about helping me. I will certainly use this forum more often.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed the code in such a way .what you intend to see the output.See the below Code which will help you

import java.io.*;


// @author Vanessa

public class buyingLunch {

public void showValues()
{
//Display prices of items
System.out.println("Apples cost $.50 each " +
"\n Cheese cost $2.05 per pound " +
"\n Bread cost $1.00 per loaf");
}
public static float getValues()
throws IOException
{
//set input stream
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
//get numbers from user input
System.out.print("How many apples do you want? ");
float x = Integer.parseInt(stdin.readLine()) * .50f;

//asks user for 2nd number, than converts it to an integer
System.out.print("How many pounds of cheese do you want? ");
float y = Integer.parseInt(stdin.readLine()) * 2.05f;

//asks user for 2nd number, than converts it to an integer
System.out.print("How many loaves of bread do you want? ");
float z = Integer.parseInt(stdin.readLine()) * 1.00f;
}
public static float calculateTotal()
{
//add x,y,z
float sum = x + y + z;

//Display sum of x,y
System.out.println("Your total is $" + sum);
}
}

Thanks
Vikram
www.developerparadise.com
 
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vanessa and Vikram,

could you please both Use Code Tags in the future? It will make your code so much easier to read.
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikram,
when I tried the code you posted I got an error. I don't understand why. Here is the error I got when it compiled.
[error]
buyingLunch.java:17: ';' expected
public void getInteger()
^
1 error
[/error]
 
vikram veera
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can you copy the code you are trying to Compile.In my code you may miss some semicolon in the code.

Thanks
Vikram
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did a copy and paste with your code about three times and went through it line for line. Does the fact that I am using jGRASP for the code make a difference. I am sorry to be such a bother and I am really trying to understand.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vanessa Roach:
I did a copy and paste with your code about three times and went through it line for line. Does the fact that I am using jGRASP for the code make a difference. I am sorry to be such a bother and I am really trying to understand.



Hi Vanessa,

I'll try to help but I'm also a homework newbie, k.

I think if you use float x, float y and float z in your getValues() method, these x, y, z are stuck inside that method only and other methods cannot use them unless you call them explicitly (but I'm not all 100% sure about this, k). You can try using instance variables float x, y, z in the BuyingLunch class so that all your methods in that class can use them. I think it works better that way.

May I know, why are you using static methods?

I've refactored your codes a bit, but I'm not sure if that's the correct way to do it. By the way, the first letter in the class name has to be in capital letter.



Regards.
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your code and it compiled fine, however when I tried to run it I got an error:
[error] ----jGRASP exec: java BuyingLunch

java.lang.NoSuchMethodError: main
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
[/error]

I appreciate your help on this. I'm not sure why the static in my code. To be honest I have never done java before this semester and am finding it impossible. I am supposed to graduate in may with associate in web design.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vanessa Roach:
[error] ----jGRASP exec: java BuyingLunch

java.lang.NoSuchMethodError: main
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
[/error]

I appreciate your help on this. I'm not sure why the static in my code. To be honest I have never done java before this semester and am finding it impossible. I am supposed to graduate in may with associate in web design.



Ah, I see, web design is very different from Java. :-)

You got the NoSuchMethodError: main is because you didn't write a main method for your class. Main method is just like any method in your class but it has this signature : public static void main(String[] args) { //code } . You need this main method as an entry point to run your code.

Put something like this inside your class and it should run:


Regards.


[HENRY: Explaining main() was good. Doing the homework assignment was a bit too much]
[ September 14, 2008: Message edited by: Henry Wong ]
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ah, I see, web design is very different from Java. :-)

You got the NoSuchMethodError: main is because you didn't write a main method for your class. Main method is just like any method in your class but it has this signature : public static void main(String[] args) { //code } . You need this main method as an entry point to run your code.

Put something like this inside your class and it should run:



LS chin, while I am pretty sure Vanessa appreciates your help, it was not clear if the "conversion to instance variables" solution was understood. And it is also not clear if your "main" solution was understood either.

You can't just give solutions. There is little learning. And while it may help help in short run, all you did was solve a homework problem without any learning, to allow Vanessa to face an even harder next assignment next.

Henry
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the few things I do know is the main . I feel really stupid for not seeing that it wasn't there. I guess I'm just really tired. I've been tryint to figure out this code for about 2 or three days. The program worked! I have no idea how to thank you for your help and I am going to print it and study it.
Something I don't understand and have never seen is the bl in the main. Why is it there and what does it mean?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Something I don't understand and have never seen is the bl in the main. Why is it there and what does it mean?



Obviously, I didn't remove the solution in time. So, I will answer the question, since no one can see the code anymore.

bl is a local variable used in the solution. If you had done it yourself, you could have named the variable differently. You could have kept the methods static and not needed the variable. Or you could have implemented something entirely different.

Henry
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Henry. Some things in LS chin' s code I realized when I saw it. I thought you only needed a try, catch with string. I didn't realize it was with all input. However as in my last post the bl thing is getting me. I appreciate the help from everyone and for the first time in three days or so I don't feel like totally throwing in the towel.
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry.... I still don't understand the local variable in the solution. I understand variables and understand the try catch but like with last week in class other people were using a line like the

and this may sound stupid but I don't understand it.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vanessa Roach:
I'm sorry.... I still don't understand the local variable in the solution. I understand variables and understand the try catch but like with last week in class other people were using a line like the

and this may sound stupid but I don't understand it.



To try to answer the question... Your main method is static, and from the static context, you can only access static variables and methods. To access instance variables or call instance methods, you actually need an instance. This is why this variable is created -- it is done to provide an instance to access the other methods in the class.

And here is the ironic part... Originally, you didn't need it. If LS chin hadn't converted your methods from static methods to instance methods, you could have called the methods directly from main, without the need for the bl variable.

Henry
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.... I still need the try catch though right? And since I didn't need the bl thing originally... could you work with me to figure out why that's not working? I have no idea why it won't work and would like to understand why since I may have been on the right track. Also, which way would be easier for me to make it through this semester?
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vanessa Roach:
and this may sound stupid but I don't understand it.



It doesn't sound stupid at all. I didn't understand it either at first.

You can name the reference variable anything you like. I just chose 'bl' because it's the first letters in BuyingLunch. If you want to name it something else e.g. 'aa', it's also ok.

BuyingLunch aa = new BuyingLunch();

Which can also be written like this:

BuyingLunch aa; ------ line 1
aa = new BuyingLunch(); ----- line 2

Line 1 is a variable declaration. We call "aa" a 'reference variable'. It acts like a 'pointer', or we can think of it like a 'remote control' to an object.

Line 2 has the word "new". It means that we've created a new object and this new object is referenced using the variable "aa".

That's what I understand anyway. I hope it is correct!


p/s: sry about posting the whole complete working code but sometimes we need to see a working code to get us motivated again, huh.
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I get it! and chin, although you weren't supposed to show me the code... it did help because I am a visual person and thats the way I learn see it and understand it.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Originally posted by Henry Wong:
And here is the ironic part... Originally, you didn't need it. If LS chin hadn't converted your methods from static methods to instance methods, you could have called the methods directly from main, without the need for the bl variable.



Can you show us how to write it with the static methods?
I'm still learning static methods and static variables.

Thanks a bunch!

Regards.

Edit: It's ok, I think I got it working with all static methods and static variables, without using any reference variable.
[ September 14, 2008: Message edited by: LS chin ]
 
Marshal
Posts: 80061
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by LS chin:
Can you show us how to write it with the static methods?
I'm still learning static methods and static variables.

Please don't try to get the thing working with static methods; using only instance methods is much better object-oriented design.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Please don't try to get the thing working with static methods; using only instance methods is much better object-oriented design.



Thanks! :-)
 
Campbell Ritchie
Marshal
Posts: 80061
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a little rule of thumb about static methods:

If they never take any information from an instance, never use any information from an instance, never display any information from an instance, never return any information from an instance, never put any information into an instance and never alter any information in an instance, not even as a side-effect, then you are looking at static methods.
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone, thanks for the help on my assignment. I understand a little more about java although I think I will be back often. I do have another question.... How does a person search topics on here (so maybe I can find answers without bugging you all)?
 
Campbell Ritchie
Marshal
Posts: 80061
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a search link above the messages. I think it's here, but that link only works for beginner's. You can change forum, keywords, etc, easily.
 
Vanessa Roach
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok..... Thank you!!!
 
Campbell Ritchie
Marshal
Posts: 80061
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic