ollyb342
04-18-2008, 12:14 PM
hey guys, i'm completely stumped!
I've started teaching myself Java this week and i can't work something out:
I have a method which accepts input from a user and calculates how much they'll earn in a week, it then stores the input into an array at the end they get the option to cycle back through the program, quit the program or view each of the records which have been stored into the array so far. my code works perfectly but i cannot understand how to make it store the input into the NEXT space in the array :S.
Here's my code so far:
import java.util.*;
public class sainsburys2 {
public static void main(String[] args) {
rateCalculator();
}
public static void rateCalculator() {
int x = 0;
String name;
int hours;
double pay;
double wage;
String answer;
String[] records;
records = new String[10];
Scanner in = new Scanner(System.in);
System.out.println("******************************************************************");
System.out.println("********* Welcome to your personal planner for the week **********");
System.out.println("******************************************************************");
System.out.println("Please enter your name to begin");
System.out.print(">");
name = in.next();
if (name.equalsIgnoreCase("Ollie")) {
System.out.println("\nhey Ollie, you made this program =]");
} else {
System.out.println("\nhello " + name.toUpperCase() + "\n");
}
System.out.println("\nPlease enter the amount of hours you will be working this week");
System.out.print(">");
hours = in.nextInt();
if (hours == 0) {
System.out.println("\nlazy goat, you should be shot!\n");
} else if(hours > 40) {
System.out.println("\nYou're working far too many hours!!\n");
}
System.out.println("\nOkay " + name + ", so you'll be working " + hours + " hours this week\n");
System.out.println("\nNow.. if you don't mind me asking, how much do you earn per hour?\n");
System.out.print("> $ ");
pay = in.nextDouble();
if (pay == 0) {
System.out.println("\nGet a different job, they're having a laugh\n");
} else if(pay >= 6) {
System.out.println("\nI wish i was earning as much as you =(\n");
}
System.out.println("\nOkay then, two seconds while i calculate how much you'll earn\n");
wage = pay * hours;
System.out.println("\nThis week you will be earning $" + wage);
System.out.println("\nwowzers!!\n");
records[x] = ("name: " + name.toUpperCase() + "\n hours: " + hours + "\n pay: $" + pay + "\n salary this week: $" + wage + "\n");
System.out.println(records[x]);
System.out.print("would you like to run through again? y or n please or r to view all records so far > ");
answer = in.next();
if (answer.equalsIgnoreCase("y")) {
System.out.println("n\n\n\n");
x++
rateCalculator();
} else if (answer.equalsIgnoreCase("n")) {
System.exit(1);
} else if (answer.equalsIgnoreCase("r")) {
for (int i = 0;i<records.length;i++) {
System.out.println(records[i]);
}
}
}
}
I realise that my code is probably stupidly convoluted and pointless but it's my first real chunk of Java, the problem is that if they press Y at the end, i need it to increase "x" by one to go to the next space in the array, but obviously as the program reiterates, x is set to 0 again which will overwrite what was stored in the array originally, i need to find a way of making x increase by one every time the program iterates so that if "r" is pressed at the end it'll print every record in the array out.
Please help guys!
Ollie.
I've started teaching myself Java this week and i can't work something out:
I have a method which accepts input from a user and calculates how much they'll earn in a week, it then stores the input into an array at the end they get the option to cycle back through the program, quit the program or view each of the records which have been stored into the array so far. my code works perfectly but i cannot understand how to make it store the input into the NEXT space in the array :S.
Here's my code so far:
import java.util.*;
public class sainsburys2 {
public static void main(String[] args) {
rateCalculator();
}
public static void rateCalculator() {
int x = 0;
String name;
int hours;
double pay;
double wage;
String answer;
String[] records;
records = new String[10];
Scanner in = new Scanner(System.in);
System.out.println("******************************************************************");
System.out.println("********* Welcome to your personal planner for the week **********");
System.out.println("******************************************************************");
System.out.println("Please enter your name to begin");
System.out.print(">");
name = in.next();
if (name.equalsIgnoreCase("Ollie")) {
System.out.println("\nhey Ollie, you made this program =]");
} else {
System.out.println("\nhello " + name.toUpperCase() + "\n");
}
System.out.println("\nPlease enter the amount of hours you will be working this week");
System.out.print(">");
hours = in.nextInt();
if (hours == 0) {
System.out.println("\nlazy goat, you should be shot!\n");
} else if(hours > 40) {
System.out.println("\nYou're working far too many hours!!\n");
}
System.out.println("\nOkay " + name + ", so you'll be working " + hours + " hours this week\n");
System.out.println("\nNow.. if you don't mind me asking, how much do you earn per hour?\n");
System.out.print("> $ ");
pay = in.nextDouble();
if (pay == 0) {
System.out.println("\nGet a different job, they're having a laugh\n");
} else if(pay >= 6) {
System.out.println("\nI wish i was earning as much as you =(\n");
}
System.out.println("\nOkay then, two seconds while i calculate how much you'll earn\n");
wage = pay * hours;
System.out.println("\nThis week you will be earning $" + wage);
System.out.println("\nwowzers!!\n");
records[x] = ("name: " + name.toUpperCase() + "\n hours: " + hours + "\n pay: $" + pay + "\n salary this week: $" + wage + "\n");
System.out.println(records[x]);
System.out.print("would you like to run through again? y or n please or r to view all records so far > ");
answer = in.next();
if (answer.equalsIgnoreCase("y")) {
System.out.println("n\n\n\n");
x++
rateCalculator();
} else if (answer.equalsIgnoreCase("n")) {
System.exit(1);
} else if (answer.equalsIgnoreCase("r")) {
for (int i = 0;i<records.length;i++) {
System.out.println(records[i]);
}
}
}
}
I realise that my code is probably stupidly convoluted and pointless but it's my first real chunk of Java, the problem is that if they press Y at the end, i need it to increase "x" by one to go to the next space in the array, but obviously as the program reiterates, x is set to 0 again which will overwrite what was stored in the array originally, i need to find a way of making x increase by one every time the program iterates so that if "r" is pressed at the end it'll print every record in the array out.
Please help guys!
Ollie.