I'm trying to use a method to print another method - java

public static void main(String[] args) {
System.out.println("Hello and welcome! Please enter the following: ");
String q = null, s = "nul";
userName(q);
userGender(s);
print(userName(q));
print(userGender(s)); // how to achieve something like this?
}
public static void userName(String x) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String n = sc.nextLine();
}
public static void userGender(String y) {
Scanner sd = new Scanner(System.in);
System.out.print("Enter Gender: ");
String v = sd.next().toString();
}
public static void print(String a) {
System.out.println(a);
}
So I was trying to make it so that a method would be used to print another method after they were done executing but I couldn't get the desired result and it gave an error.

The method print works fine, it takes a String and return nothing
public static void print(String a)
{
System.out.println(a);
}
However, your method userGender and userName returns nothing, so when you are feeding print with a method that isn't returning a string, it will produce an compile-time error. You want to do something similar to:
public static String userGender(String y){
Scanner sd = new Scanner(System.in);
System.out.print("Enter Gender: ");
return sd.next().toString();
}
I haven't tested it, as your logic is unclear to me, but this is probably why your IDE is complaining.

Your method needs to return something. You are declaring your method like this: public static void userGender(String y) the void means that your method won't return anything. But since you want that the method returns a String you need to tell this in the method signature.
Your code could look like this:
public static void main(String[] args) {
System.out.println("Hello and welcome! Please enter the following: ");
print(userName());
print(userGender());
}
public static String userName() {
try(Scanner sc = new Scanner(System.in)){ // this is try resource see which will close your resource once you are done in the try block see https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
System.out.print("Enter name: ");
return sc.next();
}
}
public static String userGender() {
try(Scanner sc = new Scanner(System.in)) {
System.out.print("Enter Gender: ");
return sc.next();
}
}
public static void print(String a) {
System.out.println(a);
}
You don't need to use toString() since the next returns already a String. Also you can use the same variable name in different methods. And really important you need to close the Scanner again, otherwise it will consume endless resources.

Like #reebow and #Kerry Gougeon both pointed out that your method is looking to return something so you make it public static String userName() or public static String userName(String s).
If you're wanting to user Scanner then you're going to have to declare Scanner globally, otherwise it will throw a NoSuchElementExcpetion
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Hello and welcome! Please enter the following: ");
print(userName());
print(userGender());
}
public static String userName() {
System.out.print("Enter name: ");
return sc.next();
}
public static String userGender() {
System.out.print("Enter Gender: ");
return sc.next();
}
public static void print(String a) {
System.out.println(a);
}
If you're not using Scanner then you can just return the String that you passed to the method.
public static void main(String[] args) {
System.out.println("Hello and welcome! Please enter the following: ");
String a = null, b ="nul";
print(userName(a));
print(userGender(b));
}
public static String userName(String a) {
System.out.print("Enter name: ");
return a;
}
public static String userGender(String b) {
System.out.print("Enter Gender: ");
return b;
}
public static void print(String a) {
System.out.println(a);
}

Related

Is there any public/global variable in java?

I'm writing a program that uses java.util.Scanner to take user input. This will be used in multiple methods...
Is it possible to..
public class Main {
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
...
String mainInput = input.nextLine();
...
}
private static void add(){
...
String addInput = input.nextLine();
...
}
}
Or will I have to have Scanner input = new Scanner(System.in); In both
// methods.
public class Main {
public static void main(String[] args) {
...
Scanner input = new Scanner(System.in);
String mainInput = input.nextLine();
...
}
private static void add(){
...
Scanner input = new Scanner(System.in);
String addInput = input.nextLine();
...
}
}
I plan to have this in 3-4 other methods.
EDIT:
In my program I followed the first. I receive an error: Non-static field ‘input’ cannot be referenced from a static context
ANSWERED!
static Scanner input = new Scanner(System.in);
If you read the input right away the first method would be my way to go.

Is there a creative way to pass in multiple arguments to contentEquals() method?

As I understand, the contentEquals() method only accepts one argument to be compared with.
In the following program, what if I wanted to pass in more?
like:
(1)YES
(2)Yes
(3)Y
(4)y
import java.util.Scanner;
public class ifStatement1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Want some pizza?");
String userInput = input.nextLine();
boolean answer = **userInput.contentEquals("yes");**
if(answer) {
System.out.println("so go take a break from all this code");
}
else {
System.out.println("so keep writing code");
}
}
}
Set.of("YES", "Yes", "Y", "y").contains(userInput)
String.equals would be more normal than using String.contentEquals with a String.
import java.util.Scanner;
import java.util.Set;
public class example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String userInput = scanner.nextLine();
boolean answer;
if(Set.of("yes","y").contains(userInput.toLowerCase())){
answer = true;
} else {
answer = false;
}
System.out.print(answer);
}
}

Java string array with setter getter

I wanted to create a simple program for user to insert 3 strings to a private string array in a class and then print it back by creating a new object using object reference but I think I am facing problem in the setter/getter.(Pretty new to class and setter/getter) Here is what I have so far:
import java.util.Scanner;
public class Stringtest {
public static void main(String[] args)
{ Scanner input=new Scanner(System.in);
Stringer Strung=new Stringer();
System.out.println("Strings:"+Strung.print());
}
}
class Stringer
{ Scanner input=new Scanner(System.in);
private String[] aa=new String[3];
aa[0]="zero";
aa[1]="one";
aa[2]="two";
Stringer()
{}
{ System.out.println("Please enter 3 strings:");
for(int i=0;i<4;i++)
{
aa[i]=input.next();
}
}
public void setaa(String[] a)
{
aa=a;
}
public String[] getaa()
{
return aa;
}
public void print(String[] a)
{
for(int b=0;b<4;b++)
{
System.out.printf("%s",a[b]);
}
}
}
Due to populating the array while creating a class instance, you don't require any setters. The only getter requires.
Divide the logic from the runner.
Always use array.length() while looping or use a simple for loop otherwise you'll be getting an indexOfBoudException error.
Didn't get why you are using printf() while printing results.
My solution:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
App.run();
}
private static void run() {
Stringer stringer = new Stringer();
stringer.print(stringer.getStrings());
}
}
class Stringer {
private String[] strings = new String[3];
Stringer() {
System.out.println("Please enter 3 strings:");
for (int i = 0; i < 4; i++) {
Scanner scanner = new Scanner(System.in);
strings[i] = scanner.next();
}
}
String[] getStrings() {
return strings;
}
void print(String[] strings) {
System.out.println("Strings are:");
for (String string : strings) {
System.out.println(string);
}
}
}

Java looping 3 times before giving correct output

I'm making an application that quizzes you on politics or astronomy.
My problem is that when you say "politics" or you say "astronomy", it will ask you again 2 more times for your input, before giving the desired output of "test".
Here's the code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
do {
if (getAnswer().equalsIgnoreCase("neither")) {
System.out.println("Please enter \'astronomy\' or \'politics\'.");
}
getAnswer();
}
while(getAnswer().equalsIgnoreCase("neither"));
System.out.println("test");
}
public static String getAnswer() {
Scanner quizType = new Scanner(System.in);
System.out.println("Would you like to be quizzed on politics or astronomy?");
String typeAnswer = quizType.next();
if (typeAnswer.equalsIgnoreCase("politics")) {
return "politics";
}
else if (typeAnswer.equalsIgnoreCase("astronomy")) {
return "astronomy";
}
else {
return "neither";
}
}
}
Any ideas?
Thanks
There no need to getAnswer() 3 times, just getAnswer() into a String variable and you are good to go.
Like this:
public static void main(String[] args) {
String answer = "";
do {
answer = getAnswer();
if (answer.equalsIgnoreCase("neither")) {
System.out.println("Please enter \'astronomy\' or \'politics\'.");
}
} while (answer.equalsIgnoreCase("neither"));
System.out.println("test");
}
public static String getAnswer() {
Scanner quizType = new Scanner(System.in);
System.out.println("Would you like to be quizzed on politics or astronomy?");
String typeAnswer = quizType.next();
if (typeAnswer.equalsIgnoreCase("politics")) {
return "politics";
} else if (typeAnswer.equalsIgnoreCase("astronomy")) {
return "astronomy";
} else {
return "neither";
}
}
If you're learning do-while, then you only need to prompt inside your do. Currently you're calling getAnswer three times, which forces the repeated prompt.
Here is a quick way to solve it using do-while
public static void main(String[] args) {
List<String> validAnswers = Arrays.asList("neither","politics","astronomy");
String answer;
do {
answer = promptForAnswer();
} while(!validAnswers.contains(answer));
System.out.println("test");
}
public static String promptForAnswer() {
System.out.println("Would you like to be quizzed on politics or astronomy?");
return new Scanner(System.in).next();
}
Or you can go with the while loop...
public static void main(String[] args) {
List<String> validAnswers = Arrays.asList("neither","politics","astronomy");
while(!validAnswers.contains(promptForAnswer())) {
System.out.println("That was not a valid response, try again!");
}
System.out.println("test");
}
public static String promptForAnswer() {
System.out.println("Would you like to be quizzed on politics or astronomy?");
return new Scanner(System.in).next();
}

Identifier expected, Java

I'm trying to debug this simple application for an assignment, but I'm not sure why it won't compile...
import java.util.Scanner;
public class DebugThree3
{
public static void main(String[] args)
{
String name;
getName();
displayGreeting(name);
}
public String getName(name)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
return name;
}
public static void displayGreeting()
{
System.out.println("Hello, " + name + "!");
}
}
Attempting to compile tells me an identifier is expected on line 12, public String getName(name). Can anyone tell me what I'm missing here?
Thanks
You need to specify the Type of the parameter that a method accepts
public String getName(String name)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
return name;
}
Pass a parameter to the getname function in the main function.
Like SURESH pointed out, the getname function needs to be marked as static. Else, you will need to create an object of the class before you access the methods in it.
I took the liberty to fix your program in two standard ways of doing what you're trying to do.
Currently you're not passing name and you probably assume it exists in scopes in which it does not.
I also moved the redundant put-something-in-a-parameter-and-return behaviour in:
String name = input.nextLine()
return name;
And only left:
return input.nextLine();
First way is passing name between functions as a parameter, specifying its type:
import java.util.Scanner;
public class DebugThree3
{
public static void main(String[] args)
{
String name = getName();
displayGreeting(name);
}
public String getName()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
return input.nextLine();
}
public static void displayGreeting(String name)
{
System.out.println("Hello, " + name + "!");
}
}
Second way is having name as a static class member:
import java.util.Scanner;
public class DebugThree3
{
String name;
public static void main(String[] args)
{
getName();
displayGreeting();
}
public String getName()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
DebugThree3.name = input.nextLine();
}
public static void displayGreeting()
{
System.out.println("Hello, " + DebugThree3.name + "!");
}
}

Categories