Adblock breaks this site

Java homework help

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

Thread Status:
Not open for further replies.
  1. MohtasaUnique

    MohtasaUnique Grand Master
    Retired Global Moderator

    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235
    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?
     
  2. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    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);
    
     
  3. Clashfan

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User
    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);
     
  4. MohtasaUnique

    MohtasaUnique Grand Master
    Retired Global Moderator

    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235
    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.
     
  5. Clashfan

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User
    Java homework help

    Paste what your code is now
     
  6. MohtasaUnique

    MohtasaUnique Grand Master
    Retired Global Moderator

    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235
    Java homework help

  7. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java homework help

    i changed the class name to DJName.java couldn't help myself :p
     
  8. Clashfan

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User
    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)
     
  9. MohtasaUnique

    MohtasaUnique Grand Master
    Retired Global Moderator

    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235
    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
     
  10. Clashfan

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User
    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
     
  11. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java homework help

    You need to import scanner.
     
  12. MohtasaUnique

    MohtasaUnique Grand Master
    Retired Global Moderator

    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235
    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
     
  13. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java homework help

    You need to change the main class name on the online site to the name of your class.
     
  14. MohtasaUnique

    MohtasaUnique Grand Master
    Retired Global Moderator

    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235
    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 >
Thread Status:
Not open for further replies.


 
 
Adblock breaks this site