Results 1 to 5 of 5
  1. #1
    popeyito18's Avatar
    popeyito18 is offline Member
    Join Date
    Jun 2011
    Location
    USA
    Posts
    17
    Rep Power
    0

    Post Confusion with variable types

    the program should do the following




    this is my code (The code if part of a while loop, this is only one part of it) and it's supposed to do what's on the picture. I have problems with the variables and finding ways to make it work.

    Java Code:
    while (option1 ==3)  //if users picks option 3 the following codes will be executed
               
                {
                 String empty; System.out.print(" "); //Program did not run so I created an empty variable
                 empty = keyboard.nextLine(); // for reasons I don't know it worked this way
                 
                 String name2;
                //creating scanner for input
                System.out.println("Enter customer's name ");
                name2 = keyboard.nextLine(); //name2 contains the name you want to match with the
                                                         // one on the text file. 
                                                      //The file was created earlier in the program
              
                //obtaining the desired product from customer   
                
                     System.out.println("what product are you buying? \n1.Shoes \n2.T-Shirts \n3.Shorts   \n4.Caps \n5.Jackets");
                product = keyboard.nextInt(); //obtaining the customer's selection
                             
                int price = 1;
                switch (product) {
                  case 1:
                    price = 50; break; //options depending of the customer's selection
                case 2:
                    price = 30; break;
                case 3:
                    price = 75; break;
                case 4:
                    price = 15; break;
                case 5:
                    price = 100; break;
                  default:
                    System.out.println("Wrong choice");
                }
                
             
                
                System.out.println("How many do you want?");
                quantity = keyboard.nextInt(); //quantity purchased
        
                
                
                tax = .0825; //8% of tax
                total = (product*quantity*tax);
                TOTAL = Totalt + product;                      
                System.out.println(total);
                System.out.println(TOTAL);
                
                String cinfo = ("customerInfo.txt"); //assigning the name of the file to the variable cinfo
               
                File file = new File(cinfo); //file is ready to display
                Scanner inputFile = new Scanner(file);
                String name3 = inputFile.nextLine(); // name3 contains the name in the fil
                inputFile.close();
                //compare name2 & name3
                //if names are the same display the content of the file, otherwise error message
                //use if-statements for this step
                
                if (name2.equals(name3))
                {
                  File file2 = new File(cinfo); //opening file
                  Scanner inputFile2 = new Scanner(file2);
                  while (inputFile.hasNext()) //loop to display all the lines in the text file
                  {
                    String alltext = inputFile.nextLine();
                  // displaying the content of the file
                    System.out.println(alltext);
                  }
                }
                else 
                { System.out.println("Could not find name in the system ");
                
                }
    }
    Help is appreciated. I know it's long. I'm sorry for that.

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    What output are you getting? post any error messages also

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You didn't tell us the problem at all. Is it compiling? If not whats the full error message. If it is and its not producing the expected output, show us the expected output and the actual output so we can see what's going on in your code.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    popeyito18's Avatar
    popeyito18 is offline Member
    Join Date
    Jun 2011
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default

    The program should ask the user for a name. Then what product he wants to buy and the quantity.
    If the name entered matches the name in text file (already created and with information about a customer) it should display the complete information about the customer and total. The product * quantity*tax.

    The program should do this.


    But it is doing this. I am new to java so I'm sorry if not giving the details needed to help me.



    I can't defined the variable price depending on the user's selection.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The problem may be due to your keyboard scanner object not handling end of line tokens correctly. Consider adding keyboard.nextLine() after each keyboard.nextInt() statement. For example, instead of this:

    Java Code:
    System.out.println("How many do you want?");
    quantity = keyboard.nextInt(); //quantity purchased
    do this:
    Java Code:
    System.out.println("How many do you want?");
    quantity = keyboard.nextInt(); //quantity purchased
    [color="red"][b]keyboard.nextLine(); // to swallow the end of line token[/b][/color]

Similar Threads

  1. Unknown Variable Types
    By Slayer in forum New To Java
    Replies: 6
    Last Post: 10-25-2010, 05:44 AM
  2. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 04:42 PM
  3. variable number of fields with different types
    By Sir G. in forum Advanced Java
    Replies: 11
    Last Post: 03-29-2010, 01:40 AM
  4. Dynamic types for a variable.
    By Somelauw in forum New To Java
    Replies: 5
    Last Post: 11-27-2009, 11:38 AM
  5. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 02:47 AM

Posting Permissions

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