0

I'm working a project in my Java online course, and I've been forced to construct a program to give the desired output, without being given any clues to assist me. So the error is most definitely on my part, and not the machine's. Though extensive googling has led me to a dead end.

This is the sample output given to me by edX.

Java sample output

And here is the code I've devised so far, with the little knowledge I have.

import java.util.Scanner;
public class planner {

public static void main(String[] args) {
    System.out.println("Planner initialized.");
    System.out.println("Reading settings..");
    System.out.println("Assessing environment..");
    System.out.println("Gathering tools..");
    System.out.println("****************************************");
    System.out.println("");
    intro();
}

public static void intro() {
    System.out.println("Welcome to the vacation planner.");
    System.out.println("What is your name?");
    Scanner fn = new Scanner(System.in);
    Scanner ln = new Scanner(System.in);
    System.out.println("Welcome, " + fn + ln + ". Where are you traveling to?");
    Scanner loc1 = new Scanner(System.in);
    Scanner loc2 = new Scanner(System.in);
    System.out.println("Excellent. " + loc1 + loc2 + " it is.");
    System.out.println("*****************************************");
    System.out.println("");
    calc();
}

public static void calc() {
    System.out.println("How many days are you planning to spend away?");
    Scanner x = new Scanner(System.in);
    System.out.println("How much money, in U.S. dollars, are you planning to spend?");
    Scanner m = new Scanner(System.in);
    System.out.println("What is the three-letter currency symbol for your destination?(Google may be required)");
    Scanner i = new Scanner(System.in);
    System.out.println("How many " + i + " are there in USD? (Google may be required)");
    Scanner o = new Scanner(System.in);
    System.out.println("");


    float t = x.nextFloat();
    float r = m.nextFloat();
    float k = o.nextFloat();


    //To calculate the amount of hours
    float n = t * 24;
    //To calculate the amount of minutes
    float u = (t * 24) * 60;
    //To calculate daily budget
    float y = t / r;
    // To calculate currency exchange
    float e = k * r;
    //To calculate budget in alt currency
    float q = t / e;

    System.out.println("If you are travelling for " + t + " days, that is the same as " + n + " hours, or " + u + " minutes.");
    System.out.println("If you are going to spend " + r + " U.S. dollars per day, you can spend" + y + " dollars per day.");
    System.out.println("Your total budget in " + i + "is " + e + i + ", which, per day, is " + q + i);
    System.out.println("****************************************");
    System.out.println("");
 }
}

And the output I've been given:

/home/foo/jdk-9.0.4/bin/java -javaagent:/opt/intellij-idea-community/lib/idea_rt.jar=37671:/opt/intellij-idea-community/bin -Dfile.encoding=UTF-8 -classpath /home/samuel/IdeaProjects/Module-test/out/production/Module-test planner
Planner initialized.
Reading settings..
Assessing environment..
Gathering tools..
****************************************

Welcome to the vacation planner.
What is your name?
Welcome, java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]. Where are you traveling to?
Excellent. java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E] it is.
*****************************************

How many days are you planning to spend away?
How much money, in U.S. dollars, are you planning to spend?
What is the three-letter currency symbol for your destination?(Google may be required)
How many java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E] are there in USD? (Google may be required)

And I think it's the [need input=false] declaration that it gives me that's telling me the problem, but I don't understand what's wrong. Could I have some assistance?

  • 3
    You only ever want to construct one scanner to read from the console – GBlodgett Apr 24 '18 at 01:21
  • Ask yourself : what are you assigning to `fn` ? Is this the way the scanner works ? If yes, what makes you think it's the way ? If no, what does google think about the question ? – Yassin Hajaj Apr 24 '18 at 01:24
  • There are many existing posts here about retrieving user input using Scanner in Java. Please do some research and read a few of those posts to help you get this sorted out. Start [here](https://stackoverflow.com/q/17538182/62576) – Ken White Apr 24 '18 at 01:24
  • Just look at the [Javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html) for `Scanner`. This has all the info you need. – Java Devil Apr 24 '18 at 01:25
  • @YassinHajaj I understand the concept of Scanner. `fn` stands for **first name**, and is only ever used once, along with `ln` for **last name**. –  Apr 24 '18 at 14:21
  • @GBlodgett I've written one other program, and using multiple scanners has wrked for me. However, I have not ever divided my work into seperate **_methods_**. Could this be what you mean? –  Apr 24 '18 at 14:22
  • @KenWhite I've already browsed all the suggested posts, and browsed the ones google gave me. None of the ones I found help me any. however, the one you gave me reminded me to check my previous work, and I think I know what I did wrong. –  Apr 24 '18 at 14:23
  • @m-A_droid It is bad practice to construct more than one scanner to read from the console. As you saw from your program it creates a mess – GBlodgett Apr 24 '18 at 14:32

2 Answers2

0

I think the confusion has to do with this:
Scanner fn = new Scanner(System.in);
What this does is declare a scanner object called fn. If you want to read from System.in with that scanner object, you must use something like:
Scanner fn = new Scanner(System.in);
String answer = fn.nextLine();
Read here to learn more about scanner: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Toast
  • 1
0

If you use nextLine() String form Scanner and Float.parseFloat(String) it will be better

import java.util.*;
import java.lang.*;

public class Planner{

public static void main(String[] args) {
    System.out.println("Planner initialized.");
    System.out.println("Reading settings..");
    System.out.println("Assessing environment..");
    System.out.println("Gathering tools..");
    System.out.println("****************************************");
    System.out.println("");
    intro();
}

public static void intro() {
    Scanner fn = new Scanner(System.in);

    System.out.println("Welcome to the vacation planner.");
    System.out.println("What is your first name?");
    String name = fn.nextLine();
    System.out.println("What is your last name?");
    String lastname = fn.nextLine();
    System.out.println("Welcome, " + name + " " + lastname);
    System.out.println(". Where are you traveling to?");
    String traveling = fn.nextLine();
    System.out.println("Excellent. " + traveling + " it is.");
    System.out.println("*****************************************");
    System.out.println("");
    calc();
}

public static void calc() {
     Scanner scanner = new Scanner(System.in);

    System.out.println("How many days are you planning to spend away?");
    String x = scanner.nextLine();
    System.out.println("How much money, in U.S. dollars, are you planning to spend?");
    String m = scanner.nextLine();
    System.out.println("What is the three-letter currency symbol for your destination?(Google may be required)");
    String i = scanner.nextLine();
    System.out.println("How many " + i + " are there in USD? (Google may be required)");
    String o = scanner.nextLine();
    System.out.println("");


    float t = Float.parseFloat(x);
    float r = Float.parseFloat(m);
    float k = Float.parseFloat(o);


    //To calculate the amount of hours
    float n = t * 24;
    //To calculate the amount of minutes
    float u = (t * 24) * 60;
    //To calculate daily budget
    float y = t / r;
    // To calculate currency exchange
    float e = k * r;
    //To calculate budget in alt currency
    float q = t / e;

    System.out.println("If you are travelling for " + t + " days, that is the same as " + n + " hours, or " + u + " minutes.");
    System.out.println("If you are going to spend " + r + " U.S. dollars per day, you can spend" + y + " dollars per day.");
    System.out.println("Your total budget in " + i + "is " + e + i + ", which, per day, is " + q + i);
    System.out.println("****************************************");
    System.out.println("");
 }
}
Héctor M.
  • 2,302
  • 4
  • 17
  • 35
  • Thank you so much. After declaring `fn` a string with `String name = fn.nextLine()`, and letting the **Welcome** statement use the defined string instead of raw scanner input, my problem was solved. –  Apr 24 '18 at 14:31