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

Announcements Posted on
    • Thread Starter
    Offline

    2
    ReputationRep:
    New question in post #23.

    Apologies for the lack of key terminology and the awkward pasting of code...
    Offline

    3
    ReputationRep:
    It should be this..
    Code:
    output out = new output();
    You put in a space between the method name and the opening bracket
    • Thread Starter
    Offline

    2
    ReputationRep:
    (Original post by UWS)
    Code:
    output out = new output();
    That is what I've done, though (?)
    Offline

    3
    ReputationRep:
    (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?
    • Thread Starter
    Offline

    2
    ReputationRep:
    (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

    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
    Online

    3
    ReputationRep:
    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)
    Online

    3
    ReputationRep:
    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
    • Thread Starter
    Offline

    2
    ReputationRep:
    (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...
    Online

    3
    ReputationRep:
    (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?
    • Thread Starter
    Offline

    2
    ReputationRep:
    (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?
    Online

    3
    ReputationRep:
    (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?
    • Thread Starter
    Offline

    2
    ReputationRep:
    (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!
    • Thread Starter
    Offline

    2
    ReputationRep:
    How can I make my code case insensitive?

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

    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.
    Online

    3
    ReputationRep:
    You have two options:
    Code:
    inex.toLowerCase().contains("extrovert"); 
    inex.equalsIgnoreCase("extrovert");
    Refer to the String documentation on docs.oracle.com to understand why both of them work
    • Thread Starter
    Offline

    2
    ReputationRep:
    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";
    return;
    }

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

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

    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?
    Online

    3
    ReputationRep:
    You can declare variables inside classes e.g.
    Code:
    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 )
    Offline

    2
    ReputationRep:
    (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";
    return;
    }

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

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

    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:
    Code:
    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:
    Code:
    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
    • Thread Starter
    Offline

    2
    ReputationRep:
    Silly mistake - sorry.
    • Thread Starter
    Offline

    2
    ReputationRep:
    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.
    Online

    3
    ReputationRep:
    JavaScriptMaster you may be needed here
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: November 27, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Today on TSR
Poll
How are you feeling about doing A-levels?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.