Jump to content

Java: Trying to Learn Static Methods

Tocsin_786

I'm trying to better understand static methods by coding testing out code. In this case I want to print last name first... Ex: "Gates, Bill". Can't seem to figure out what I'm doing wrong. 

 

  public class lastNameTest {
  
  public static String lastFirst(String first, String last) {  
    first = Paul;
    last = Walker;
    System.out.println(last + ", " + first);
  }
  
  public static void main(String [] args) {
    
    lastFirst();
  }
  }

 

Space Journal #1: So Apparently i  was dropped on the moon like i'm a mars rover, in a matter of hours i have found the transformers on the dark side of the moon. Turns out its not that dark since dem robots are filled with lights, i waved hi to the Russians on the space station, turns out all those stories about space finding humans instead of the other way around is true(soviet Russia joke). They threw me some Heineken beer and I've been sitting staring at the people of this forum and earth since. 

Link to comment
Share on other sites

Link to post
Share on other sites

You cant declare a variable without telling it what data type it is, plus you're missing quotes

Your method declaration says you return a string, but you don't: you just print one

 

In your method call in main, you don't give it any parameters- but that wouldnt matter because you have hard coded values in there anyway

 

Buuut even though they're hard coded, you have the same variable names for your parameters as you do in the body. Not that this necessarily causes any issue but if you input "Bill" and "Gates" for your params, itll still pop out "Walker, Paul"

 

 

In short, you have a lot of combative information in here

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Slottr said:

You cant declare a variable without telling it what data type it is, plus you're missing quotes

Your method declaration says you return a string, but you don't: you just print one

 

In your method call in main, you don't give it any parameters- but that wouldnt matter because you have hard coded values in there anyway

 

Buuut even though they're hard coded, you have the same variable names for your parameters as you do in the body. Not that this necessarily causes any issue but if you input "Bill" and "Gates" for your params, itll still pop out "Walker, Paul"

 

 

In short, you have a lot of combative information in here

I'm super confused by this. my very first test was a user input, compiled correctly but didn't sprint anything out. So I wanted to hard code in some values and see if that would work, but it just made things much worse. 

import java.util.Scanner;
  public class lastNameTest {
  
  public static String lastFirst() { 
    Scanner sc = new Scanner(System.in);
    String First,Last;
    
    First = sc.next();
    Last = sc.next();
    return Last + ", " + First;
  }
  
  public static void main(String [] args) {
    
    lastFirst();
  }
  }

 

Space Journal #1: So Apparently i  was dropped on the moon like i'm a mars rover, in a matter of hours i have found the transformers on the dark side of the moon. Turns out its not that dark since dem robots are filled with lights, i waved hi to the Russians on the space station, turns out all those stories about space finding humans instead of the other way around is true(soviet Russia joke). They threw me some Heineken beer and I've been sitting staring at the people of this forum and earth since. 

Link to comment
Share on other sites

Link to post
Share on other sites

In this code you just posted, its not spitting anything out because you're not printing, you're only doing a method call and not doing anything with the return value.

 

 

Going back to your first code:

 

Your hard coded values were broken because you didn't format the data type correctly

You need to put quotes around strings.

 

such as:

String name = "Slottr";

 

Your method required  parameters, and in the method call in your main, you didnt have any

 

Eg:

 

public static String lastFirst(String first, String last) {  ... }



// AND 


lastFirst();

 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

49 minutes ago, Tocsin_786 said:

I'm super confused by this. my very first test was a user input, compiled correctly but didn't sprint anything out. 

What you have here is correct, you just never printed it. The way I learned to do it is set the method call of the lastFirst method to a string:

String word = lastFirst();

You can then print this string:

System.out.println(word);

 

Alternatively, you can just print it in one line using

System.out.println(lastFirst());

 

I am far from an expert in this so please correct me if I’m wrong.

Quote or tag me so I can see your response

 

PSU Tier List

Motherboard Tier List

Graphics Card Cooling Tier List

CPU Cooler Tier List

SSD Tier List

 

PARROT GANG

Mentioned in 7/10/20 WAN Show

Mentioned in 7/15/20 Techlinked

Mentioned in 7/17/20 Techlinked

Mentioned in 7/31/20 WAN Show

Mentioned in 7/31/20 Techlinked

Mentioned in 8/3/20 Techlinked

Mentioned twice in 8/5/20 Techlinked

Mentioned twice in 8/7/20 Techlinked

Mentioned in 8/12/20 Techlinked

Mentioned in 8/19/20 Techlinked

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Slottr said:

In this code you just posted, its not spitting anything out because you're not printing, you're only doing a method call and not doing anything with the return value.

 

 

Going back to your first code:

 

Your hard coded values were broken because you didn't format the data type correctly

You need to put quotes around strings.

 

such as:


String name = "Slottr";

 

Your method required  parameters, and in the method call in your main, you didnt have any

 

Eg:

 


public static String lastFirst(String first, String last) {  ... }



// AND 


lastFirst();

 

that finally makes some sense. This was related to my class im taking and unfortunately all my hw is due on Codelab which doesnt explain anything like you just did. Thanks!

Space Journal #1: So Apparently i  was dropped on the moon like i'm a mars rover, in a matter of hours i have found the transformers on the dark side of the moon. Turns out its not that dark since dem robots are filled with lights, i waved hi to the Russians on the space station, turns out all those stories about space finding humans instead of the other way around is true(soviet Russia joke). They threw me some Heineken beer and I've been sitting staring at the people of this forum and earth since. 

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, Tocsin_786 said:

that finally makes some sense. This was related to my class im taking and unfortunately all my hw is due on Codelab which doesnt explain anything like you just did. Thanks!

Do your best to work through it. Best way to learn the syntax and principles will be to trial and error till you're dead

 

Source: Am doing that right now

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×