• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Method error

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what I am messing up here?









I want it to print:

hee.jpg
[Thumbnail for hee.jpg]
 
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Code Ranch !

When posting code here always UseCodeTags (<-- that's a link). Since you're new I'll fix it for you this time. See the line numbers and the indentation matching your original.
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Bartender
Posts: 2402
106
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, remove all the unwanted comments such as "//start class", "//start main" and so on.. The code indentations should make the structure clear.
Next, I see that your code has a few unwanted brackets and hence it's not going to compile. Can you change it to this format: Once you've fixed the brackets you can work on the rest of the code and share your experience with us.

Side note, If I had to print three lines with dashes, I'd simply write it as:
 
Marshal
Posts: 66189
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, welcome to the Ranch

salvin francis wrote:. . .

What'd be wrong withApart from line lengths.
 
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of errors in the code you posted. The lack of proper formatting makes it difficult to spot them though.

1. Your calcTotals() method is declared inside the main() method. You can't do that.

2. You have a number of variables which you declare twice. That's not a good practice as it can lead to errors and/or confusion.

3. You have semi-arbitrary code blocks where a code block is not necessary. In fact, some of those arbitrary code blocks contribute to the confusion.

Here's your code properly formatted and indented (I suggest you pay more attention to this detail in the future if you want to avoid unnecessary headaches—you're shooting yourself in the foot, in the arm, in the stomach, etc. by not formatting your code properly):
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and the comments aren't helpful. I fact, I think some of them are incorrect.
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...and the comments aren't helpful. I fact, I think some of them are incorrect.


Yup. This, for example:

and these:
 
salvin francis
Bartender
Posts: 2402
106
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What'd be wrong withApart from line lengths.


The line lengths are bit too much for my taste
 
Danielle Beard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errors coming up as:
----jGRASP exec: javac -g Program3hw.java
Program3hw.java:31: error: illegal start of expression
                                 public void Program2hw() {//method to calculate totals
                                 ^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

I am not sure about the indentations.....I just started coding about 3 weeks ago lol. I was just following what the teacher did.

As far as all of the comments, the teacher wants them in there so she can see if we know what we are doing.......I obviously have no clue LOL.

Thank you so much for your help!!! I am so lost. I am literally brand new to coding and old.....this is terrifying lmao
 
Danielle Beard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:There are lots of errors in the code you posted. The lack of proper formatting makes it difficult to spot them though.

1. Your calcTotals() method is declared inside the main() method. You can't do that.

2. You have a number of variables which you declare twice. That's not a good practice as it can lead to errors and/or confusion.

3. You have semi-arbitrary code blocks where a code block is not necessary. In fact, some of those arbitrary code blocks contribute to the confusion.

Here's your code properly formatted and indented (I suggest you pay more attention to this detail in the future if you want to avoid unnecessary headaches—you're shooting yourself in the foot, in the arm, in the stomach, etc. by not formatting your code properly):

 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A code block is anything that looks like this:

A code block groups statements together to be executed in sequence from top to bottom. The if-statement's body is a code block. All those statements inside the {} are going to get executed if the if-statement condition is satisfied.

It's syntactically legal to have an arbitrary {} block off a section of code but it's usually unnecessary to do that. It can create problems if you're not careful or do it inadvertently (not on purpose).

Look at your declarations on lines 10, 20, 33, and 34. Those are duplicate declarations.
 
Sheriff
Posts: 6363
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, where did I declare variables twice?


Lots of places.  After you've declared a variable, you don't have to use its type again during assignment.  If you do, you will declare a new variable.  So
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielle Beard wrote:. . . So, where did I declare variables twice?  . . .

Please write code in the code tags and text outside, otherwise your questions are very hard to find.
In your first post, look at lines numbered 5, 14, 26, and 27. I think you are not actually using anything declared in line 5.
 
Danielle Beard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I fixed alot of the issues, but I am still getting an error: illegal start of expression on public static estimateTotal.....I'm not sure what this is. I want to thank you guys for helping me out! I really do appreciate it. Coding is HARDDDDDDD. I give mad props to coders who do this day in and day out. Let me know what you think!


         
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason you have an estimateTotals() method()? It says it returns a double but you're not. In fact, you need to return two doubles. So, how will you do that? You could use an array of 2 doubles but I'm not sure your up for that yet. Or you could forget using a method altogether and just use the two formulas directly in your main() method.
 
Knute Snortum
Sheriff
Posts: 6363
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason why it's hard to see that your main method isn't  closed properly is that your code is poorly formatted.  Learn HowToFormatCode (that's a link).
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You  haven't said if you are using an IDE or a text editor to develop your code. If it's an IDE, all IDE's have an auto-formatting capability. If you are using a text editor, one option is to use JavaIndenter which is a Java program I wrote to do a basic job of formatting. You can get the code for this program from the link in my signature below.
 
Danielle Beard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I am using JGrasp, it is an IDE.

I read over the turtorial on Formatting! Got you!
So, I can just use this....

public class Program3hw
{
      public static final double taxRate = .060;
{


I actually like this alot better, it's easier to find it. That actually helped me out a lot.



Ok, so I tried to fix this error. Still not working for me.....

 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielle Beard wrote:. . .
public class Program3hw
{
      public static final double taxRate = .060;
{
. . .

This is what it would have looked like if you got the code tags right, but it still isn't correct, I am afraid:-I hope that isn't exactly what you are using; you are missing the spaces at the start of line 4. Remember who is going to have the most trouble if you write badly indented code, and who needs the help from the indentation most. You yourself.

If jGrasp (I haven't used it for years and years) won't help you with automatic indentation, change it for kate (Linux) or NotePad++ (Windows) or (don't know for Mac). They support automatic indentation, which will help you no end. Don't start numbers with the decimal point. Write 0.06.
 
Knute Snortum
Sheriff
Posts: 6363
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sublime also works well for a light Java editor in Linux.  jGRASP has something bundled with it call CheckStyle, but I don't know if that helps.
 
Danielle Beard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys, I have to get this method thing down before I get to the next assignments, which are a billion times harder....

Do you have anything I can work on  or try out to help me practice this? You have any work for me to do and you can check it maybe?
 
Carey Brown
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's been a while since you posted an update to your code. I assume that you've been working on some of the suggestions that have been posted and perhaps even cleaned up the indentation a bit. Can you post the current state of your code? Did you get it to compile? Did you get it to work?
 
Danielle Beard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

/* Danielle Beard CIS120 09/17/2019 Program #3
  This program will make use of methodswith parameters and return values,
  the scanner class and String methods. It will ask the user for their full
  name and amount of the price of the item. It will then calculate sales
  cost based on 6% tax rate.
*/
//import the java.util package in order to work with the scanner class

import java.util.Scanner;//start scanner

public class DanielleBeard3 { //start class
public static void main(String[] args) {//start main
       //start of scanner for the name and amount

   final double taxRate = .060;//declare constant variables
   double price;
   double tax;
   double total;
  //create scanner object
       Scanner getName = new Scanner(System.in);
       
           System.out.println("Please enter your full name : ");//user enters name here
           String useName = getName.nextLine(); // get string    
                                                     
           System.out.println("Hi " + useName + "! Please enter the price of your item : ");
           price = getName.nextDouble();
           
           //calculate the tax
           tax = price * taxRate;
           
           //calculate total cost
           total = price + tax;
           
           
     

       //use for loop to print "-" 3 times
       //use for loop to print "-" 3 times
       for (int i = 1; i <= 3; i++) {//open for loop
           for (int j = 1; j <= 20; j++) {
               System.out.print("-");//open nested loop
           }//close for loop
           System.out.println(); //to end the line
           }
        //display prices
           System.out.println("Item price = $" + price);
           System.out.println("Tax = $" + tax);
           System.out.println("Total = $" + total);
           System.out.println();
                            }//close class
                                         } //close main




Got it to work, however, I guess my teacher wanted me to do methods? I guess I am still not understanding exactly what a method is?
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danielle,

When using code tags, your code goes between the start and end tags. Like so:

[code=java]
    Your code goes here
[/code]
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of methods as small named tasks. For example, instead of this:

You could use a method to abstract away the details:

This is a very trivial example meant to illustrate a point. The method name displayGreeting() explains the intent or what it does. The statements inside the method explain how the intent is carried out. That is, the body of the method shows the implementation.

In the main() method, the code should show intent rather than implementation. This makes the code more high-level and abstract and allows other people to get a sense of what the program does without having to decipher the details of how it does it. Abstraction at a high level is useful especially for large programs that do a lot of things.

Your problem, for example, could have the following high-level code in main():

The main method is now very short but it still tells a story of what the program does without overwhelming the reader with all the details of how the program does it. To see those details, you'd go into the specific method that says what those details are.

Does that make sense?
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, if you look at method themselves, there are a few ways you can write them.

Here's a method that just does something (without giving back anything):

The word "void" on the first line is the return type declaration. This says the method doesn't return any value, not even null.

Here's a method that does return something:

This method is declared to return a String value, which it does on line 2.

Here's a method that takes a parameter and returns something:

The declaration inside the parentheses on line 1 is called the parameter list. In this case, the list consists of a single String parameter which we've named "name" to explain what the parameter is meant to be. That is, the parameter name explains the intent or purpose. The return statement on line 2 simply gives the result of concatenating "Hello, " and the name provided to the method when it was invoked/called. You would use this method like this:

That would print out this message on the console:

Hello, Danielle
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's talk about names for a minute. It's very important to use good names in your program. Good names help explain what the program is doing. It helps people understand the story that your program is trying to tell.

Let's say we go into your kitchen and you are trying to explain to me what all the gadgets and utensils in the kitchen do (pretend I have amnesia or have never before seen a modern kitchen before).

You point to the toaster and say "That's what we call a 'makeToastedBread'. This is a plain slice of bread. We put the bread in the makeToastedBread, and then it sits in there for a while and it pops out. Now we have a slice of toast!"

An observer might think, ok, that's kind of weird but sure. Then you take something out of a box and then say "Oh yeah, you can also use the makeToastedBread for these things: Pop Tarts! You just take the cold pop tart out of the packet, pop it into the makeToastedBread and let it sit there for a minute and when it pops out, you have a little pastry that can totally obliterate your sense of taste for a couple of hours! Isn't that cool?"

Now our observer is really weirded out. Why keep calling the toaster a "makeToastedBread" when obviously it can be used to nuke a pop tart? What else can this deceptively named thing do?!

This might seem like a strange story but it's not unlike the story your code tells:

The line 1 is the equivalent of you pointing to a toaster and saying "This is a 'makeToastedBread'"
The line 3 is you popping in a slice of bread into the "makeToastedBread" and making a slice of toast.
The line 5 is you putting a pop tart into the "makeToastedBread" and making a pastry filled with boiling lava.

Compare that with this code that uses sensible names instead:


The moral of this story is that you should try to choose names that represent the purpose and intent of your program elements well so that the story makes sense to the reader.
 
Danielle Beard
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes!!! I get it now....
So its like...

Static void means we arent doing anything, static string, char, boolean and whatnot is we are getting ready to go make some convo with someone!

So we call out what we are doing first by the name of the method:
cookingInTheKitchen

Method is formatted like:
cookingInTheKitchen:
And all of the things we do in the kitchen

Then a method name has to be generic to what we are doing so we can use it for alot of things if we need to

I'm so excited, I'm actually getting this I love you guys! Lol
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielle Beard wrote:. . . Static void means we arent doing anything, . . .

'Fraid you have got a bit more to learn than that. A void method might do all sorts of things. All void means is that it doesn't leave a value behind. If you declare a method with any other type, not void, it means that method leaves a trace behind itself in the form of something of the type you have given it.

And one of the hardest things some people have to learrn is when not to write static.
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!