Need help on an app I'm working on (JAVA)

Need help on an app I'm working on (JAVA)
0.0 0

#1

So currently I’m working on a doggy app to help me remember my dog’s appointments, potty breaks, medicines and just EVERYTHING that has to do with her. I’m sorry for the shitty code, I’m in the process of learning so I thought starting a project will be helpful for me.

**This is my issue here: **

**console: Please, enter your doggy’s name: **
user input: mollyt
console: You entered: mollyt. Is this correct?
user input: no
**console: Please, enter your doggy’s name: **
user input: You entered: . Is this correct?

package pottyBreaks;
import java.util.Scanner;

public class dogQuestions
{
public static void main(String[] args)
{

	Scanner input = new Scanner(System.in);
	
	System.out.println("Please, enter your doggy's name: "); 
	String dogName = input.nextLine();
	
	System.out.println("You entered: " +dogName+ ". Is this correct?"); 
	String dogNameFix = input.next();
	
	if(dogNameFix.equalsIgnoreCase("no"))  
                    {			
		while(dogNameFix.equalsIgnoreCase("no")) 
		{	
			System.out.println("Please, enter your doggy's name: "); 
			
				dogName = input.nextLine();
			
			System.out.println("You entered: " +dogName+ ". Is this correct?"); 
				dogNameFix = input.next();
                     }

                     }
	
	System.out.println("What is the age? "); 
	double dogAge = input.nextDouble();
	
	
	I wrote it so if the user typed the dog's name wrong, it goes back and lets you type it again. (and until you get it right, it'll just keep repeating and once you do, It moves on)

when I type in ‘no’, it repeats but the I write it correctly and say yes, it does this:

Please, enter your doggy’s name:
mollyt
You entered: mollyt. Is this correct?
no
Please, enter your doggy’s name:
You entered: . Is this correct?

Any help, advice, or tips on solving this issue and maybe on how to be a better code?
I would REALLY appreciate the feedback! thank you!


#2

My java is rusty, but hope this helps:

package pottyBreaks;
import java.util.Scanner;

class dogQuestions {
 public static void main(String[] args) {
  String dogName, userConfirm;
  int dogAge;

  Scanner input = new Scanner(System.in);

  do {
   System.out.println("Please, enter your doggy's name: ");
   dogName = input.next();

   System.out.println("You entered: " + dogName + ". Is this correct?");
   userConfirm = input.next();
  } while (userConfirm.equalsIgnoreCase("no"));

  System.out.println("What is the age? ");
  dogAge = input.nextInt();
  System.out.println("Saved " + dogName + ", age " + dogAge + "\nGoodbye");
 }
}

#3

Thank you! Worked perfectly and I was able to understand! :smiley:

I’m trying my hardest to get proficient in Java. I got to admit, it’s pretty hard


#4

Since this is Java, I went ahead and revised my previous code using the object-oriented approach, and made a few trivial changes:

package pottybreaks;

import java.util.Scanner;

class DogQuestions {
 public static void main(String[] args) {
  String nameConfirm;
  Dog dog = new Dog();

  Scanner input = new Scanner(System.in);

  do {
   System.out.println("Please, enter your doggy's name: ");
   dog.setName(input.next());

   System.out.println(String.format(
    "You entered: %s. Is this correct?", dog.getName()
   ));
   nameConfirm = input.next();
  } while (nameConfirm.equalsIgnoreCase("no"));

  System.out.println("What is the age? ");
  dog.setAge(input.nextInt());
  System.out.print(dog);
 }
}

class Dog {
 private String name;
 private int age;
 // EDIT: almost forgot to add the constructors
 //  default constructor
 Dog() {}
  // constructor
 Dog(String name, int age) {
  this.name = name;
  this.age = age;
 }

 public String getName() {
  return name;
 }
 public int getAge() {
  return age;
 }
 public void setName(String name) {
  this.name = name;
 }
 public void setAge(int age) {
  this.age = age;
 }

 @Override
 public String toString() {
  return String.format("Name: %s \nAge: %d", name, age);
 }
}

My Java is still rusty, but I think this is pretty good.