0

I have fixed the errors in my original code and formatted it correctly. However, the code is now repeating String next() before it proceeds to the last method. I thought I understood why, but when I tried to fix it, the program failed again. Thank you for your time!

import java.util.Scanner;

public class LearnScanner {
    public static void main (String[] args) {
        first();
        next();
        third();
    }
    public static void first() {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to Vacation Planner!!");
        System.out.print("What is your name?");
        String name = input.nextLine();
        System.out.print("Nice to meet you " + name + ", where are you travelling to?");
        String destination = input.nextLine();
        System.out.println("Great! "+destination +" sounds like a great trip");
     }
    public static String next() {
        Scanner input = new Scanner(System.in);
        System.out.print("How many days are you going to spend travelling?");
        String days = input.nextLine();
        System.out.print("How much money in USD are you planning to spend on your trip?");
        String budget = input.nextLine();
        System.out.print("What is the three letter currency symbol for your travel destination?");
        String currency = input.nextLine();
        System.out.print("How many " + currency + " are there in 1 USD?");
        String currencyConversion = input.nextLine();
        return days;
    }
    public static void third() {
        int days = Integer.valueOf(next());
        int hours = days * 24;
        int minutes = hours * 60;
        System.out.println("If your are travelling for " + days + " days that is the same as " + hours + " hours or " + minutes + " minutes");
    }


    }
Erik Brabo
  • 11
  • 4

1 Answers1

1

From what I see, the method next() gets called once from the main method, and then gets called again in third at

int days = Integer.valueOf(next());

Maybe you should create an instance variable called days and store the value of next() in it. Then use the value of the variable in the third() method. i.e.

import java.util.Scanner;

public class LearnScanner {
    private static int days = 0;

    public static void main (String[] args) {
        first();
        days = Integer.parseInt(next());
        third();
    }
    public static void first() {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to Vacation Planner!!");
        System.out.print("What is your name?");
        String name = input.nextLine();
        System.out.print("Nice to meet you " + name + ", where are you travelling to?");
        String destination = input.nextLine();
        System.out.println("Great! "+destination +" sounds like a great trip");
     }
    public static String next() {
        Scanner input = new Scanner(System.in);
        System.out.print("How many days are you going to spend travelling?");
        String days = input.nextLine();
        System.out.print("How much money in USD are you planning to spend on your trip?");
        String budget = input.nextLine();
        System.out.print("What is the three letter currency symbol for your travel destination?");
        String currency = input.nextLine();
        System.out.print("How many " + currency + " are there in 1 USD?");
        String currencyConversion = input.nextLine();
        return days;
    }
    public static void third() {
        int tempDays = Integer.valueOf(days);
        int hours = days * 24;
        int minutes = hours * 60;
        System.out.println("If your are travelling for " + tempDays + " days that is the same as " + hours + " hours or " + minutes + " minutes");
    }
jaletechs
  • 501
  • 1
  • 7
  • 19