Results 1 to 20 of 23
Thread: Help with String I/O
- 09-01-2010, 01:36 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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.
- 09-01-2010, 01:42 AM #2
The compiler can not see where you have given the variable c1 a value before you use it.variable c1 might not have been initialized
Where do you assign a value to the variable c1?
Please enclose your posted code in code tags. [code ] & [/code ] no spaces
- 09-01-2010, 01:45 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Oh I see. I have to assign c1 to equal something?
- 09-01-2010, 01:48 AM #4
Yes, that will make the compiler happy.
Chances are that its value won't be 'y' and your while loop will never execute.
- 09-01-2010, 01:51 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
So how can I make the program repeat itself? Do you understand what I want to do?
- 09-01-2010, 01:53 AM #6
No, I don't see any comments in the code explaining what it is supposed to do.Do you understand what I want to do?
- 09-01-2010, 01:55 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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. :/
- 09-01-2010, 02:08 AM #8
What happens when you execute the program?
- 09-01-2010, 02:11 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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'.
- 09-01-2010, 02:16 AM #10
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.
- 09-01-2010, 02:22 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
When I run the program its runs until it gets to "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?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); } } }
- 09-01-2010, 02:24 AM #12
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.
- 09-01-2010, 02:25 AM #13
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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.
- 09-01-2010, 02:34 AM #14
Try this to see what is in the String:
System.out.println("s1=" + s1 + "<, length=" + s1.length());
- 09-01-2010, 02:42 AM #15
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
It says that "<" is in the String and its length is 0.
- 09-01-2010, 02:49 AM #16
Look again at what the println() statement prints. The < was used as a right-hand delimiter incase there were spaces in the String.
- 09-01-2010, 02:52 AM #17
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
It only prints "s1=<, length=0".
- 09-01-2010, 02:53 AM #18
Exactly. The contents of the String is between the = and the <. It's empty. The length=0 says the same thing.
- 09-01-2010, 03:00 AM #19
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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.
- 09-01-2010, 03:06 AM #20
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.
Similar Threads
-
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM -
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks