Results 1 to 2 of 2
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Confusing input question

    So I have these questions that are all based off the same structure. Except I am heavily confused on how to go about doing this. I can only think of reading the whole line not each work and this stupid online question does not know what hasNext() method is... I really have no idea on how to do this. Can anyone give me any ideas?
    Oh and this is what is in dogyears.txt

    Tippy 2
    Rex 7
    Desdemona 5




    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this:

    Tippy 2
    Rex 7
    Desdemona 5

    Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will result in the desired output.



    Java Code:
     String name1, name2, name3;
      int age1, age2, age3;
      Scanner inputData = new Scanner(new FileReader("dogYears.txt"));
    My code goes here


    Java Code:
      System.out.println(name1+" "+age1);
      System.out.println(name2+" "+age2);
      System.out.println(name3+" "+age3);

    }
    Last edited by jigglywiggly; 03-13-2009 at 04:38 AM.

  2. #2
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Nevermind got it :D that was easier than I thought :P
    Java Code:
    name1=inputData.nextLine();
    String d =inputData.nextLine();
    age1 = Integer.parseInt(d);
    
    name2=inputData.nextLine();
    String e =inputData.nextLine();
    age2=Integer.parseInt(e);
    name3=inputData.nextLine();
    String j = inputData.nextLine();
    age3=Integer.parseInt(j);

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Simple code, but confusing
    By t.prasanna in forum New To Java
    Replies: 5
    Last Post: 01-22-2009, 10:26 AM
  3. Confusing IndexOutOfBounds Error?
    By tibbyuk in forum New To Java
    Replies: 2
    Last Post: 08-08-2008, 11:42 PM
  4. Confusing with Frameworks
    By hisouka in forum New To Java
    Replies: 0
    Last Post: 08-05-2008, 05:52 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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