Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default Help with String I/O

    I keep on getting this error message every time I compile. I can't figure out the error!

    C:\Documents and Settings\HP_Administrator\Desktop\Momentum.java:16 : variable c1 might not have been initialized
    while(c1 == 'y') {
    ^
    1 error

    Tool completed with exit code 1

    The up arrow should be under the "c".


    Source code:

    Java Code:
    import java.util.Scanner;
    
    class Momentum
    {
    	public static void main(String [] args)
    		{
    			char c1;
    			String s1;
    			Scanner reader = new Scanner(System.in);
    
    			while(c1 == 'y') {
    
    			double mass;
    			double velocity;
    			double momentum;
    
    			System.out.print("Enter mass in kilograms (kg): ");
    			mass = reader.nextDouble();
    
    			System.out.println();
    
    			System.out.print("Enter velocity in meters per second (m/s): ");
    			velocity = reader.nextDouble();
    
    			System.out.println();
    
    			momentum = mass * velocity;
    
    			System.out.println("The object's momentum is " + momentum +".");
    
    			System.out.println();
    			 }
    
    			System.out.println("Again? 'y' for yes, 'n' for no.");
    
    			s1 = reader.nextLine();
    			c1 = s1.charAt(0);
    
    	    }
    }
    Last edited by javaman1; 09-01-2010 at 02:18 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    variable c1 might not have been initialized
    The compiler can not see where you have given the variable c1 a value before you use it.
    Where do you assign a value to the variable c1?


    Please enclose your posted code in code tags. [code ] & [/code ] no spaces

  3. #3
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Oh I see. I have to assign c1 to equal something?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Yes, that will make the compiler happy.
    Chances are that its value won't be 'y' and your while loop will never execute.

  5. #5
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    So how can I make the program repeat itself? Do you understand what I want to do?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Do you understand what I want to do?
    No, I don't see any comments in the code explaining what it is supposed to do.

  7. #7
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Well, basically after the program has run, I want the user to be able to input either 'y' to run it again or 'n' to exit. Understand now? Sorry I haven't put in any comments. :/

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    What happens when you execute the program?

  9. #9
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    The program calculates the momentum of a certain object by multiplying its mass by it velocity. The user inputs both. Once the computer finds the right answer, it will display it. After the computer displays the answer, I want the user to be able to decide whether to run it again by entering 'y' or exit the program by entering 'n'.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    What happens when you execute the program?

    What value did you give c1?

    Have you played computer with your program? Trace the steps it takes and the values it gives to variables as it executes.

  11. #11
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    When I run the program its runs until it gets to "c1 = s1.charAt(0);".

    Java Code:
    import java.util.Scanner;
    
    class Momentum2
    {
    	public static void main(String [] args)
    		{
    			char c1 = 'y';
    			String s1;
    
    			while(c1 == 'y') {
    
    			Scanner reader = new Scanner(System.in);
    
    			double mass;
    			double velocity;
    			double momentum;
    
    			System.out.print("Enter mass in kilograms (kg): ");
    			mass = reader.nextDouble();
    
    			System.out.println();
    
    			System.out.print("Enter velocity in meters per second (m/s): ");
    			velocity = reader.nextDouble();
    
    			System.out.println();
    
    			momentum = mass * velocity;
    
    			System.out.println("The object's momentum is " + momentum +".");
    
    			System.out.println();
    			System.out.println("Again? 'y' for yes, 'n' for no.");
    			s1 = reader.nextLine();
    			c1 = s1.charAt(0);
    			}
    	    }
    }
    I am getting an error when I execute the program. It tells me the String is "out of range of 0". How can I fix this?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    What is in the String? Is it empty? If so, there is no char at index 0.
    Test the length of the String by using its length method.

  13. #13
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    The string isn't empty. If you look at my code, near the end of the while loop, I defined the String by using the Scanner.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Try this to see what is in the String:
    System.out.println("s1=" + s1 + "<, length=" + s1.length());

  15. #15
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    It says that "<" is in the String and its length is 0.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Look again at what the println() statement prints. The < was used as a right-hand delimiter incase there were spaces in the String.

  17. #17
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    It only prints "s1=<, length=0".

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Exactly. The contents of the String is between the = and the <. It's empty. The length=0 says the same thing.

  19. #19
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Then can you please look at my CODE? I am trying to define the String with the Scanner but the Scanner won't read the line. When I execute the program, it seems as though its skips the step of allowing the Scanner to read the line I want to put characters into. All it does is return to the beginning of the while loop.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    The Scanner class uses a buffer to hold a line of data it reads in. When you use a nextXXX method to get some data, it retrieves it from the buffer. It doesn't read the newline character at the end of the buffer with each read. Your nextLine() is reading the newline character that remains in the buffer.

    To get a feel for how the Scanners methods work, write a small program with a loop, different Scanner nextXXXX methods and try entering different data, different amounts of data on a line and mixing up the calls to nextInt, nextDouble, next and nextLine to see how to use all the different methods. Also use some hasXXX methods with println()s to show what they return.
    There are a lot of different combinations to work thru.

    When you understand how to use those methods, you'll be able to use them in your program.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  2. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  3. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Tags for this Thread

Posting Permissions

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