• Log in or sign up for an account here: https://www.sythe.org/login

Java homework help

Status
Not open for further replies.

MohtasaUnique

Grand Master

Retired Global Moderator
Joined
Sep 1, 2007
Sythe Gold
690
Messages
6,681
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?
 
PHP Shit Show 2k26
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);
 
Two Factor Authentication User
What iJava said, and put your scanner inside your main class, or make it static
Code:
static Scanner scan = new Scanner(System.in);
 
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.
 
PHP Shit Show 2k26
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.

i changed the class name to DJName.java couldn't help myself :p
 
Two Factor Authentication User
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)
 
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
 
Two Factor Authentication User
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
 
PHP Shit Show 2k26
You need to import scanner.
 
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
 
PHP Shit Show 2k26
You need to change the main class name on the online site to the name of your class.
 
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
 
Status
Not open for further replies.
Runescape Gamblingrunescape gambling
Back
Top