Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Problem with Loop

  1. #1
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem with Loop

    I am new to java and in this assignment I need to make a spot to enter the customer's name and amount of kilowatt usage. I need to do this for three people, the first time through it works fine but after that there is no place to put the customer's name during the process. In our class we write it up in notebook and run from the command prompt. The input only appears in command prompt while output shows in the joption pane.

    //
     import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class Electric1//Exercise 1 not complete
    {
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner(System.in);
     
    		String name;
    		int kilowatts;
    		String message = "";
    		double pay;
     
     
    		System.out.print("What is your name?");
    		name =  input.nextLine();
     
    		kilowatts = System.out.print("How many kilowatts did you use?");
     
     
    		pay = kilowatts * .056;
     
    		message = String.format("%s, you owe $%.2f", name, pay);
     
    		JOptionPane.showMessageDialog(null, message);
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Problem with Loop

    kilowatts = System.out.print("How many kilowatts did you use?");

    That doesn't make any sense.

    The method println() is void...which means it doesn't return anything.

    However...this would work...but that's not what you're doing so don't use it....

    kilowats = Integer.parseInt(JOptionPane.showMessageDialog(null, "How many kilowatts did you use?" , "Enter Kilowatt usage", JOptionPane.QUESTION_MESSAGE));

    Your code...using command line...should be:
    System.out.println("How many kilowatts do you use?");
    kilowats = input.nextInt();

    To get output for three people...put your code in the for loop.

    Make sure to initialize all variables before starting the for loop.

    for (int i =0; i < 3; i++)
    {
     
    // put the code you have here and it will go through 3 times.
     
    }
    Last edited by javapenguin; February 1st, 2011 at 11:10 PM. Reason: Oops...made error on the for loop but fixed it. It's less than 3,not 2. My bad.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    Dragonkndr712 (February 3rd, 2011)

  4. #3
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Loop

    Thank you for pointing out about kilowatt, you were right on that point but it still doesn't fix my problem. here is the new code, hopefully you can figure out where I am going wrong.

    //import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class Electric1
    {
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner(System.in);
     
    		String name;
    		int kilowatts;
    		String message = "";
    		double pay;
    		int counter = 1;
     
    		while(counter < 4)
    		{		
     
    			System.out.print("What is your name?");
    			name =  input.nextLine();
     
    			System.out.print("How many kilowatts did you use?");
    			kilowatts = input.nextInt();
     
    			pay = kilowatts * .056;
     
    			message = String.format("%s, you owe $%.2f", name, pay);
     
    			JOptionPane.showMessageDialog(null, message);
     
    			counter++;
    		}
    	}
    }

    I know I am doing something wrong but I can not see it. To me this looks right but like I said I am new to java.
    Last edited by Dragonkndr712; February 3rd, 2011 at 06:07 PM. Reason: code not going into Java area

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem with Loop

    What is your problem?

    What output are you getting?

    What output are you expecting?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Problem with Loop

    If you remove "JOptionPane.showMessageDialog(null, message);" from your code, it will work fine, but I can't figure out why .
    Might be down to not specifying a parent, and its therefore not appearing in the foreground?
    With that in mind, It is late and you should take my guess with a very small pinch of salt ^^.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Loop

    I also have this same exact problem for class. I used the information below and when i go to compile it ask the name and the watts for the first person just fine and then it will ask the name and watts for the second person all at one time. is there any advice that can help me with this?

    Here is my code
    import java.util.Scanner;
     
    public class Electric1
    {
        public static void main(String args[])
        {
            Scanner input = new Scanner(System.in);
     
            String name;
            int kilowatts;
            String message = "";
            double pay;
            int counter = 1;
     
            while(counter < 4)
            {       
    	    //Ask name
                System.out.print("What is your name?");
                name =  input.nextLine();
     
                System.out.print("How many kilowatts did you use?");
                kilowatts = input.nextInt();
     
                pay = kilowatts * .056;
     
                message = String.format("%s, you owe $%.2f", name, pay);
     
    	    JOptionPane.showMessageDialog(null, message);
     
                counter ++;
            }
        }
    }

  8. #7
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Loop

    Output expected is that in command prompt it will ask for the name and how kilowatts. It will then give the answer in the joptionpane. The first customer works fine but when the second time comes up it pushes pass the name and automatically goes to the kilowatt question.

    There has to be a joptionpane because that is the way the teacher wants it. Actually she said this will be how all our programs have to run.

    I have a capture of the command prompt.
    Attached Images Attached Images
    Last edited by Dragonkndr712; February 5th, 2011 at 07:48 PM. Reason: adding info

  9. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problem with Loop

    Quote Originally Posted by Dragonkndr712 View Post
    Output expected is that in command prompt it will ask for the name and how kilowatts. It will then give the answer in the joptionpane. The first customer works fine but when the second time comes up it pushes pass the name and automatically goes to the kilowatt question.

    There has to be a joptionpane because that is the way the teacher wants it. Actually she said this will be how all our programs have to run.

    I have a capture of the command prompt.
    I have the exact same homework. Not sure about your problem but i think its where your "name = input.nextLine(); remove Line and make it "name = input.next();" without quotations. Hopefully that works.

  10. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Loop

    Quote Originally Posted by anthony23415 View Post
    I have the exact same homework. Not sure about your problem but i think its where your "name = input.nextLine(); remove Line and make it "name = input.next();" without quotations. Hopefully that works.
    Tried to do that with no luck..

  11. #10
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problem with Loop

    Quote Originally Posted by Chappell817 View Post
    Tried to do that with no luck..
    This is what I did.

     
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    public class Electric1
    {
        public static void main(String args[])
        {
            Scanner input = new Scanner(System.in);
     
            String name;
            int kilowatts;
            double total;
            int counter = 1;
     
            while(counter < 4)
            {       
            //Ask name
                System.out.println("What is your name?");
                name =  input.next();
                System.out.println("How many kilowatts did you use?");
                kilowatts = input.nextInt();
     
                total = kilowatts * .056;
     
                String message = String.format("%s $%.02f", name, total);
     
                JOptionPane.showMessageDialog(null, message);
     
                counter = counter + 1;
            }
        }
    }

  12. The Following User Says Thank You to anthony23415 For This Useful Post:

    Dragonkndr712 (February 7th, 2011)

  13. #11
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Loop

    Quote Originally Posted by anthony23415 View Post
    This is what I did.

     
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    public class Electric1
    {
        public static void main(String args[])
        {
            Scanner input = new Scanner(System.in);
     
            String name;
            int kilowatts;
            double total;
            int counter = 1;
     
            while(counter < 4)
            {       
            //Ask name
                System.out.println("What is your name?");
                name =  input.next();
                System.out.println("How many kilowatts did you use?");
                kilowatts = input.nextInt();
     
                total = kilowatts * .056;
     
                String message = String.format("%s $%.02f", name, total);
     
                JOptionPane.showMessageDialog(null, message);
     
                counter = counter + 1;
            }
        }
    }
    Thanks i think you help me figure out what i was doing wrong..but not 100% sure..

  14. #12
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Loop

    That worked not sure why but my program works now. Thank you.

  15. #13
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Loop

    got that actually in class now trying to work on the second problem

  16. #14
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problem with Loop

    Quote Originally Posted by Chappell817 View Post
    got that actually in class now trying to work on the second problem
    If you dont mind asking? You go to UTA? and have class at 9:30 am in room 255 for that java class?

Similar Threads

  1. Java Do..while loop problem
    By jwill22 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 13th, 2010, 04:02 AM
  2. Problem with For Loop
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 12:23 PM
  3. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 11:23 AM
  4. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM
  5. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM