Results 1 to 11 of 11
  1. #1
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default Adding numbers for sales reciept program(Biginner)

    hello guys can someone help me... i a program that can add numbers... for example you enter 1 number then when you enter the second number it adds the two inputs.... ive tried my best but this is the only thing i can do... pls help....

    Java Code:
    import java.io.*;
    
    
    public class reciept
    {
    	public static void main(String []args)throws IOException{
    	BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
        String in;
        int input1, total;
        do{
        System.out.println("enter a number: ");
        input1=Integer.parseInt(userInput.readLine());
        total=+input1;
        
        System.out.println(total+"\n");
        
        System.out.println("add another number? [y]yes or [n]no");
       	in=userInput.readLine();
       	if(in.equalsIgnoreCase("y"));
       	if(in.equalsIgnoreCase("n")){System.exit(0);}
        }while(true);
    	}
    	 
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Thanks for using CODE tags. Spacing and indentation are very important -- develop good style habits now! If you develop sloppy habits when you are a beginner, they will be very difficult to break, and you just make things so much harder for yourself. Here is your code, not fixed, but simply re-formatted with proper spacing and indentation. See how much easier it is to read?
    Java Code:
    import java.io.*;
    
    public class reciept
    {
    	public static void main(String []args) throws IOException{
                    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
                    String in;
                    int input1, total;
    
                    do {
                            System.out.println("enter a number: ");
                            input1 = Integer.parseInt(userInput.readLine());
                            total [COLOR="Red"]=+[/COLOR] input1;
        
                            System.out.println(total + "\n");
        
                            System.out.println("add another number? [y]yes or [n]no");
                            in = userInput.readLine();
                            if (in.equalsIgnoreCase("y"));
                            if (in.equalsIgnoreCase("n")) {
                                    System.exit(0);
                            }
                    } while (true);
            }
    }
    Give your class a name that starts with an upper-case letter. If it's an English word, spell it correctly. So change "reciept" to "Receipt".

    Your real error should almost jump out at you, now that the code is formatted properly. Take a good look, and if you still can't find it, ask again.

    Also, this:
    Java Code:
                            if (in.equalsIgnoreCase("y"));
    is not good style. If that is really what you want to do, consider writing it this way:
    Java Code:
                            if (in.equalsIgnoreCase("y")) {
                                    // do nothing
                            }
    The way your code is written right now, it will interpret anything other than "n" or "N" as a "Yes" -- is that what you want?

    -Gary-
    Last edited by gcalvin; 03-16-2010 at 01:30 AM.

  3. #3
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    hehe thank you very much for the advice... hehe ill remember not to be sloppy next time... hehe xD
    Java Code:
    import java.io.*;
    
    public class Receipt
    {
    	public static void main(String []args) throws IOException{
                    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
                    String in;
                    int input1, total;
    
                    do {
                            System.out.println("enter a number: ");
                            input1 = Integer.parseInt(userInput.readLine());
                            total =+ input1;
        
                            System.out.println(total + "\n");
        
                            System.out.println("add another number? [y]yes or [n]no");
                            in = userInput.readLine();
                            if (in.equalsIgnoreCase("y"));{//"Go back to enter another number"}
                            if (in.equalsIgnoreCase("n")) {
                                    System.exit(0);
                            }
                    } while (true);
            }
    }
    TT^TT i still dont know how to add inputs.... Im very slow when it comes to this pls help....

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I edited my above post. Take a good look.

    Also,
    Java Code:
                            if (in.equalsIgnoreCase("y"));{//"Go back to enter another number"}
    The curly braces do nothing there -- get rid of them, as they will only confuse you or somebody else. And I still don't think the logic there is quite what you want. Once you fix the other problem, try running this and see what happens when you are prompted for [y] or [n] but you actually type "x" or "q" or even "[n]" or "No".

    -Gary-
    Last edited by gcalvin; 03-16-2010 at 01:35 AM.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Actually, I was wrong. The curly braces do worse than nothing, as the second one is commented out. Get rid of them.

    -Gary-

  6. #6
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    TT^TT its still the same....
    Java Code:
    import java.io.*;
    
    public class Receipt
    {
    	public static void main(String []args) throws IOException{
                    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
                    String in;
                    int input1, total;
    
                    do {
                            System.out.println("enter a number: ");
                            input1 = Integer.parseInt(userInput.readLine());
                            total =+ input1;
        
                            System.out.println(total + "\n");
        
                            System.out.println("add another number? [y]yes or [n]no");
                            in = userInput.readLine();
                            if (in.equalsIgnoreCase("y"));{//"Go back to enter another number"}
                            if (in.equalsIgnoreCase("n")) {
                                    System.exit(0);
                            }
                    } while (true);
            }
    }
    for example i want to enter "5" for the first try then i press "y" so that i can enter the second number "6".... how can i make it so that the display is "11".............

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Java Code:
                            total =+ input1;
    That says, for example, if input1 has the value 5, then total should be set to +5 (which is 5 -- we don't display the sign for positive numbers). It does not say that total should have its value increased by 5. For that, you need:
    Java Code:
                            total += input1;
    Also,
    Java Code:
                            System.out.println("enter a number: ");
    You don't want println() there -- you want print().

    -Gary-

  8. #8
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    hehe xD wooot!!! thank you very much for helping.... hehe
    Java Code:
    import java.io.*;
    
    public class Receipt
    {
    	public static void main(String []args) throws IOException{
                    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
                    String in;
                    int input1, total=0;
    
                    do {
                            System.out.print("enter a number: ");
                            input1 = Integer.parseInt(userInput.readLine());
                            total+=input1;
        
                            System.out.println(total + "\n");
        
                            System.out.println("add another number? [y]yes or [n]no");
                            in = userInput.readLine();
                            if (in.equalsIgnoreCase("y"));//add another number
                            if (in.equalsIgnoreCase("n")) {
                                    System.exit(0);
                            }
                    } while (true);
            }
    }

  9. #9
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    can i ask another question Mr. gcalvin... can you tell me how I can make a list of the numbers that I inputted? for example:
    -----------------(display)
    input1 = 5.00
    input2 = 6.00

    total = 11.00

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Study about List<> and ArrayList<>.

    -Gary-

  11. #11
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Biginner! Array to LinkedList
    By SungsooKim in forum New To Java
    Replies: 3
    Last Post: 09-28-2009, 08:18 AM
  2. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  3. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 07:22 AM
  4. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 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
  •