Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this Lab: Modify the code for LineNumberer so that the input fi

ID: 3734675 • Letter: I

Question

I need help with this Lab:

Modify the code for LineNumberer so that the input file comes from a different project, using a relative path to identify it. For example, run LineNumberer on the source file Deck.java

This is LineNumberer:

package lab8;
import java.util.Scanner;

public class LineNumberer
{
public static void main(String[] args)
{
    Scanner scanner = new Scanner(System.in);
    int lineCount = 1;

    while (scanner.hasNextLine())
    {
      String line = scanner.nextLine();
      System.out.print(lineCount + " ");
      System.out.println(line);
      lineCount += 1;
    }
   
    System.out.println("Done");
}
}

And this is Deck.Java:

package lab7;
import java.util.Random;

import lab7.Card.Suit;

/**
* Class representing a standard 52-card deck of playing
* cards from which cards can be selected at random.
*/
public class Deck
{
/**
   * The cards comprising this deck.
   */
private Card[] cards;

/**
   * The random number generator to use for selecting cards.
   */
private Random rand;

/**
   * Constructs a new deck with a default random number generator.
   */
public Deck()
{
    rand = new Random();
    init();
}

/**
   * Constructs a new deck with the given random number generator.
   */
public Deck(Random givenGenerator)
{
    rand = givenGenerator;
init();
}

/**
   * Returns a new array containing k elements selected
   * at random from this deck.
   */
public Card[] select(int k)
{
    // TODO
    return null;
}

/**
   * Initializes a new deck of 52 cards.
   */
private void init()
{
    cards = new Card[52];
    int index = 0;
    for (int rank = 1; rank <= 13; ++rank)
    {
      cards[index] = new Card(rank, Suit.CLUBS);
      index += 1;
      cards[index] = new Card(rank, Suit.DIAMONDS);
      index += 1;
      cards[index] = new Card(rank, Suit.HEARTS);
      index += 1;
      cards[index] = new Card(rank, Suit.SPADES);
      index += 1;
    }

}
}

Explanation / Answer

your written code is pretty much clear. you just have to change the path of file. in your code it is asking your to give input using standard input devices but if you change that thing with input file then it gets input from files on your storage.

NOTE:- 1: use // instead of /

2:use File to store path if you directly give path in scanner in double quotes then it acts as string to scanner not as a file thats why we used File file = new File("exact path of file");  

---------------------------------------

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class LineNumberer

{

public static void main(String[] args) throws FileNotFoundException

{ File file = new File("D:\abc.java");

Scanner scanner = new Scanner(file);

int lineCount = 1;

while (scanner.hasNextLine())

{

String line = scanner.nextLine();

System.out.print(lineCount + " ");

System.out.println(line);

lineCount += 1;

}

System.out.println("Done");

}

}