The Student Room Group

Java nerds: why isn't this basic code being compiled?

New question in post #27.

Apologies for the lack of key terminology and the awkward pasting of code...
(edited 7 years ago)

Scroll to see replies

Reply 1
It should be this..
output out = new output(); You put in a space between the method name and the opening bracket
(edited 7 years ago)
Reply 2
Original post by UWS
output out = new output();


That is what I've done, though (?)
Reply 3
Original post by _howl
That is what I've done, though (?)


Sorry please see edit, there is a space in there. Perhaps you can provide us with what the error says?
Reply 4
Original post by UWS
Sorry please see edit, there is a space in there. Perhaps you can provide us with what the error says?


It's still not working :angry:

everyone.java:1: error: cannot find symbolclass everyone extends basic ^
symbol: class basiceveryone.java:4: error: cannot find symbol output out = new output(); ^ symbol: class output location: class everyoneeveryone.java:4: error: cannot find symbol output out = new output(); ^ symbol: class output location: class everyone3 errors
Oh yeah this is the qm lecturer's code that I've seen too! Back then it took a while for me too but:
-Basic and Output are objects whose class file should be on the same directory as your source code (.java file), those objects hide java library code (such as BufferedReader and BufferedWriter from the java.io library)
Just FYI, by convention, class names should begin with a capital letter. Also in Java most people put the first brace on the same line compared to C++ where people put it on the next line.

Posted from TSR Mobile
Reply 7
Original post by shawn_o1
Oh yeah this is the qm lecturer's code that I've seen too! Back then it took a while for me too but:
-Basic and Output are objects whose class file should be on the same directory as your source code (.java file), those objects hide java library code (such as BufferedReader and BufferedWriter from the java.io library)


So I need to import it? I'm still confused...
Original post by _howl
So I need to import it? I'm still confused...


Do you have two files called Basic.class and Output.class? In the same directory as your code?
Reply 9
Original post by shawn_o1
Do you have two files called Basic.class and Output.class? In the same directory as your code?


No, I don't. As you've said, this is something that should be in the same directory, but where do I get/move/copy it from?
Original post by _howl
No, I don't. As you've said, this is something that should be in the same directory, but where do I get/move/copy it from?


It should be from where you got the code as well, which is your course website?
Reply 11
Original post by shawn_o1
It should be from where you got the code as well, which is your course website?


Just very stupidly realised that I haven't downloaded all the code files... two of which include basic.class and output.class. *major facepalm*

Thanks for your help!
(edited 7 years ago)
Reply 12
How can I make my code case insensitive?

System.out.println("Okay, and would you describe yourself as an introvert or an extrovert?":wink:;
inex = scanner.next();
if (inex.contains("extrovert":wink:) { System.out.println("Interesting. You must enjoy being the centre of attention. What is your favourite colour?":wink:; }
else if (inex.contains("introvert":wink:) { System.out.println("Somewhat like me, I see. What is your favourite colour?":wink:; }
else { System.out.println("Well, we'll assume that you're somewhere in between then. What is your favourite colour?":wink:; }

If the front-end user typed in 'Extrovert' instead of 'extrovert', for example, the 'else' method would be run instead of the 'if' method. I don't want it to be case-sensitive.
(edited 7 years ago)
You have two options:
inex.toLowerCase().contains("extrovert");
inex.equalsIgnoreCase("extrovert");

Refer to the String documentation on docs.oracle.com to understand why both of them work
Reply 14
If I wanted to use a variable from a different method in the same class, how can I be able to do so?

For example,

public static void one ()
{
int totalage;

Scanner scanner = new Scanner (System.in);
totalage = Integer.parseInt(scanner.next());
System.out.println("Your age is " + totalage":wink:;
return;
}

public static void two ()
{
int average;
int score;

System.out.println("How old are you actually?":wink:;

Scanner scanner = new Scanner (system.in);
average = Integer.parseInt(scanner.next());

score = average + totalage;
System.out.println("Your score is " + score);
return;
}

Basically, how can I take the totalage from method one and use it in method two?
You can declare variables inside classes e.g. public class Foo
{
static int var1;
}


Then, any method you declare inside Foo can change the int variable var1.
Later on in your course you'll learn more about variable scope (public vs. private vs. protected), and instance variables (those without "static" when declared )
Original post by _howl
If I wanted to use a variable from a different method in the same class, how can I be able to do so?

For example,

public static void one ()
{
int totalage;

Scanner scanner = new Scanner (System.in);
totalage = Integer.parseInt(scanner.next());
System.out.println("Your age is " + totalage":wink:;
return;
}

public static void two ()
{
int average;
int score;

System.out.println("How old are you actually?":wink:;

Scanner scanner = new Scanner (system.in);
average = Integer.parseInt(scanner.next());

score = average + totalage;
System.out.println("Your score is " + score);
return;
}

Basically, how can I take the totalage from method one and use it in method two?


you can change your function one to an int and have it return totalAge so write it out like this:private int one () {
// evil code in here
}
for example you can return the total age in the function and call it when you want to assign it to something else:private int evilNumber (int x, int y) {
int total;
total = x + y;

return total;
}

public void evilFunction () {
int newTotal = evilNumber(5, 6);
System.out.println("the total is" + newTotal);
}
so it will return 11 when you call evilFunction.

Also as a side note you do not need to have the return; statement at the end of your functions, they don't do anything useful in this instance
Reply 17
Silly mistake - sorry.
(edited 7 years ago)
Reply 18
If I wanted to print the smallest value from my array, how would I do so?

For example, with the bold being the information taken from two different arrays, how would I print:

The most endangered animal is the Butterfly and there are only 18 left in the wild.
@JavaScriptMaster you may be needed here

Quick Reply

Latest

Trending

Trending