• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cant find the answer  RSS feed

 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I put this code in a way that it works because I know it is probably something obvious but I cant find how to make the array readable without error. It is supposed to intake two values and output what day of the week it is. Please review and see if you can fix it.

Testing Program

Initial day: Monday

Next day: Tuesday

Next day again: Wednesday

Previous day: Tuesday

Next week: Tuesday

Add 4 days from initial day: Friday




ERROR ; Days.java:5: error: class, interface, or enum expected
String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
^
1 error
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That line needs to be inside the class definition.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And lines 4 and 5 should be inside a main method.
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I got to this point, and I want to know why


are both giving me errors. How would I declare a main method for them. Thanks in advance.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Steele wrote:
are both giving me errors. How would I declare a main method for them. Thanks in advance.


Start reading basics of java then you can find the answer .

You need to declare this statement inside main() .

Check this link : webpage


Satya
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Steele wrote:
I got to this point, and I want to know why


are both giving me errors. How would I declare a main method for them. Thanks in advance.


To run a Java application, you need a main() method. It will look basically like this:



This is the method that is executed to start your program.

All statements must be in a method except for declarations. They don't have to be in a main method, but some method.

The code above is failing for two reasons: args[] has not been declared and you can't initialize a declaration outside of a method.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also if these two statements are inside main(String args[]) then args is declared to be of type "String".

The above statement will give an error because args is of type "String" and you trying to assign that value to a variable of type "int".
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I put this in a main method, but I get one error.

-------------------

Days2.java:7: error: '.class' expected
String newDay = args[];
^
1 error

------------------

I think I am missing something there. By the way I am working on the basics of JAVA through an actual "guide to learn". The book is called Head First, and it is going to be how I learn JAVA. My teacher has given us a reference book and along with our last assignment is one to write a rap about JAVA. Not the best teacher considering it is an online course and our only source of help is textbook. She also takes forever to get back to you if you ask a question and some times not at all. So if my posts seem uninformed please consider my introduction to JAVA has horrible. There was no assignments in the beginning like.... what is int, double, char or string. It was left up to us to pick from the text what we thought we had to learn. </End rant>



Is this the correct way to put it in main class and why do I have an error?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Steele wrote:


Is this the correct way to put it in main class and why do I have an error?

look at line 4. On the left side of the equals, you are declaring a String variable. It can point to a single String.

On the RIGHT side, you have a String array.

You cannot have a String point to a String Array.

I would suggest you stop. Step away from the computer, and think about what you are trying to do. You seem to be trying random stuff, and just HOPING it works. That is really not a good way to write a program. Given enough time (i.e approaching infinity) you will get it to work, but I'm betting you don't have that long.

so...shut off you computer. write down in English what you want to have happen. Don't use "java" words, but plain old words you learned in grade school. In fact, pretend you are telling a 10 year old child what they need to do.

only when you have that done should you write a single line of Java.
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jagotta help me out. I got this far, but there is something keeping from outputting correctly. Admire my code.



 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a close look at your "change","changeday" and "previousday" methods. The variables declared inside them namely "nextDay","changeDay" and "previousDay" cease to exist as soon as you come out of the corresponding method. Also "nextDay","changeDay" and "previousDay" variables declared inside main() and declared inside those three methods have no relation to eachother.They are completely independent. Assigning a value to a variable of one name will not assign that value to a variable of the same name in some other method.You have to return the value that you calculated inside the method so that you don't "lose" it once you come out of the method.Here is a link to the java tutorial on returning a value from a method.
But I would suggest you to take out a pencil and paper and actually do some writing as to what exactly you want to do. How you're going to do it and then actually write pseudo code that does the job. After this is done writing java code shouldn't take long and if you're pseudo code is correct the java code written correctly will be sure to work.
Hope I helped!
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are trying to help you.
But at this site, we don't help you by giving you the answer. This is NotACodeMill

We help you by pointing you in the right direction so that you can do it yourself.

How do you intend to run your program?
What do you expect it to do?
How do you pass it values?


Lets start with the first line of executable code:
int currentDay = Integer.parseInt(args[18]);

What is this line meant to do?
What is 'args' ?
Why the number 18?
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets start with the first line of executable code:
int currentDay = Integer.parseInt(args[18]);

What is this line meant to do?
What is 'args' ?
Why the number 18?

(1)The line is meant to set the day string args[] = {"Sun","Mon","Tues"...} ( might be done at the end of the code... to tie numbers to the days of the week.

Then the number 18 is supposed to be divided by 7 to give a remainder that gives the day of the week. So 18/7 equals two remainder 4. The day would be Wed.
----
Then the code is supposed to give for example, current day:Mon , previous day:Sun (currentDay-1), new day: Thur
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okaaaaaaay.

Given:


What is args? (hint: http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html)
Knowing that, what would: Integer.parseInt(args[18]) give you?
(Hint: it's not what you thought in your last post)




 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an arguement "args" says "get ready for the following information" ?? like then store it in an int

And then you're right. It would be Wed., because 4 is the remainder.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learn here how arrays work and what the number between the square brackets means: Java Tutorial: Arrays

What the 'args' arguments on the main method means: Command-Line Arguments

Why are you using args[18]? What do you think that does? you should be able to understand that if you know what args is and what the number between the square brackets means if you use it on an array.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!