-1

I copy and paste this code into my Intellij and the result came out with the system does not allow the user to answer(input) the question "What is the three-letter currency symbol of your destination?" and it directly skip to the next question. May i know what is the issues and what is the solutions? Thank you for your teaching first.

This code I get from the link for my practice and study for Java : How to fix the error <identifier> expected?

enter image description here

package com.Test3;

import java.util.Scanner;

public class Example1 {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        intro(input);
        time(input);
    }

    public static void intro(Scanner input) {
        System.out.println("Welcome");
        System.out.println("What is your name");

        String name = input.nextLine();
        System.out.println("Nice to meet you, " + name + " where are you travelling to?");
        String dest = input.nextLine();
        System.out.println("Great! " + dest + " sounds like a great trip");
    }

    public static void time(Scanner input) {
        int hours, minutes;
        float perd, perdc, change;



        System.out.println("How many days are you going to spend travelling?");

        int days = input.nextInt();

        hours = days * 24;
        minutes = hours * 60;

        System.out.println("How much money in USD are you going to spend?");

        float money = input.nextFloat();
        perd = money / days;

        System.out.println("What is the three letter currency symbol of your destination?");

        String curr = input.nextLine();

        System.out.println("How many " + curr + " are there in 1USD?");

        float ex = input.nextFloat();
        change = money * ex;
        perdc = perd * ex;

        System.out.println("If you are travelling for " + days + " that is the same as " + hours + " or " + minutes + " minutes");
        System.out.println("If you are going to spend " + money + " $USD that means per day you can spend upto $" + perd + " USD");
        System.out.println("Your total budget in " + ex + " is " + change + ex + " ,which per day is  " + perdc + curr);
    }

}
Holger
  • 243,335
  • 30
  • 362
  • 661
Machine_Z
  • 19
  • 2

1 Answers1

2

Do not use nextLine. It does what the docs say it does, but it's not what you want, hence, 'broken'.

Instead, properly configure your scanner; after making it, invoke: scanner.useDelimiter("\\R"); (which tells the scanner that tokens are separated by newlines), and use .next() to read a line of text.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37