How to fix it ? i am very very new to java - java

package app;
import java.util.Scanner;
import java.util.UUID;
public class AccountThis {
private static Scanner scanner;
public static void main( String[] args ) {
//name input
scanner = new Scanner( System.in );
System.out.print( "Type your name: " );
String nameInput = scanner.nextLine();
System.out.println("Hello" + " " + nameInput);
//deposit input
scanner = new Scanner( System.in );
System.out.print( "Type how much you want to deposit:" );
double depositInput = scanner.nextDouble();
System.out.println("you want to deposit" + " " + depositInput + "$");
public AccountThis(String id) {
System.out.println("ID Generated"
+ "Please Write it down :" + id);
}
class RandomStringUUID {}
public id = randID;{
UUID randID = UUID.randomUUID();
UUID randomUUIDString = randID;
System.out.println(randomUUIDString );
new AccountThis( nameInput, depositInput, id);
}
{
}
}
public AccountThis() {
System.out.println( "created an account." );
}
public AccountThis( String nameInput ) {
System.out.println( " created account with name " + nameInput );
}
public AccountThis(double depositInput) {
System.out.println(depositInput + "$" + "added to your account!" );
}
}
i am new to java and i am trying to get id and assign it to the console with some text as you can see i failed a little bit if you can help me figure it out i would be very very happy
btw i called it id cause i set on it all night and couldn't figure it out.

Try this code. It is working fine
import java.util.Scanner;
import java.util.UUID;
public class AccountThis {
private static Scanner scanner;
public AccountThis(String nameInput) {
System.out.println(" created account with name " + nameInput);
}
public AccountThis() {
System.out.println("created an account.");
}
public AccountThis(double depositInput) {
System.out.println(depositInput + "$" + "added to your account!");
}
public AccountThis(String nameInput, double depositInput, String id) {
System.out.println(" created account with \n ID : " + id + ", \n name : " + nameInput);
System.out.println(depositInput + "$" + "added to your account!");
}
public static void main(String[] args) {
//name input
scanner = new Scanner(System.in);
System.out.print("Type your name: ");
String nameInput = scanner.nextLine();
System.out.println("Hello" + " " + nameInput);
//deposit input
scanner = new Scanner(System.in);
System.out.print("Type how much you want to deposit:");
double depositInput = scanner.nextDouble();
System.out.println("you want to deposit" + " " + depositInput + "$");
UUID randID = UUID.randomUUID();
String id=randID.toString();
new AccountThis(nameInput, depositInput,id);
}
}

Related

FileNotFoundException and invalid input in varying runs

I'm new to coding. I'm currently trying to get an input value and grab information from a csv file and format it into a song method (predefined) via four outputs. However, I'm not able to get to this point because every time I try to run it through it saids it is unable to load it. Either that or it automatally cuts to the default in the switch statement, which I don't understand. I'm completely lost right now.
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
/**
* #author chasestauts
*
*/
public class JukeboxHero {
public static void main(String args[]) {
final String MENU = "*****************************" + "\n" + "* Program Menu *" + "\n" + "*****************************"
+ "\n" + "(L)oad catalog" + "\n" + "(S)earch catalog" + "\n" + "(A)nalyse catalog" + "\n" + "(P)rint catalog" + "\n" + "(Q)uit"
+ "\n" + "\n";
final String cR = ("Please enter a command (press 'm' for Menu):");
System.out.print(MENU + cR);
Scanner input = new Scanner(System.in);
String decision = input.nextLine();
while(decision.equalsIgnoreCase("q") == false)
{
System.out.println(cR);
decision = input.nextLine();
switch (decision){
case ("L"):
{
ArrayList<Song> songList = new ArrayList<Song>();
System.out.println("Load Catalog..");
System.out.print("Please enter filename: ");
String filepath = input.nextLine();
File songFile = new File(filepath);
try
{
Scanner songScan = new Scanner(songFile);
while (songScan.hasNext())
{
String line = songScan.nextLine();
Scanner lineScan = new Scanner(line);
lineScan.useDelimiter(",");
while (lineScan.hasNext())
{
String artist = lineScan.next();
String album = lineScan.next();
String title = lineScan.next();
int duration = lineScan.nextInt();
Song song = new Song(title, artist, album, duration);
songList.add(song);
}
lineScan.close();
}
songScan.close();
int size = songList.size() + 1;
System.out.println("Successfully loaded " + size + " songs!");
}
catch (FileNotFoundException error)
{
System.out.println("Sorry, unable to open file: " + filepath);
}
}
break;
case ("S"):
{
System.out.println();
break;
}
case ("A"):
{
System.out.println();
break;
}
case ("P"):
{
System.out.println();
break;
}
case ("Q"):
{
System.out.println("Goodbye!");
break;
}
case ("M"):
{
System.out.println(MENU);
break;
}
default:
{
System.out.println("Invalid selection");
}
}
}
input.close();
}
}

How to make sure my method only asks for a file name one time and adds objects into it

I have a method in my super class that writes data into a file, and that method is overridden in my subclasses. Problem is at run time I get prompted three different times to create a new file. Here is my code:
public class Employee
{
protected String name;
protected String employeeNum;
protected String department;
protected char type;
protected BufferedWriter printer;
}
public void writeData() throws IOException
{
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the name of the new file for next week");
String newFile = sc.nextLine();
printer = new BufferedWriter(new FileWriter(newFile + ".txt"));
String data= getData();
printer.write(data);
}
protected String getData()
{
return name + " " + employeeNum + " " + department + " " + type;
}
public class Commission extends Employee
{
private int weeksStart;
private double baseWeeklySalary;
private double salesWeekly;
private double totalSales;
private double commissionRate;
}
#Override
protected String getData()
{
return name + " " + employeeNum + " " + department + " " + type + " " + weeksStart + " " + baseWeeklySalary + " " + salesWeekly + " " + totalSales + " " + commissionRate;
}
Assume all relevent construcors are there. I have two other subclasses; Hourly, and Salary that trys to implement the same writeData() method. When I iterate through my ArrayList of employees and try to writeData(),
It creates a new file for every type of employee when I need it to create just one
Public class PayRoll
{
Private ArrayList < Employee > employees;
public class PayRoll()
{
employees = new ArrayList Employee > ();
}
public void endOfDay()
{
for (int I = 0: i < employees.size (): i++)
{
employees.get(i).writeData():
}
}
}
I'd break this method in to two parts. One part requests a file name from the user, opens it, etc. This part doesn't need to be overridden. The only part that needs to be overridden is the part that generates the data string, which can be placed in a method of its own:
public class Employee {
/* Data members, omitted for brevity's sake */
public void writeData() throws IOException {
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the name of the new file");
String newFile = sc.nextLine();
printer = new BufferedWriter(new FileWriter(newFile + ".txt"));
String data = getData();
printer.write(data);
}
protected String getData() {
return name + " " + employeeNum + " " + department + " " + type;
}
}
public class Commission extends Employee {
/* Data members, omitted for brevity's sake */
#Override
protected String getData() {
return name + " " + employeeNum + " " + department + " " + type + " " + weeksStart + " " + baseWeeklySalary + " " + salesWeekly + " " + totalSales + " " + commissionRate;
}
}
Add a static variable isFileCreated, set it to true after creating file, and execute that block only if that variable is false.

Calling a class from same package and assigning variables

I'm trying to pull the RestaurantSelector1 class into CuisineChoice so that I can be able to have users input choices that I can then assign to the variables in RestaurantSelector1(if that makes sense. This is what I have:
In RestaurantSelector1:
public class RestaurantSelector1 {
public static String getRandom(List<String[]> list) {
Random random = new Random();
int listAccess = random.nextInt(list.size());
String[] s = list.get(listAccess);
return s[random.nextInt(s.length)];
}
public static void main(String[] args){
ArrayList<String[]> WestVillageCuisine = new ArrayList<String[]>();
WestVillageCuisine.add(Expensive.WestVillage.italian);
.
.
.
WestVillageCuisine.add(Expensive.WestVillage.greek);
final String[] randomCuisine = WestVillageCuisine.get(random.nextInt(WestVillageCuisine.size()));
final String randomRestaurant = randomCuisine[random.nextInt(randomCuisine.length)];
final String[] randomWVItalian = WestVillageCuisine.get(0);
final String westVillageItalian = r randomWVItalian[random.nextInt(randomWVItalian.length)];
System.out.println(getRandom(WestVillageCuisine));
System.out.println(randomRestaurant);
System.out.println(westVillageItalian);
}
And this is Cuisine Choice class:
class CuisineChoice{
public static void main(String[] args){
RestaurantSelector1 A = new RestaurantSelector1();
System.out.println("What price range are you looking for? one = $-$$ and two = $$$-$$$$");
Scanner scan2 = new Scanner(System.in);
String price = scan2.next();
if (price.equals("two")){
price = "Expensive";
System.out.println("In which neighborhood would you like to eat?");
Scanner scan = new Scanner(System.in);
String location = scan.next();
if (null != location)switch (location) {
case "Tribeca":{
System.out.println("Great! And what type cuisine would you like in " + location + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location + " " + type + " " + price);
break;
}
case "West Village":{
System.out.println("Great! And what type cuisine would you like in " + location + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location + " " + type + " " + price);
break;
}
case "Flatiron":{
System.out.println("Great! And what type cuisine would you like in " + location + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location + " " + type + " " + price);
break;
}
case "Chelsea":{
System.out.println("Great! And what type cuisine would you like in " + location + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location + " " + type + " " + price);
break;
}
default:
break;
}
}
else{
price= "Inexpensive"; System.out.println("In which neighborhood would you like to eat?");
Scanner scan3 = new Scanner(System.in);
String location1 = scan3.next();
if (null != location1)switch (location1) {
case "Tribeca":{
System.out.println("Great! And what type cuisine would you like in " + location1 + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location1 + " " + type + " " + price);
break;
}
case "West Village":{
System.out.println("Great! And what type cuisine would you like in " + location1 + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location1 + " " + type + " " + price);
break;
}
case "Flatiron":{
System.out.println("Great! And what type cuisine would you like in " + location1 + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location1 + " " + type + " " + price);
break;
}
case "Chelsea":{
System.out.println("Great! And what type cuisine would you like in " + location1 + " ?");
Scanner scan1 = new Scanner(System.in);
String type = scan1.next();
System.out.print(location1 + " " + type + " " + price);
break;
}
default:
break;
}
}
}
}
I just can't figure out how to use the user inputs of location and cuisine types as inputs to get a random restaurant of that cuisine in that location. I'm having trouble even getting the RestaurantSelector1 class to show up in CuisineChoice at all.
Any help would be much appreciated!
Both have main method, which one is the start point of the application, and what is the structure of your application, are they in the same package or different package? and you have some typo in code like if statement, it doesn't have "{" to open the statement, and in the case statement you don't need "{}".
if (null != location)switch (location) {
It should be:
if (null != location){ switch (location) {

Compiler says cannot find symbol [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 3 years ago.
Compiler throws an exception saying:
Cannot find symbol p and x
This program takes user input and prints as per the loop condition. While compiling it throws an error that it cannot find the symbol.
import java.util.Scanner;
class Puppy{
String name;
String breed;
String gender;
int weight;
int age;
int loud;
void hammer()
{
System.out.println("Enter your Dogs name :");
p[x].name = user_input.next();
System.out.println("Enter your Dogs Breed :");
p[x].breed = user_input.next();
System.out.println("Enter your Dogs Gender :");
p[x].gender = user_input.next();
System.out.println("Enter your Dogs weight in Kg:");
p[x].weight = user_input.nextInt();
System.out.println("Enter your Dogs age :");
p[x].age = user_input.nextInt();
System.out.println("Rate your Dogs Loudness out of 10 :");
p[x].loud = user_input.nextInt();
}
void jammer()
{
System.out.println("Hello my name is" + " " + p[x].name);
System.out.println("I am a" + " " + p[x].breed);
System.out.println("I am" + " " + p[x].age + " " + "years old" + " " + p[x].gender);
System.out.println("I weigh around" + " " + p[x].weight + " " + "kg's");
System.out.println("I sound this loud" + " " + p[x].loud);
}
}
class PuppyTestDrive
{
public static void main(String []args)
{
Scanner user_input = new Scanner(System.in);
int x = 0;
Puppy[] p = new Puppy[4];
while(x < 4)
{
p[x] = new Puppy();
p[x].hammer();
p[x].jammer();
x = x + 1;
}
}
}
Your program should work with the following code:
import java.util.Scanner;
class Puppy{
String name;
String breed;
String gender;
int weight;
int age;
int loud;
void hammer()
{
Scanner user_input = new Scanner(System.in);
System.out.println("Enter your Dogs name :");
this.name = user_input.next();
System.out.println("Enter your Dogs Breed :");
this.breed = user_input.next();
System.out.println("Enter your Dogs Gender :");
this.gender = user_input.next();
System.out.println("Enter your Dogs weight in Kg:");
this.weight = user_input.nextInt();
System.out.println("Enter your Dogs age :");
this.age = user_input.nextInt();
System.out.println("Rate your Dogs Loudness out of 10 :");
this.loud = user_input.nextInt();
}
void jammer()
{
System.out.println("Hello my name is" + " " + this.name);
System.out.println("I am a" + " " + this.breed);
System.out.println("I am" + " " + this.age + " " + "years old" + " " + this.gender);
System.out.println("I weigh around" + " " + this.weight + " " + "kg's");
System.out.println("I sound this loud" + " " + this.loud);
}
}
class PuppyTestDrive {
public static void main(String []args)
{
int x = 0;
Puppy[] p = new Puppy[4];
while(x < 4)
{
p[x] = new Puppy();
p[x].hammer();
p[x].jammer();
x = x + 1;
}
}
}

How can I use a while to continuously ask for input from a user and exit the program when "quit" is typed without using system.exit()?

I am currently taking an AP Computer Science class in my school and I ran into a little trouble with one of my projects! The project requires me to create a calculator that can evaluate an expression and then solve it. I have got most of that down, but I ran into a little trouble because my teacher asked me to use a while loop to continuously ask for input and display the answer, and I am stuck on that. To end the program the user has to type in "quit" and I can't use system.exit() or any cheating thing like that, the program has to just run out of code. Does anyone have any tips?
import java.util.*;
public class Calculator {
public static void main(String[] args) {
System.out.println("Welcome to the AP Computer Science calculator!!");
System.out.println();
System.out.println("Please use the following format in your expressions: (double)(space)(+,-,*,/...)(space)(double)");
System.out.println("or: (symbol)(space)(double)");
System.out.println();
next();
}
public static void next() {
Scanner kb = new Scanner(System.in);
System.out.print("Enter an expression, or quit to exit: ");
String expression = kb.nextLine();
next3(expression);
}
public static void next3(String expression) {
while (!expression.equals("quit")) {
next2(expression);
next();
}
}
public static void next2(String expression) {
if (OperatorFor2OperandExpressions(expression).equals("+")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) + SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("*")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) * SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("-")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) - SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("/")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) / SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor2OperandExpressions(expression).equals("^")) {
System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + Math.pow(FirstOperandFor2OperandExpressions(expression),SecondOperandFor2OperandExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("|")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.abs(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("v")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sqrt(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("~")) {
double x = 0.0;
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + (Math.round(OperandFor1OperatorExpressions(expression))+ x));
}
else if (OperatorFor1OperandExpressions(expression).equals("s")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sin(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("c")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.cos(OperandFor1OperatorExpressions(expression)));
}
else if (OperatorFor1OperandExpressions(expression).equals("t")) {
System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.tan(OperandFor1OperatorExpressions(expression)));
}
}
public static double FirstOperandFor2OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[0];
double y = Double.parseDouble(OperandOrOperator);
return y;
}
public static double SecondOperandFor2OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[2];
double y = Double.parseDouble(OperandOrOperator);
return y;
}
public static String OperatorFor2OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[1];
return OperandOrOperator;
}
public static String OperatorFor1OperandExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[0];
return OperandOrOperator;
}
public static double OperandFor1OperatorExpressions(String expression) {
String[] tokens = expression.split(" ");
String OperandOrOperator = tokens[1];
double y = Double.parseDouble(OperandOrOperator);
return y;
}
public static boolean QuitFunction(String expression) {
if (expression.equalsIgnoreCase("quit")) {
System.out.println("Goodbye!");
return false;
}
else {
return true;
}
}
}
Take a look at this code. I think this might help you in the right direction. It's similar to what you have already written except it eliminates the need for method calls in your while loop.
Scanner input = new Scanner(System.in);
while (!input.hasNext("quit")) {
String expression = input.nextLine(); // gets the next line from the Scanner
next2(expression); // process the input
}
// once the value "quit" has been entered, the while loop terminates
System.out.println("Goodbye");
Writing it this way drastically cleans up your code and prevents a new declaration of Scanner kb = new Scanner(System.in); each time an input is processed.

Categories