Question

Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, ...

Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, nextInt, next, next, and nextLine. Then I have to instantiate a User object using those 5 fields and insert that object in the usersArr. I just need to see what this would look like if a text file with 5 fields for 5 users existed. Here's my code so far:

int [] usersArr = new int[200];
  
       System.out.print("Enter file name: ");
       Scanner input = new Scanner(System.in);
       String inputFile = input.nextLine();  
       File myFile = new File(inputFile);
       Scanner scan = new Scanner (myFile);  
       int LineCount = 0;
       while(scan.hasNextLine())
       {
           scan.nextInt();
           scan.nextInt();
           scan.next();
           scan.next();
           scan.nextLine();
           for (int i = 0; i <= 200; i++)
           {
           usersArr[i] = new User();
           }
          
       }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.util.Scanner;

class User {
   private int a1;
   private int a2;
   private int a3;
   private int a4;
   private int a5;

   public User(int aA1, int aA2, int aA3, int aA4, int aA5) {
       super();
       a1 = aA1;
       a2 = aA2;
       a3 = aA3;
       a4 = aA4;
       a5 = aA5;
   }

   @Override
   public String toString() {
       return a1 + "," + a2 + "," + a3 + "," + a4 + "," + a5 + "\n";
   }

}

public class TestUser {
   public static void main(String[] args) throws Exception {
       User[] usersArr = new User[5];

       System.out.print("Enter file name: ");
       Scanner input = new Scanner(System.in);
       String inputFile = input.nextLine();
       File myFile = new File(inputFile);
       Scanner scan = new Scanner(myFile);
       int LineCount = 0;
       while (scan.hasNextLine()) {

           usersArr[LineCount++] = new User(scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(),
                   scan.nextInt());
       }
       for (int i = 0; i < 5; i++)
           System.out.println(usersArr[i]);
   }
}

E Console 3 temid> TestUser Java Application] C1Soft PegaEcli Enter file 1,2,3,4,5 name: input.txt 2,3,4,5,6 10,20,30,40,50 1

Add a comment
Know the answer?
Add Answer to:
Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Hey Guys , I need help with this assignment: This component of the final exam will...

    Hey Guys , I need help with this assignment: This component of the final exam will have two data structures. The two data structures will be a stack and the other will be a hashmap. The hashmap object will be added to the stack every time the user picks an answer. The hashmap will have a key and value. The key will be the timestamp (system time when the user selected a choice) and the value will be the choice...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

  • Hey I really need some help asap!!!! I have to take the node class that is...

    Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • Hi , i have facing problems in both 5 error while i am soloving it can...

    Hi , i have facing problems in both 5 error while i am soloving it can you help me with good solotion for both pleas pleas ??? a- Write a java class to define the new data type Item. The data type Item is defined by the attributed name of type String) and price of type double). Provide the following methods in the class implementation: • Default constructor (that initializes the attribute name to the value “Unknown”, and the attribute...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • This is the code we have to edit, i know how to do everything except finding...

    This is the code we have to edit, i know how to do everything except finding if the x and y are in the space, please explain to me when you do the code. We are using addshape() api and more. public class Skeleton { public static void main(String[] args) { //================================ //== Setting up game window ====== SpaceGame myGame; //declaring the object form the class Scanner scan = new Scanner(System.in); System.out.println("What is the window name? "); //window title text...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT