Purchase Java part 2
NickName:Raymond Ask DateTime:2013-10-10T21:03:02

Purchase Java part 2

Can any one help me? Our instructor said that he does'nt want to use array list in our program. And only array,for loops,and do while only. The program is a Purchase program.(Adding item, records the transactions.) This is the first step i want to understand.

In this program, i want do display item code and descip properly. For example code=123 and descrip= yeah. The output displays, Item code = 123, Item descrpition = yeah. But once i say yes and i put another, example code =456 and desription = oh. Output Item code = 123456, Item description = yeah.oh.

import java.util.Scanner;

public class Apps {

    public static void main(String[] args) {

        Scanner a = new Scanner(System.in);
        String code = "", des = "", ans;
        String[] b = new String[1];
        String[] aw = new String[1];
        int x;

        do {

            System.out.print("Item code:");
            code += "" + a.next();

            System.out.print("des:");
            des += "" + a.next();

            System.out.println("yes or no:");
            ans = a.next();

        }

        while (ans.equals("yes"));

        for (x = 0; x < 1; x++) {
            b[x] = code;
            aw[x] = des;

            System.out.println("Item code:" + b[x]);
            System.out.println("Item description:" + aw[x]);

        }

    }

}

Copyright Notice:Content Author:「Raymond」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19296519/purchase-java-part-2

Answers
Parvin Gasimzade 2013-10-10T13:27:44

You can use this code for any number of items. It stores data in String and splits the string after user selects no.\n\npublic static void main(String[] args) {\n Scanner a = new Scanner(System.in);\n String ans;\n String itemCounts = \"\";\n String descriptions = \"\";\n\n do {\n System.out.print(\"Item code:\");\n itemCounts += \"\" + a.next() + \"\\n\";\n\n System.out.print(\"des:\");\n descriptions += \"\" + a.next() + \"\\n\";\n\n System.out.println(\"yes or no:\");\n ans = a.next();\n } while (ans.equals(\"yes\"));\n\n String[] b = itemCounts.split(\"\\n\");\n String[] aw = descriptions.split(\"\\n\");\n\n for (int i = 0; i < b.length; i++) {\n System.out.println(\"Item code:\" + b[i]);\n System.out.println(\"Item description:\" + aw[i]);\n }\n}\n",


Maroun 2013-10-10T13:07:41

next() doesn't handle the end of the line, so when you call next() again, it'll take as input the enter (\\n) you entered before.\n\nYou should call nextLine() after each next() (So it will swallow the \\n from the previous one).",


libik 2013-10-10T13:14:47

This works for example :\n\npublic static void main(String[] args) {\n\n Scanner a = new Scanner(System.in);\n String ans;\n int capacity = 100;\n String[] b = new String[capacity];\n String[] aw = new String[capacity];\n int itemCount = 0;\n int x;\n\n do { \n System.out.print(\"Item code:\");\n b[itemCount] = a.next();\n\n System.out.print(\"des:\");\n aw[itemCount] = a.next();\n\n System.out.println(\"yes or no:\");\n ans = a.next();\n itemCount++;\n } while (ans.equals(\"yes\"));\n\n for (x = 0; x < itemCount; x++) {\n System.out.println(\"Item code:\" + b[x]);\n System.out.println(\"Item description:\" + aw[x]);\n }\n}\n\n\nIf you want to be sure, that user can add \"infinite\" numbers of items, you have to manualy check if the itemCount is lesser than capacity of array and when it reaches, you have to allocate new array with higher capacity and copy the values into it.",


More about “Purchase Java part 2” related questions

Purchase Java part 2

Can any one help me? Our instructor said that he does'nt want to use array list in our program. And only array,for loops,and do while only. The program is a Purchase program.(Adding item, records ...

Show Detail

Find Last Purchase Order For Each Part

I need to find the last P.O.for parts purchased from Vendors. I was trying to come up with a way to do this using a query I found that allowed me to find the max Creation date for a group of Quotes

Show Detail

How to validate purchase with date in Java

I want have function that validating a purchase by date. The Scenario like. 1. We get the date from computer. 2. we do check, if the current date is the day after the purchase date. The purchase ...

Show Detail

Android consuming a part of purchase only

I analysed official docs about this but there is mentioned only that user can consume purchase after he is own it, using: mService.consumePurchase(3, getPackageName(), token); or method consume(

Show Detail

Working with multiple parameter options purchase() purchase(2)

The program is a virtual soda machine, Im having trouble with the purchase method. In the cases I need to pass there is a purchase() case which assumes one can is being bought, and a purchase(2) ca...

Show Detail

Java card purchase query

I want to develop a android application in which i have to use java card. Which development kit i have to buy for ?? I want to buy java card , which implement Java Card 2.1.1 specifications and

Show Detail

Java code that prints the customer name, discount level, the original purchase price, promotional price and the amount saved

Over all, this code is supposed to ask for the customers name, member level, and original purchase price. It should store all of those in their respective variables, if something other than an acce...

Show Detail

Unable to get updated Purchase object after Acknowledging the purchase in new Google billing Api 2.0

I am implementing the new billing API 2.0 which has this purchase acknowledge method. Earlier i was using AIDL for my purchase and i had following use case is that i used to pass developer payload

Show Detail

Create QuickBook purchase Order in java

Is it possible to create a Quick Books purchase order using java? I am planning to use QB web connector. Does it support?

Show Detail

In App Purchase in Android

i have to intregrate in app purchase in my new upcoming application. I am familiar about the purchase part and all, but a bit confused related to the applilcation is purchase or not part. That is, ...

Show Detail