Results 1 to 10 of 10
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default question on input in java?

    Hey I am trying to learn input from my book and I wrote this code but it keeps saying could not find the main input in it. Could you guys help me out? Thanks, and heres the code.
    Java Code:
     import java.io.*;
    class bytereader {
      public static void main(String[] args)   
        throws IOException {
         byte data[] = new byte[10];  //I have a bunch of questions here: Why do we need the brackets, and why the 10 in it? also, what is it even doing in this line? 
       System.out.println("Hey, enter some characters.");
      System.in.read(data);
     System.out.println("You entered: " + data);
      }
    }

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: question on input in java?

    For your first question, read up on Arrays.

    Your second question. Are you sure you copied everything and did it correctly? What book are you using?

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: question on input in java?

    Yeah, i did. What else could be wrong..?

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: question on input in java?

    It's working for me. Can you copy and paste the entire error?

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: question on input in java?

    Hi, it worked, and compiled, but when I entered characters, it gave me: Hey, enter some characters.
    MW: hey!!!
    You entered: [B@3a0fbd45
    weird... Also, I know arrays, but how does it relate to this? I still don't understand why there are brackets... Please help :(

  6. #6
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: question on input in java?

    How do you declare and use arrays?

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: question on input in java?

    You name the array, then set it equal to new and then its value type, and then in brackets are how many values it can have. Then, you declare its values. But why do you need 10 values?
    and why does it say byte data[] and not just byte data? Thanks wnt2bsleepin!

  8. #8
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: question on input in java?

    Good, in the future though type out the code instead of explaining it
    I don't know why he chose 10 values, or why he chose the byte data type. I also don't know why he is having you do

    Java Code:
    System.out.println("You entered: " + data);
    Which won't print out any of the information in the array. He wanted you to use an array, for which reason I don't know. It's probably in your book.

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: question on input in java?

    Lol ok. I see. Its the book that oracle made I think. I guess i'll just learn it later or something. But I really, really appreciate your help. You have answered almost all of my questions. Thank you.

  10. #10
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: question on input in java?

    You can use a statement like the following to create an array with 10 elements which are zero-filled.

    Java Code:
    byte[] data = new byte[10];
    That make array 10 bytes long. Reading into a byte array seems like a low-level, maybe advanced thing to do. I did get the code working using the following code.

    Java Code:
    import java.io.*;
    class bytereader {
    	public static void main(String[] args) throws IOException {
    		byte data[] = new byte[10];  //I have a bunch of questions here: Why do we need the brackets, and why the 10 in it? also, what is it even doing in this line? 
    		System.out.println("Hey, enter some characters.");
    		System.in.read(data);
    		System.out.println("You entered: ");
    		for (int i = 0; i < data.length; i++) {
    			char ch = (char)data[i];
    			System.out.print(ch);
    		}
    	}
    }
    Before, in your code, when it tried to print the whole array, it printed something weird because a byte array is an object and it's class hasn't overrided the toString() method. So for arrays, when you want to print them out, just make a for-loop and print out the individual elements.

    For practical purposes, getting data from the user, using the console, you should use a Scanner object. For one thing characters are not always a byte long, for another, having an array of 10 bytes limits the code. What happens if you enter 11 characters? Either the input will be truncated or an exception will be thrown.

    The following code shows how to use a Scanner object to get input from the user:

    Java Code:
    import java.util.Scanner;
    
    public class ScannerTest {
    	
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("Enter a few characters");
    		String string = scanner.next();
    		System.out.println(string + " was entered");
    	}
    
    }

Similar Threads

  1. User input to Array Question
    By LukasHopkins in forum New To Java
    Replies: 5
    Last Post: 08-14-2012, 01:12 PM
  2. Replies: 2
    Last Post: 04-26-2012, 05:59 AM
  3. Question about Keyboard input
    By littlebirdpoo in forum New To Java
    Replies: 3
    Last Post: 12-17-2011, 04:33 PM
  4. Very basic question regarding user input
    By fugazi in forum New To Java
    Replies: 6
    Last Post: 01-06-2011, 07:40 PM
  5. Confusing input question
    By jigglywiggly in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 04:38 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
  •