File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Basic Java - Please help a stressed noob with a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Java - Please help a stressed noob with a method" Watch "Basic Java - Please help a stressed noob with a method" New topic
Author

Basic Java - Please help a stressed noob with a method

Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
My name's Ali, I have just started two units of study with the Open University, so I am entirely fresh-faced to Java. I am trying to get the following code to compile but the lack of support i'm getting from my tutor is horrendous and the several books I have splayed out across my room aren't helping me get going.

I have been given a method that I wish to answer some questions about:

public char[] methodA()
{
char[] alphas = {'s', 't', 'e', 'a', 'm'};
char temp = alphas[0];
int i = 0;
while (i < alphas.length - 1)//1
{
alphas[i] = alphas[i+1]; //2
i++;
}
alphas[alphas.length-1]=temp;
return alphas;
}


How do I get this to compile successfully using my IDE? I have tried just enclosing it within the syntax for my 'main' method but it's not accepting it.

Thanks to anyone who can help. Ali
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Did you enclose your main method within a class?

If you are so new to java, i would recommend using a notepad or any other text editor. Type what you feel the answer is and compile your code. Check for compilation errors. I dont want to give you an exact answer as that would defeat the purpose of your assignment. Try doing what i have said and paste the compilation errors that you're getting. Also, paste what code you have written.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
Hi Chander, thanks for the quick reply.

I am using NetBeans as this is essential for further parts of the study.
Whilst I appreicate you do not want to help me with the assignment, I will receive no marks for having the correct code, nor will anybody even look at it. I wish to get the correct code
a. to learn
b. to check my answers.

I have been given a list of statements to see if they are true or false
e.g. "The for loop header labelled //3 will be evaluated 7 times."

Here is what I have at the moment code-wise:

package openuniversity;

public class Main
{

public static void main(String[] args)
{

public static char[] methodA()
{
char[] alphas = {'s', 't', 'e', 'a', 'm'};
char temp = alphas[0];
int i = 0;
while (i < alphas.length - 1)//1
{
alphas[i] = alphas[i+1]; //2
i++;
}
alphas[alphas.length-1]=temp;
return alphas;
}


Basically just want to get the thing running!
Ricardo Espírito Santo
Greenhorn

Joined: Nov 04, 2009
Posts: 25
Hey Ali,


(Save this code in a file called A.java)
worked for me no IDE just plain text editor (vim in linux) and the good old javac

What sort of difficulties is your IDE (which one are you using by the way) presenting?

chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

There you go:




You defined your method inside main. Which is why your IDE was throwing exceptions. I think you should spend some time reading this: http://download.oracle.com/javase/tutorial/java/
Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
Hey -- I think I have it!



package openuniversity;

public class Main
{

public static void main(String[] args)
{
}

public static char[] methodA()
{
char[] alphas = {'s', 't', 'e', 'a', 'm'};
char temp = alphas[0];
int i = 0;
while (i < alphas.length - 1)//1
{
alphas[i] = alphas[i+1]; //2
i++;
}
alphas[alphas.length-1]=temp;
return alphas;
}
}



My issue was that I had my method inside my main method.

Now for the final part of my drama with this method. How do I find out what is returned by it. I know there is the 'return alphas;' part, but how do I display what is being returned?
A simple System.out.println(alphas); didn't work haha.

Thanks for your help with this, it is appreciated!
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

In that case, your main method should look something like this:


Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
Thank you very, very much Chander.

Just from examining your code there, I have learnt more in 10 minutes than I have for the last two nights I have been struggling with this coursework.

I hate taking something and not being able to contribute anything back -- hopefully in a year or two I can start helping people out.

Thanks again!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Good plan -- helping others out also helps you to learn. For example when you see a question that you cannot answer, it's an opportunity to learn enough to answer that question. And as you go along, you'll find that you will be able to answer tougher and tougher questions as your own skill level increases.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
OK, this is a similar problem.
I have adapted the code provided earlier to accomode this new method, methodB.

The line
return first;
gives me an error: incompatible types, char[] required

Any suggestions as to what i'm missing here?

chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Look at your function signature:



It says your function will return a char Array

When calling your function from main, you are storing the method's return value in a char Array

However, what you're really returning is a single character (char first) and not a character array. Which is what you're compiler is complaining.

Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
Ok if I understand what you are trying to tell me



is telling the compiler that the function will return an array.

I have learnt that an array is a type of container that store elements of a similar type.
So the list 'alphas' is an array containing the letters.

I have nooo idea what the 'for' loop is doing.

What should I do to make the code work?
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

I'm not really sure what you're being asked to do in your assignment. Is correcting the code part of your assignment? Because, if you're professor has given you this code and simply asked you to execute it, then there is a bug in his code.



Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
I think I got it chander.

This is one of the things I hate about the Open University. Here is the question I was given:

Study the two instance methods below and then select only the options that are correct.


  • The assignment statement labelled //2 will put a copy of the char element one to the right of the current element in alphas into the current element in alphas.
  • The for loop header labelled //3 will be evaluated 7 times.
  • The if statement labelled //4 will update the value held by the variable first if the value held in the current element in alphas comes before the current value of first.
  • The boolean condition in the line labelled //1 will evaluate to false repeatedly until i takes the value 4.
  • The returned value on invoking methodA is a char array containing the values 't', 'e', 'a', 'm' and 's'.
  • The returned value from invoking methodB is the character 'u'.


  • I changed my main method and function signature to:




    That got it to work.

    As for the questions, would you be able to assist? I don't want you to give me the answers, as I really do genuinely want to learn, but the course material does not cover any of this which is making me mad and stressed.

    The second statement is false as the for loop will be evaluated 6 times, not seven.
    The fifth statement is true as I tested it with the first code you helped me with.
    The sixth statement is false as the returned value is 'a'.

    As for the first statement, I believe it to be true..? Is this correct.

    Just the third and fourth statements that have me stumped.. then I can go to bed.
    chander shivdasani
    Ranch Hand

    Joined: Oct 09, 2007
    Posts: 206

    Let me try to explain you what the first method does:




    Try to understand this method clearly. The best way is to use pen and paper and try to do it yourself.

    Try doing the same with the second method and see if you are able to understand.
    [edit]Add new lines CR[/edit]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38818
        
      23
    Ali Lumsden wrote: . . .
    I am using NetBeans as this is essential for further parts of the study.
    . . .
    No, it isn’t. It is never necessary to use an IDE in Java™ at all, and at the beginner’s stage it is liable to cause confusion and prevent you learning the basics, particularly the instructions at the command line. It is also usually unnecessary to use package names at this stage; again this can simply confuse you until you are more experienced.

    And welcome to the Ranch
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Basic Java - Please help a stressed noob with a method