9 Replies - 792 Views - Last Post: 15 October 2009 - 09:13 AM Rate Topic: -----

#1 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Inputting an Integer into a String

Posted 15 October 2009 - 12:26 AM

Just as the name implies I'm having trouble trying to enter an integer into a string. Basically I'm trying to create an Inventory application, where the user inputs an Item code, Description (e.g. Hammer) unit price, Qoh, calculate inventory cost, and finally to display the results of the inventory cost, and total cost of all transactions. It does it all, and I'm sure on a users perspective it's a nifty little tool, however, on the programmers end it's HELL! :angry:

Here's a little more detail:
Your program must contain seven methods:
1. editItemNum() inputs and validates the item number. It accepts no parameters and returns a valid item number.
2. editItemDesc() inputs and validates the item description. It accepts no parameters and returns a valid Item Description.
3. editUnitPrice() inputs and validates the unit price. It accepts no parameters and returns a valid unit price.
4. editQoh() inputs and validates the quantity-on-hand. It accepts no parameters and returns a valid quantity-on-hand.
5. calcInventory() calculates inventory cost. It receives the unit price and quantity as parameters and returns the calculated cost.
6. print() An overloaded method to display the results of each transaction. It receives the item number, description, quantity and unit price. This method calls the calcInventory() method above.
7. print() An overloaded method to display the total transactions and total inventory cost. It accepts the total transaction count and total inventory cost and returns no value.

and here's what I've created so far:

// import the Scanner class to access data from keyboard
import java.util.Scanner;


public class JavaMart 
{


	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String editItemNo;
		editItemNo = editItemNo(in);
		String editItemDesc;
		editItemDesc = editItemDesc(in);
		String editUnitPrice;
		editUnitPrice = editUnitPrice(in);
		System.out.println(" Item Number is as follows:  \n" + editItemNo);
		System.out.println("Item Description is as follows: \n" + editItemDesc);
	}
	 
	public static String editItemNo(Scanner in) 
	{
		 String itemNo;
			boolean error;
			do
			{
			  error = false;
			  System.out.print("\n\tEnter item number in the format(##LLL) :");
			  itemNo = in.nextLine();
			  // check length
			  if (itemNo.length() != 5)
			  {
				error = true;
				System.out.println("\n\t\t**ERROR01** - Item number must be five characters\n");
			  }
			  // check format (2 numbers and three letters)
			  else
			  {
				String letters = itemNo.substring(2).toUpperCase();	 // retrieve first two positions
				String numbers = itemNo.substring(0,2); //retrieve number portion
				for (int i = 0; i < numbers.length();i++)
				{
				  if (!Character.isDigit(numbers.charAt(i))) error = true;
				} //end for
				if (error)
				   System.out.println("\n\t\t**ERROR02** - Item number must start with two numbers\n");
				else
				{	// check letters

				  for (int i = 0; i < letters.length(); i++)
				  {
					if (!Character.isLetter(letters.charAt(i))) error = true;
				  }  //end for
				  if (error)
					 System.out.println("\n\t\t **ERROR03** - Item number must end with three letters\n");
				} // end else
			} // end else
			}  while (error);  // end itemNo validation
			return itemNo;
		 }
		public static String editItemDesc(Scanner in) {
			String itemDesc; 
			boolean error;
			do
			{ 
				error = false;
				System.out.println("Please enter the item description (Max 20 Char) : ");
				itemDesc = in.nextLine();
				
				if (itemDesc.length() > 20) {
					error = true; 
					System.out.println("Item Descripition can be no more than 20 characters!");
								}
					else { 
					error = false; 
						 }
			}   while (error);		
			return itemDesc; 
		}
		public static String editUnitPrice(Scanner in) {
			String unitPrice;
			boolean error;
			do 
			{
				error = false;
				System.out.println("Enter unit price: ");
				unitPrice = in.nextLine();
				int x = unitPrice;

					  
					}
				
			} 
		}
}
		



Before you say anything, I know, it's no where near complete. So far I'm able to check off the first step, the editItemNum, editItemDesc is pretty straight forward and I didn't seem to be having any problems there, it is still riddled with bugs but I was too lazy to finish at the moment, I'll save that for later when I'm doing the final tweaks. Now the part I'm stuck on is step 3, editUnitPrice. As you can see I've imported a scanner so that the program can acquire input from the user for the fields requiring user input, which are editItemNum, editItemDesc, editUnitPrice, and finally editQoh.

If you take a look at my code you'll notice this bit towards the bottom:
public static String editUnitPrice(Scanner in) {
			String unitPrice;
			boolean error;
			do 
			{
				error = false;
				System.out.println("Enter unit price: ");
				unitPrice = in.nextLine();
				int x = unitPrice;

this is the part I'm stuck at I can't seem to figure out how to set up the unitPrice into an interger. Why am I doing this you ask? Well it's because I need to check for errors in the user input, the unit price must be somewhere between 0 and $5000 so I was going to set it up so that if whatever the user inputs for unit price is over the $5000 limit It would then trigger a boolean error and then I could display something like "Error: Too much"

Now I've been racking my head about this single part for a couple hours now and just can't seem to figure out how, it's already 2:25 in the morning and my classes start in 6 more hours, I'm no longer capable of thought that is why I now kindly ask you, great people of DreamInCode to give me a pointer or let me know of a better way.

Thanks!

This post has been edited by Murderality: 15 October 2009 - 12:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Inputting an Integer into a String

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 12:52 AM

Hi
instead of using
int unitPrice = in.nextLine();


use
int unitPrice = in.nextInt();


hope it helps! :)
Was This Post Helpful? 0
  • +
  • -

#3 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 01:06 AM

View Postjapanir, on 14 Oct, 2009 - 11:52 PM, said:

Hi
instead of using
int unitPrice = in.nextLine();


use
int unitPrice = in.nextInt();


hope it helps! :)


Hmm, so instead of:

String unitPrice;
			boolean error;
			do 
			{
				error = false;
				System.out.println("Enter unit price: ");
				 unitPrice = in.nextLine();


It would be:
String unitPrice;
			boolean error;
			do 
			{
				error = false;
				System.out.println("Enter unit price: ");
				int unitPrice = in.nextLine();


If that's what you meant then it still doesn't seem to work, it then underlines the unitPrice in red calling it a duplicate local variable, also it says for "in.nextLine" Cannot convert from String to int after adding the int.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 01:11 AM

no
instead of in.nextLine() type in.nextInt()
Was This Post Helpful? 0
  • +
  • -

#5 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 01:31 AM

View Postjapanir, on 15 Oct, 2009 - 12:11 AM, said:

no
instead of in.nextLine() type in.nextInt()


Oh, haha sorry I didn't notice that, alright I'm starting to get somewhere now, but there's still one remaining problem:

public static String editUnitPrice(Scanner in) {
			String unitPrice;
			boolean error;
			do 
			{
				error = false;
				System.out.println("Enter unit price: ");
				int unitPrice = in.nextInt();
				if (unitPrice <= 5000) {
					error = true;
					System.out.println("Error!");
				}
				else { 
					error = false;}		   
			   				
			} while (error);
			return unitPrice;


The "int unitPrice" is still being recognized as a duplicate value, it must be because it recognizes "String unitPrice" at the top and "int unitPrice" as too seperate values, any clues? Sorry for being a pester, once I get through this one problem I should be able to manage with the rest of it.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 01:39 AM

delete the declaration:
String unitPrice;


you declare twice on the same variable, once as a string and second time as an int.
you wont need it anymore.
i copied your code and just deleted the problematic line.
public static String editUnitPrice(Scanner in) {

			boolean error;
			do
			{
				error = false;
				System.out.println("Enter unit price: ");
				int unitPrice = in.nextInt();
				if (unitPrice <= 5000) {
					error = true;
					System.out.println("Error!");
				}
				else {
					error = false;}		  
							   
			} while (error);
			return unitPrice;


This post has been edited by japanir: 15 October 2009 - 02:47 AM

Was This Post Helpful? 1
  • +
  • -

#7 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 08:25 AM

View Postjapanir, on 15 Oct, 2009 - 12:39 AM, said:

delete the declaration:
String unitPrice;


you declare twice on the same variable, once as a string and second time as an int.
you wont need it anymore.
i copied your code and just deleted the problematic line.
public static String editUnitPrice(Scanner in) {

			boolean error;
			do
			{
				error = false;
				System.out.println("Enter unit price: ");
				int unitPrice = in.nextInt();
				if (unitPrice <= 5000) {
					error = true;
					System.out.println("Error!");
				}
				else {
					error = false;}		  
							   
			} while (error);
			return unitPrice;




Oh ok, I see how that works now, but now I have another problem..

public static String editUnitPrice(Scanner in) {
		
			boolean error;
			do 
			{
				error = false;
				System.out.println("Enter unit price: ");
				int unitPrice = in.nextInt();
				if (unitPrice >= 5000) {
					error = true;
					System.out.println("Error!");
				}
				else { 
					error = false;}		   
			   				
			} while (error);
			return unitPrice;
			
		}


That is what it looks like now, the problem I'm now having is at the bottom where it says "return unitPrice;" for some reason it is not able to resolve the unitPrice integer declared above, I think this is because it is expecting a string. How can I get around this?

This post has been edited by Murderality: 15 October 2009 - 08:25 AM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10779
  • View blog
  • Posts: 40,135
  • Joined: 27-December 08

Re: Inputting an Integer into a String

Posted 15 October 2009 - 08:28 AM

You need to declare unit price outside of the do-loop. As you have it now, this variable exists only within the loop, so the return statement can't access it.
Was This Post Helpful? 1
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 08:30 AM

also your method returns String Objects
change it to int:
public static int editUnitPrice(Scanner in){
//code
}


Was This Post Helpful? 0
  • +
  • -

#10 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Re: Inputting an Integer into a String

Posted 15 October 2009 - 09:13 AM

View Postjapanir, on 15 Oct, 2009 - 07:30 AM, said:

also your method returns String Objects
change it to int:
public static int editUnitPrice(Scanner in){
//code
}



Haha, I already fixed that, I finally got it working! Oh boy I feel like jumping for joy right now! You guys have been so much help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1