Java homework help

Discussion in 'Programming General' started by MohtasaUnique, Jan 20, 2013.

Thread Status:
Not open for further replies.
Java homework help
  1. Unread #1 - Jan 20, 2013 at 9:08 PM
  2. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java homework help

    So bare with me, I'm just now learning java, and I literally know nothing about it. I have a homework problem where I'm supposed to program some stupid DJ Name generator

    I have this code: http://pastebin.com/cRwrMZNt

    And the problem is, if I use Scanner, lines 7 and 10 give an error because non-static variable scan cannot be referenced from a static context. If I use static Scanner, lines 8 and 11 give an error because non-static method substring(int,int) cannot be referenced from a static context. Or something like that, idk


    It was probably stupid to try, but I also tried importing both a static and non-static scanner, and that didn't solve the issue...


    How do I fix this?
     
  3. Unread #2 - Jan 20, 2013 at 9:57 PM
  4. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Java homework help

    http://pastie.org/5748784

    That should work.

    You don't do String.substring(), you use it like this :

    Code:
    String penis = "yourgay";
    penis.substring(1, 3);
    
     
  5. Unread #3 - Jan 20, 2013 at 10:07 PM
  6. Clashfan
    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Java homework help

    What iJava said, and put your scanner inside your main class, or make it static
    Code:
    static Scanner scan = new Scanner(System.in);
     
  7. Unread #4 - Jan 20, 2013 at 10:07 PM
  8. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java homework help

    I'm still getting a shitton of errors.

    http://pastebin.com/P2rLhFZ5

    But maybe it's because of the internet compiler...... I'm using compilr.com, maybe it sucks ass?

    I never had a problem like this when I was working with jGrasp, and SuF and I worked a lot with scanners.
     
  9. Unread #5 - Jan 20, 2013 at 10:09 PM
  10. Clashfan
    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Java homework help

    Paste what your code is now
     
  11. Unread #6 - Jan 20, 2013 at 10:17 PM
  12. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java homework help

  13. Unread #7 - Jan 20, 2013 at 10:18 PM
  14. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Java homework help

    i changed the class name to DJName.java couldn't help myself :p
     
  15. Unread #8 - Jan 20, 2013 at 10:20 PM
  16. Clashfan
    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Java homework help

    Code:
        public class djName
        {
               
                public static void main(String[] args)
                {
                        Scanner scan = new Scanner(System.in);
                        System.out.println("What is your first Name?");
                        String firstName = scan.nextLine();
                        String firstNamePrint = firstName.substring(0, firstName.length() / 2);
                        System.out.println("What is your last Name?");
                        String lastName = scan.nextLine();
                        String lastNamePrint = lastName.substring(lastName.length() / 2, lastName.length());
                        System.out.println("Your DJ Name is: " + firstNamePrint + lastNamePrint + "izzle");
                }
        }
    Try that, you had String.substring instead of firstName.substring, and your class name needs to be the same as your file name (assuming your file was still called djName.java)
     
  17. Unread #9 - Jan 20, 2013 at 10:29 PM
  18. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java homework help

    I've used your code and it entirely eliminated all of the static vs non-static variables.


    However, there's a new problem:

    http://pastebin.com/tNPiUehu
     
  19. Unread #10 - Jan 20, 2013 at 10:32 PM
  20. Clashfan
    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Java homework help

    Code:
        
        import java.util.Scanner;
        public class djName
        {
               
                public static void main(String[] args)
                {
                        Scanner scan = new Scanner(System.in);
                        System.out.println("What is your first Name?");
                        String firstName = scan.nextLine();
                        String firstNamePrint = firstName.substring(0, firstName.length() / 2);
                        System.out.println("What is your last Name?");
                        String lastName = scan.nextLine();
                        String lastNamePrint = lastName.substring(lastName.length() / 2, lastName.length());
                        System.out.println("Your DJ Name is: " + firstNamePrint + lastNamePrint + "izzle");
                }
        }
    You need to import scanner
     
  21. Unread #11 - Jan 20, 2013 at 10:37 PM
  22. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Java homework help

    You need to import scanner.
     
  23. Unread #12 - Jan 20, 2013 at 10:38 PM
  24. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java homework help

    I thought that's what Scanner scan = new Scanner(System.in); was.


    EDIT: NEVERMIND!! That's import java.util.Scanner;


    EDITEDIT: Gorgeous, it compiled with no errors. Thanks guys c:

    EDITEDITEDIT: new problem http://pastebin.com/0jvbYmEj
     
  25. Unread #13 - Jan 20, 2013 at 10:48 PM
  26. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Java homework help

    You need to change the main class name on the online site to the name of your class.
     
  27. Unread #14 - Jan 20, 2013 at 11:07 PM
  28. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java homework help

    I didn't have the class named djName.java, but it insisted on naming it "Program" so I changed it back to Program and it works perfectly. That's fine by me I guess, I'll change it back again when I copy the script over to my jGrasp program at home.

    Thanks for the help broskis. I'll reopen and bump this if I can't figure out the second homework program I'm supposed to make.

    Just finished the second program in under 5 minutes, I'M LEARNING! Thanks again for the help
     
< Looking for a Programmer // Simple Bank Program | Can't download Java >

Users viewing this thread
1 guest
Thread Status:
Not open for further replies.


 
 
Adblock breaks this site