Results 1 to 17 of 17

Thread: Java Project

  1. #1
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Exclamation Java Project

    Need help with a java project I need to do. I've already posted what the project specifications are in a previous thread, but here it is again.

    """Create a console calculator applicaion that:
    * Takes one command line argument: your name and surname. When the
    program starts, display the date and time with a welcome message for the
    user.
    * Display all the available options to the user. Your calculator must include
    the arithmetic operations as well as at least five scientific operations of the
    Math class.
    -Your program must also have the ability to round a number and
    truncate it.
    -When you multiply by 2, you should not use the '*' operator to perform the
    operation.
    -Your program must also be able to reverse the sign of a number.
    * Include sufficient error checking in your program to ensure that the user
    only enters valid input. Make use of the String; Character, and other
    wrapper classes to help you.
    * Your program must be able to do conversions between decimal, octal and
    hex numbers.
    * Make use of a menu. You should give the user the option to end the
    program when entering a certain option.
    * When the program exits, display a message for the user, stating the
    current time, and calculate and display how long the user used your
    program.
    * Make use of helper classes where possible.
    * Use the SDK to run your program."""

    I have done alot more coding since my last thread, thanks to the help from xcallmejudasx and others. (Thank you!)


    I have a few more questions that I need your help with because I'm stuck at the moment.

    1. When the program starts it asks the user for his/her name and surname,
    I have inserted an error check in case the user does not input anything
    and it works, but now I'm struggling with the error check for when the
    user inputs a number for his/her name and surname. Don't know how to do
    this...

    2. I have created methods for the calculator options in the "menu", nrs. '1-7'
    works fine but nrs. 8; 9 and 0 from the "menu" doesn't want to work.
    Let me explain, when the user enters '8' which he/she has chosen from,
    (which is "Octal and Hexadecimal equivalent of numbers") in this case, it
    does not give me the desired output, the output/answer it gives me
    everytime is ' 0 ', which is totally not right.
    The same is happening for option '9' which is ("Round numbers") from
    the 'menu'.
    The other option which is not working is '0' which is "Exit program", what
    happens here is that when the user chooses this option (this option must
    exit/stop the whole program immediately) but what happens is that the
    program first asks the user for the second numbers input before stopping...

    I think this is enough for now (there are a few more q's but I will come back to them later).

    Here is my code that I have done so far, please have a look through my code and assist me of where I went wrong.

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.text.*;
    
    public class Project {
    	
    	
    	private static String nameSurname = "";	
    	private static String num1 = null; 
    	private static String num2 = null; 
    	private static String choice1 = null;
    	private static int answer = 0;
    	private static String more = null;
    	
    	public int Add() {
    		answer = Integer.parseInt(num1) + Integer.parseInt(num2);
    		return answer;
    	}
    	
    	public int Subtract() {
    		answer = Integer.parseInt(num1) -  Integer.parseInt(num2);
    		return answer;
    	}
    	
    	public int Multiply() {
    		answer = Integer.parseInt(num1) * Integer.parseInt(num2);
    		return answer;
    	}
    	
    	public int Divide() {
    		answer = Integer.parseInt(num1) / Integer.parseInt(num2);
    		return answer;
    	}
    	
    	public int Modulus() {
    		answer = Integer.parseInt(num1) % Integer.parseInt(num2);
    		return answer;
    	}
    	
    	public int maximumValue() {
    		answer = (Math.max(Integer.parseInt(num1), Integer.parseInt(num2)));
    		return answer;
    	}
    	
    	public int minimumValue() {
    		answer = (Math.min(Integer.parseInt(num1), Integer.parseInt(num2)));
    		return answer;
    	}
    	
    	public int absoluteNumber1() {
    		answer = (Math.abs(Integer.parseInt(num1)));
    		return answer;
    	}
    	
    	public int absoluteNumber2() {
    		answer = (Math.abs(Integer.parseInt(num2)));
    		return answer;
    	}
    	
    	public double Squareroot1() {
    		answer = (int)(Math.sqrt(Integer.parseInt(num1)));
    		return answer;
    	}
    	
    	public double Squareroot2() {
    		answer = (int)(Math.sqrt(Integer.parseInt(num2)));
    		return answer;
    	}
    	
    	public int octalEquivalent() {
    		answer = Integer.parseInt(num1 + num2);
    		answer = Integer.parseInt(Integer.toOctalString(answer));
    		return answer;
    	}
    	
    	public int hexadecimalEquivalent() {
    		answer = Integer.parseInt(num1 + num2);
    		answer = Integer.parseInt(Integer.toHexString(answer));
    		return answer;
    	}
    	
    	public double Round1() {
    		answer = Integer.parseInt(null, Math.round(answer));
    		return answer;
    	}
    	
    	public double Round2() {
    		answer = Integer.parseInt(null, Math.round(answer));
    		return answer;
    	}
    	
    	
    		SimpleDateFormat format1 = new SimpleDateFormat("EEEE, dd MMMM yyyy");
    	    Date now = new Date();
    	    
    	    SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a");
    	    Date now2 = new Date();
    	    
    	    
    	    public String insertNameAndSurname() throws IOException{
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			while (nameSurname == null || nameSurname.length() == 0) {
    	    				inputCorrect = false;
    	    			try {
    	    				BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.println("Please enter your name and surname :");
    	    				nameSurname = inStream.readLine();
    	    				inputCorrect = true;
    	    			}catch (IOException ex) {
    	    				System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :");
    	    				inputCorrect = false;
    	    			}
    	    			}
    	    			System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now));
        				System.out.println("and the time is now exactly " + format2.format(now2) + ".");
    	    		}
    				return nameSurname;
    	    }
    	 
    	    
    	    public String inputNumber1() throws IOException {
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			try {
    	    				BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.print("\nPlease enter a number you want to do a calculation with and hit <ENTER>: ");
    	    				num1 = br.readLine();
    	    				int number1 = Integer.parseInt(num1);
    	    				System.out.println("\nThe number you have entered is: " + number1);
    	    				inputCorrect = true;
    	    			} catch (NumberFormatException nfe) {
    	    				System.out.println("\nYou did not enter a valid number:  " + "\""+ num1 + "\"  is not a number!!");
    	    				inputCorrect = false;
    	    			} 
    	    		}
    				return num1;
    	    }
    		
    	    
    	    public String calculatorChoice() throws IOException {
    	    	System.out.println("Please select an option of what you would like to do with this number from the menu below and hit <ENTER>: ");
    	    
    	    	System.out.println("\n*********************************************");
    	    	System.out.println("---------------------------------------------");
    	    	System.out.println("Please select an option from the list below: ");
    	    	System.out.println("---------------------------------------------");
    	    	System.out.println("1 - Add");
    	    	System.out.println("2 - Subtract");
    	    	System.out.println("3 - Multiply");
    	    	System.out.println("4 - Divide (remainder included)");
    	    	System.out.println("5 - Maximum and minimum value of two numbers");
    	    	System.out.println("6 - Squareroot");
    	    	System.out.println("7 - Absolute value of numbers");
    	    	System.out.println("8 - Octal and Hexadecimal equivalent of numbers");
    	    	System.out.println("9 - Round numbers");
    	    	System.out.println("0 - Exit program");
    	    	System.out.println("**********************************************");
    	    	
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			try {
    	    				BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.println("Please enter your option and hit <ENTER>:");
    	    				choice1 = inStream.readLine();
    	    				int c1 = Integer.parseInt(choice1);
    	    				System.out.println("\nYou have entered choice number: " + c1);
    	    				inputCorrect = true;
    	    			} catch (NumberFormatException nfe) {
    	    				System.out.println("You did not enter a valid  choice number:  " + "\""+ choice1 + "\"  is not in the list!!");
    	    				inputCorrect = false;
    	    			} 
    	    		}
    				return choice1;
    	    }
    	    
    	    
    	    public String inputNumber2() throws IOException {
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			try {
    	    				BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.print("\nPlease enter another number you want to do the calculation with and hit <ENTER>: ");
    	    				num2 = br2.readLine();
    	    				int n2 = Integer.parseInt(num2);
    	    				System.out.println("\nThe second number you have entered is: " + n2);
    	    				System.out.println("\nYour numbers are: " + num1 + " and " + num2);
    	    				inputCorrect = true;
    	    			} catch (NumberFormatException nfe) {
    	    				System.out.println("You did not enter a valid number:  " + "\""+ num2 + "\"  is not a number!!");
    	    			} 	
    	    		}
    				return num2;
    	    }
    	    
    	    
    	    public int Calculator() {
    	    	int choice2 = Integer.parseInt(choice1);
    	    	switch (choice2) {
    				case 1 :
    					Add();
    					System.out.println("The answer of " + num1 + " + "  + num2 + " is: " + answer);
    					break;
    				case 2 :
    					Subtract();
    					System.out.println("The answer of " + num1 + " - " + num2 + " is: " + answer);
    					break;
    				case 3 :
    					Multiply();
    					System.out.println("The answer of " + num1 + " * " + num2 + " is: " + answer);
    					break;
    				case 4 :
    					Divide();
    					System.out.print("The answer of " + num1 + " / " + num2 + " is: " + answer);
    					Modulus();
    					System.out.println(" and the remainder is "  + answer);
    					break;
    				case 5 :
    					maximumValue();
    					System.out.println("The maximum number between the numbers " + num1 + " and " + num2 + " is: " + answer);
    					minimumValue();
    					System.out.println("The minimum number between the numbers " + num1 + " and " + num2 + " is: " + answer);
    					break;
    				case 6 :
    					Squareroot1();
    					System.out.println("The squareroot of value " + num1 + " is: " + answer);
    					Squareroot2();
    					System.out.println("The squareroot of value " + num2 + " is: " + answer);
    					break;
    				case 7 :
    					absoluteNumber1();
    					System.out.println("The absolute number of " + num1 + " is: " + answer);
    					absoluteNumber2();
    					System.out.println("The absolute number of " + num2 + " is: " + answer);
    					break;
    				case 8 :
    					octalEquivalent();
    					System.out.println("The octal equivalent of " + num1 + " is: " + answer);
    					hexadecimalEquivalent();
    					System.out.println("The hexadecimal equivalent of " + num1 + " is: " + answer);
    					octalEquivalent();
    					System.out.println("\nThe octal equivalent of " + num2 + " is: " + answer);
    					hexadecimalEquivalent();
    					System.out.println("The hexadecimal equivalent of " + num2 + " is: " + answer);
    				case 9 :
    					Round1();
    					System.out.println("The rounded number of " + num1 + " is: " + answer);
    					Round2();
    					System.out.println("The rounded number of " + num2 + " is: " + answer);
    				case 0 :
    					if (choice2 == 0) {
    						System.exit(1);
    					}
    	    	}
    			return choice2;
    	    }
    	    
    	    public String anotherCalculation() throws IOException {
    	    	BufferedReader br3 = new BufferedReader (new InputStreamReader(System.in));
    	    	System.out.print("\nWould you like to do another calculation? Y/N");
    	    	more = br3.readLine();
    	    	if ((more == "y") | (more == "Y")) {
    	    		inputNumber1();
    	    		System.out.println("");
    	    		calculatorChoice();
    	    		System.out.println("");
    	    		inputNumber2();
    	    		System.out.println("");
    	    		Calculator();
    	    		System.out.println("");
    	    	} else {
    	    		System.exit(0);
    	    	}
    			return more;
    	    }
    	    
    	    
    	    public static void main(String[] args) throws IOException {
    	    	Project p1 = new Project();
    	  
    	    	p1.insertNameAndSurname();
    	    	System.out.println("");
    	    	p1.inputNumber1();
    	    	System.out.println("");
    	    	p1.calculatorChoice();
    	    	System.out.println("");
    	    	p1.inputNumber2();
    	    	System.out.println("");
    	    	p1.Calculator();
    			System.out.println("");
    			p1.anotherCalculation();
    			System.out.println("");
    	    }
    }
    Your help would be much appreciated.
    Thank you!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Did you run your application? If so what happen, you didn't get expecting output? You have to test your application first. If you comes with any issue ask it here.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    1. When the program starts it asks the user for his/her name and surname,
    I have inserted an error check in case the user does not input anything
    and it works, but now I'm struggling with the error check for when the
    user inputs a number for his/her name and surname. Don't know how to do
    this...
    This is a simple solution. All character values(capital and lowercase) fall in between a-Z, or A-z( I don't remember which is first); You can create a loop and use the charAt() method within string to check if each character is betwen a and Z. If one character is invalid then the entire name is invalid.

    Let me compile and mess around with your code for the second question and I'll get back to you.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Your round methods are messing up because your trying to round answer instead of num1 and num2. I answer to
    Java Code:
    answer = Math.round(Integer.parseInt(num1);
    answer = Math.round(Integer.parseInt(num2);
    and that works. You have a logic flaw with your round methods also. I tried to input a decimal and it tells me not a number. It might make everything work more smoothly if you change some of your parses to parseDouble instead of parseInt

  5. #5
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Exclamation Java Project

    I got the round methods to work perfectly now, like youv'e said xcallmejudasx.
    Tnx.
    But I still can't the input name surname part right, I tried what youv'e said about using the charAt() method but I think I'm missing something, please look at my code and help me.

    Here is the code:

    Java Code:
    public String insertNameAndSurname() throws IOException{
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			while (nameSurname == null || nameSurname.length() == 0) {
    	    				for (int i = 0; i < nameSurname.length(); i++) {
    	    					if (String(nameSurname.charAt(i))){
    	    						inputCorrect = false;
    	    					}
    	    				}
    	    				inputCorrect = false;
    	    			try {
    	    				BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.println("Please enter your name and surname :");
    	    				nameSurname = inStream.readLine();
    	    				inputCorrect = true;
    	    			}catch (IOException ex) {
    	    				System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :");
    	    				inputCorrect = false;
    	    			}
    	    			}
    	    			System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now));
        				System.out.println("and the time is now exactly " + format2.format(now2) + ".");
    	    		}
    				return nameSurname;
    	    }
    Thanx

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    your not comparing anything in your if statement.
    Java Code:
    for (int i = 0; i < nameSurname.length(); i++) {
    	    if ((nameSurname.charAt(i) > "a") && (nameSurname.charAt(i) < "Z")){
    	    		inputCorrect = true;
               }
                else{
                        inputCorrect = false;
                        break;
                 }
    }
    This compares each character in your string and checks if it's a letter. If it is it goes to the next character and checks. If it finds a character that isn't a letter it returns false and breaks the loop. I'm not sure where a space character fits in the alphabet your going to need to look that up and maybe throw another conditional into the if to deal with it.
    Last edited by xcallmejudasx; 11-13-2008 at 10:56 PM.

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    also remove the inputCorrect = false outside your loop. That just removes all work the loop did and makes every input false.

  8. #8
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default Java Project

    I tried the code what youv'e said but it doesn't want to work, I get an error message that states - "The operator > is undefined for the argument type(s) char, String", at this part of the code :
    Java Code:
    if (((nameSurname.charAt(i) > "a")) && (nameSurname.charAt(i) < "Z")){...
    I know the problm lies in the error message but not sure how to fix this??

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You cannot use > sign for character comparing. Just think that there is a meaning comparing characters as less than or greater than...

    Check that equal or not.

  10. #10
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by Eranga View Post
    You cannot use > sign for character comparing. Just think that there is a meaning comparing characters as less than or greater than...

    Check that equal or not.
    Are you sure? I am 90% sure I've compared characters like this before. Maybe I had a valueOf or something in front of it. I know in theory each character has an ASCII value and you can compare that value to determine if a letter is in fact a letter(since all characters from a-Z are letters). Is there something like charToIntValue() that would do that?

  11. #11
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    omg woops didn't notice this before. Change the double quotes around "a" and "Z" to single quotes. Double means strings, single means char.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Are you sure? I am 90% sure I've compared characters like this before. Maybe I had a valueOf or something in front of it. I know in theory each character has an ASCII value and you can compare that value to determine if a letter is in fact a letter(since all characters from a-Z are letters). Is there something like charToIntValue() that would do that?
    Yes lol, I've miss the same thing here. You cannot compare strings, not characters.

  13. #13
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Thumbs down Java Project

    Hi all, I please need further help on my java project I need to do.

    I'm stuck at the moment and isn't sure how to rectify these few problems:

    1. The 'octalEquivalent();' and 'hexadecimalEquivalent(); methods that I
    have created doesn't want to work, the error messages I'm getting is
    "The method toOctalString(String) is undefined for the type Double" &
    "The method toHexString(double) in the type Double is not applicable for
    the arguments (String) ",
    how can I correct this??

    2. I have created a method ( anotherCalculation(); ), so that if the user
    comes to the end of his/her first calculation, he/she has a choice if they
    want to do another calculation, (here is the method: )

    Java Code:
    public char anotherCalculation() throws IOException {
    	    	try {
    	    	   	System.out.print("\nWould you like to do another calculation? Y/N");
    	    	   	more = (char) System.in.read();
    	    	   	do {
    	    	   		inputNumber1();
    	    	   		System.out.println("");
    	    	   		calculatorChoice();
    	    	   		System.out.println("");
    	    	   		inputNumber2();
    	    	   		System.out.println("");
    	    	   		Calculator();
    	    	   		System.out.println("");
    	    	   		anotherCalculation();
    	    			System.out.println("");
    	    	   	} 	while((more == 'y') | (more == 'Y')); 
    	    	   	System.exit(0);
    	    	} catch (IOException ex){
    	    		System.exit(0);
    	    	}
    			return more;
    	    }
    the problem is that when the user inputs antything or just press enter the program continues and never stops, what I want the program to do, is when the user comes to the question where the program asks ' Would you like to do another calculation? Y/N ', the user must input " 'y/Y' for yes and 'n/N' for no. If the user enters 'y/Y', the program has to continue and start where the program asks for a number... When the user enters ' n/N ' the program must exit, and display a message for the user, stating the current time and calculate and display how long the user used the program..

    Here is the code for the whole program that I coded so far...

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.text.*;
    
    public class Project {
    	
    	
    	private static String nameSurname = "";	
    	private static String num1 = null; 
    	private static String num2 = null; 
    	private static String choice1 = null;
    	private static double answer = 0;
    	private static char more;
    	
    	public double Add() {
    		answer = (Double.parseDouble(num1) + Double.parseDouble(num2));
    		return answer;
    	}
    	
    	public double Subtract() {
    		answer = (Double.parseDouble(num1) - Double.parseDouble(num2));
    		return answer;
    	}
    	
    	public double Multiply() {
    		answer = (Double.parseDouble(num1) * Double.parseDouble(num2));
    		return answer;
    	}
    	
    	public double Divide() {
    		answer = (Double.parseDouble(num1) / Double.parseDouble(num2));
    		return answer;
    	}
    	
    	public double Modulus() {
    		answer = (Double.parseDouble(num1) % Double.parseDouble(num2));
    		return answer;
    	}
    	
    	public double maximumValue() {
    		answer = (Math.max(Double.parseDouble(num1), Double.parseDouble(num2)));
    		return answer;
    	}
    	
    	public double minimumValue() {
    		answer = (Math.min(Double.parseDouble(num1), Double.parseDouble(num2)));
    		return answer;
    	}
    	
    	public double absoluteNumber1() {
    		answer = (Math.abs(Double.parseDouble(num1)));
    		return answer;
    	}
    	
    	public double absoluteNumber2() {
    		answer = (Math.abs(Double.parseDouble(num2)));
    		return answer;
    	}
    	
    	public double Squareroot1() {
    		answer = (Math.sqrt(Double.parseDouble(num1)));
    		return answer;
    	}
    	
    	public double Squareroot2() {
    		answer = (Math.sqrt(Double.parseDouble(num2)));
    		return answer;
    	}
    	
    	public double octalEquivalent1() {
    		answer = Double.parseDouble(Double.toOctalString(num1));
    		return answer;
    	}
    	
    	public double octalEquivalent2() {
    		answer = Double.parseDouble(Double.toOctalString(num2));
    		return answer;
    	}
    	
    	public double hexadecimalEquivalent1() {
    		answer = Double.parseDouble(Double.toHexString(num1));
    		return answer;
    	}
    	
    	public double hexadecimalEquivalent2() {
    		answer = Double.parseDouble(Double.toHexString(num2));
    		return answer;
    	}
    	
    	public double Round1() {
    		answer = Math.round(Double.parseDouble(num1));
    		return answer;
    	}
    	
    	public double Round2() {
    		answer = Math.round(Double.parseDouble(num2));
    		return answer;
    	}
    	
    	
    		SimpleDateFormat format1 = new SimpleDateFormat("EEEE, dd MMMM yyyy");
    	    Date now = new Date();
    	    
    	    SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a");
    	    Date now2 = new Date();
    	    
    	    DecimalFormat decimals = new DecimalFormat("#0.00");
    	    
    	    
    
    	    
    	    public String insertNameAndSurname() throws IOException{
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			while (nameSurname == null || nameSurname.length() == 0) {
    	    				
    	    				for (int i = 0; i < nameSurname.length(); i++) {
    	    				    if ((nameSurname.charAt(i) > 'a') && (nameSurname.charAt(i) < 'Z')){
    	    				    		inputCorrect = true;
    	    			           }
    	    			            else{
    	    			                    inputCorrect = false;
    	    			                    break;
    	    			             }
    	    			}
    	    				
    	    			try {
    	    				BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.print("Please enter your name and surname: ");
    	    				nameSurname = inStream.readLine();
    	    				inputCorrect = true;
    	    			}catch (IOException ex) {
    	    				System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :");
    	    				inputCorrect = false;
    	    			}
    	    			}
    	    			System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now));
        				System.out.println("and the time is now exactly " + format2.format(now2) + ".");
    	    		}
    				return nameSurname;
    	    }
    			
    	    	 
    	    
    	   
    
    		public String inputNumber1() throws IOException {
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			try {
    	    				BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.print("\nPlease enter a number you want to do a calculation with and hit <ENTER>: ");
    	    				num1 = br.readLine();
    	    				double number1 = Double.parseDouble(num1);
    	    				System.out.println("\nThe number you have entered is: " + number1);
    	    				inputCorrect = true;
    	    			} catch (NumberFormatException nfe) {
    	    				System.out.println("\nYou did not enter a valid number:  " + "\""+ num1 + "\"  is not a number!!");
    	    				inputCorrect = false;
    	    			} 
    	    		}
    				return num1;
    	    }
    		
    	    
    	    public String calculatorChoice() throws IOException {
    	    	System.out.println("Please select an option of what you would like to do with this number from the menu below and hit <ENTER>: ");
    	    
    	    	System.out.println("\n*********************************************");
    	    	System.out.println("---------------------------------------------");
    	    	System.out.println("Please select an option from the list below: ");
    	    	System.out.println("---------------------------------------------");
    	    	System.out.println("1 - Add");
    	    	System.out.println("2 - Subtract");
    	    	System.out.println("3 - Multiply");
    	    	System.out.println("4 - Divide (remainder included)");
    	    	System.out.println("5 - Maximum and minimum value of two numbers");
    	    	System.out.println("6 - Squareroot");
    	    	System.out.println("7 - Absolute value of numbers");
    	    	System.out.println("8 - Octal and Hexadecimal equivalent of numbers");
    	    	System.out.println("9 - Round numbers");
    	    	System.out.println("0 - Exit program");
    	    	System.out.println("**********************************************");
    	    	
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			try {
    	    				BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.print("Please enter your option and hit <ENTER>: ");
    	    				choice1 = inStream.readLine();
    	    				int c1 = Integer.parseInt(choice1);
    	    				System.out.println("\nYou have entered choice number: " + c1);
    	    				inputCorrect = true;
    	    			} catch (NumberFormatException nfe) {
    	    				System.out.println("You did not enter a valid  choice number:  " + "\""+ choice1 + "\"  is not in the list!!");
    	    				inputCorrect = false;
    	    			} 
    	    		}
    				return choice1;
    	    }
    	    
    	    
    	    public String inputNumber2() throws IOException {
    	    	boolean inputCorrect = false;
    	    		while (inputCorrect == false) {
    	    			try {
    	    				BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in));
    	    				System.out.print("\nPlease enter another number you want to do the calculation with and hit <ENTER>: ");
    	    				num2 = br2.readLine();
    	    				double n2 = Double.parseDouble(num2);
    	    				System.out.println("\nThe second number you have entered is: " + n2);
    	    				System.out.println("\nYour numbers are: " + num1 + " and " + num2);
    	    				inputCorrect = true;
    	    			} catch (NumberFormatException nfe) {
    	    				System.out.println("You did not enter a valid number:  " + "\""+ num2 + "\"  is not a number!!");
    	    				inputCorrect = false;
    	    			} 	
    	    		}
    				return num2;
    	    }
    	    
    	    
    	    public int Calculator() {
    	    	int choice2 = (int) Double.parseDouble(choice1);
    	    	switch (choice2) {
    				case 1 :
    					Add();
    					System.out.print("The answer of " + num1 + " + "  + num2 + " is: " + decimals.format(answer));
    					break;
    				case 2 :
    					Subtract();
    					System.out.print("The answer of " + num1 + " - " + num2 + " is: " + decimals.format(answer));
    					break;
    				case 3 :
    					Multiply();
    					System.out.print("The answer of " + num1 + " * " + num2 + " is: " + decimals.format(answer));
    					break;
    				case 4 :
    					Divide();
    					System.out.print("The answer of " + num1 + " / " + num2 + " is: " + decimals.format(answer));
    					Modulus();
    					System.out.print(" and the remainder is "  + decimals.format(answer));
    					break;
    				case 5 :
    					maximumValue();
    					System.out.println("The maximum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer));
    					minimumValue();
    					System.out.println("The minimum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer));
    					break;
    				case 6 :
    					Squareroot1();
    					System.out.println("The squareroot of value " + num1 + " is: " + decimals.format(answer));
    					Squareroot2();
    					System.out.println("The squareroot of value " + num2 + " is: " + decimals.format(answer));
    					break;
    				case 7 :
    					absoluteNumber1();
    					System.out.println("The absolute number of " + num1 + " is: " + decimals.format(answer));
    					absoluteNumber2();
    					System.out.println("The absolute number of " + num2 + " is: " + decimals.format(answer));
    					break;
    				case 8 :
    					octalEquivalent1();
    					System.out.println("The octal equivalent of " + num1 + " is: " + decimals.format(answer));
    					hexadecimalEquivalent1();
    					System.out.println("The hexadecimal equivalent of " + num1 + " is: " + decimals.format(answer));
    					octalEquivalent2();
    					System.out.println("\nThe octal equivalent of " + num2 + " is: " + decimals.format(answer));
    					hexadecimalEquivalent2();
    					System.out.println("The hexadecimal equivalent of " + num2 + " is: " + decimals.format(answer));
    					break;
    				case 9 :
    					Round1();
    					System.out.println("The rounded number of " + num1 + " is: " + decimals.format(answer));
    					Round2();
    					System.out.println("The rounded number of " + num2 + " is: " + decimals.format(answer));
    					break;
    				case 0 :
    					if (choice2 == 0) {
    						System.exit(1);
    					}
    					break;
    	    	}
    			return choice2;
    	    }
    	    
    	    public char anotherCalculation() throws IOException {
    	    	try {
    	    	   	System.out.print("\nWould you like to do another calculation? Y/N");
    	    	   	more = (char) System.in.read();
    	    	   	do {
    	    	   		inputNumber1();
    	    	   		System.out.println("");
    	    	   		calculatorChoice();
    	    	   		System.out.println("");
    	    	   		inputNumber2();
    	    	   		System.out.println("");
    	    	   		Calculator();
    	    	   		System.out.println("");
    	    	   		anotherCalculation();
    	    			System.out.println("");
    	    	   	} 	while((more == 'y') | (more == 'Y')); 
    	    	   	System.exit(0);
    	    	} catch (IOException ex){
    	    		System.exit(0);
    	    	}
    			return more;
    	    }
    	    
    	    
    	    public static void main(String[] args) throws IOException {
    	    	Project p1 = new Project();
    	  
    	    	p1.insertNameAndSurname();
    	    	System.out.println("");
    	    	p1.inputNumber1();
    	    	System.out.println("");
    	    	p1.calculatorChoice();
    	    	System.out.println("");
    	    	p1.inputNumber2();
    	    	System.out.println("");
    	    	p1.Calculator();
    			System.out.println("");
    			p1.anotherCalculation();
    			System.out.println("");
    			
    	    }
    }
    Please help.
    I would very much apreciate it.
    Thanks

  14. #14
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Unhappy Java Project

    I tried to correct my code according to the errors I got while waiting for help, and my ' octalEquivalent(); ' method now works, I also changed my ' hexadecimalEquivalent(); ' method, but I get an error message that states
    "Exception in thread "main" java.lang.NumberFormatException: For input string: "b"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at org.Java.Instructor.Project.hexadecimalEquivalent1 (Project.java:86)
    at org.Java.Instructor.Project.Calculator(Project.jav a:271)
    at org.Java.Instructor.Project.main(Project.java:329) "

    By the looks of the error message I cannot have letters in the answer.
    How can I correct this??
    Please Help

    Java Code:
    public double hexadecimalEquivalent1() {
    		long n1 = Long.parseLong(num1);
    		answer = Long.parseLong(Long.toHexString(n1));
    		return answer;
    	}

  15. #15
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Look up a conversion algorithm. It's not as simple as just parsing it. You have to read the character, determine if its a letter or number, if its a number convert it to its decimal equivalent. Hex is base 16 which is 0-9 A B C D E F correct?

  16. #16
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Exclamation [HOMEWORK] Please need help with java calculator project.

    I've sorted out the problem about the octal and hex thanx, but now I'm stuck with another problem.

    The other thing that I don't know how to correct is, when the user comes to the end of the program, the program asks the user if he/she wants to do another calculation.

    I have created a method for this which is called " antoherCalculation(); " , what I want the method to do is, if the user answers ' y/Y ', it must ask the user again for a number and continue with the process as in the beginning (except for asking his/her name again!).

    At the moment my program allows any input even only enter and continues on and on and on without stopping...

    I want it to accept only answers ' y/Y ' or 'n/N' , when the user chooses e.g. 'y' the program must continue and when the user chooses 'n', the program must stop/exit, and display a message for the user, stating the current time, and calculate and display how long the user used the program.

    Here is the code for the method ' anotherCalculation(); ' :

    Java Code:
    public char anotherCalculation() throws IOException {
                try {
                       System.out.print("\nWould you like to do another calculation? Y/N");
                       more = (char) System.in.read();
                       do {
                           inputNumber1();
                           System.out.println("");
                           calculatorChoice();
                           System.out.println("");
                           inputNumber2();
                           System.out.println("");
                           Calculator();
                           System.out.println("");
                           anotherCalculation();
                        System.out.println("");
                       }     while((more == 'y') | (more == 'Y')); 
                       System.exit(0);
                } catch (IOException ex){
                    System.exit(0);
                }
                return more;
            }
    Please help me with this.
    Thanks

  17. #17
    efsiken is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Post simulation of Symmetric Multiprocessing(SMP) in java

    Hello,
    I need some help in the above mentioned exercise. I want to proceed by taking a matrix, dividing it into subsets programming each part separately and then bringing the results together.
    regards

Similar Threads

  1. Java Project
    By Smirre in forum New To Java
    Replies: 7
    Last Post: 11-06-2008, 06:13 PM
  2. java project
    By gvdn in forum Advanced Java
    Replies: 4
    Last Post: 07-13-2008, 06:45 AM
  3. UML to Java project
    By banie in forum NetBeans
    Replies: 3
    Last Post: 01-28-2008, 10:16 AM
  4. Help Java project.
    By mandrake446 in forum New To Java
    Replies: 1
    Last Post: 11-26-2007, 11:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •