AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Java read input10/2/2023 answer = keyboard.nextLine().toUpperCase().charAt(3) Would return the 3rd character in the array, and the following line would be equivalent. answer = keyboard.nextLine().toUpperCase().toCharArray() So if desired, for short hand, or another project, you could write something like this, that would get the input, convert it to uppercase, convert it to a character array, and select one element from said array all in one line. answer = keyboard.nextLine().toUpperCase().toCharArray() Ĭould return an array that looks like this: => 'T', => 'E', => 'S', => 'T' (strings are really just objects that hold an array of characters, so in reality, you could also call. answer = keyboard.nextLine().toUpperCase().charAt(0) Then charAt(i) gets the character at that place in the string. toUpperCase(), depending on what's needed answer = keyboard.nextLine().toLowerCase() You'd want to do this to help with selections (I.E., want to continue? (y/n)) That way regardless of case, if the user enters y or n you get the same result. Then you can add in a to lowercase, or to uppercase. Then you get the "next line" which is every character until the next newline character. Input is your scanner object that is using System.in Scanner keyboard = new Scanner(System.in) Or, alternately answer = keyboard.nextLine().toUpperCase().charAt(0) What you're looking for is something along the lines of this answer = keyboard.nextLine().toLowerCase().charAt(0) Input = keyboard.nextLine() // Get a line of input.Īnswer = input.charAt(0) // Get the first character.Įxception in thread "main" : String >index out of range: 0Īt (Unknown Source) ("How many hours did you work this week? ") Get the number of hours worked this week. Scanner keyboard = new Scanner(System.in) * This program demonstrates the Scanner class.Ĭhar answer //To hold a single character ![]() The book explains it step by step, so I added the new lines to an already existing program called Payroll.java, and the original program works, but it won't let me input anything after the new lines are printed. Nevertheless, I'm stuck in the section called "Reading Keyboard Input," to be more specific, "Reading a Character." The book explains that the Scanner class does not have a method to read a single character, therefore the nextLine method is used to read a string from the keyboard, you enter a single letter, then the charAt method is used to extract the first character of the string. I took it upon myself to start over and read the textbook page by page, and sure enough, many of the things I did not initially understand make sense now, and most of the programs that wouldn't work before now do. He also gives us examples to programs and then assigns programs that are far more complex than what he showed us in class. The professor seems to just "do" in class and not really instruct. I'm taking an introductory class to programming using Java and I've already fallen behind.
0 Comments
Read More
Leave a Reply. |