Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need help with Scanner/while loops/substrings

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Scanner/while loops/substrings

    Hi, I am a newbie programmer and I have an assignment due tomorrow that I haven't been able to figure out.

    We are given a .txt file that contains NBA players names followed by some numbers that represent stats on the same line.
    The program is simple. Ask the user for a player name (using JOptionPane) and the program spews out that players stats, the stats of the top player in the league and the average stats. The stats are calculated into an arbitrary made up "total stat" which I can easily calculate once I have all the individual ints.

    The .txt file is in the following format:

    NBA Player Name int1 int2 int3 int4 int5 int6 int7
    NBA Player Name int1 int2 int3 int4 int5 int6 int7
    NBA Player Name int1 int2 int3 int4 int5 int6 int7
    NBA Player Name int1 int2 int3 int4 int5 int6 int7
    NBA Player Name int1 int2 int3 int4 int5 int6 int7

    etc.

    The .txt file is read in using Scanner and we have not learned how to use arrays so I can't use those. We are supposed to be able to do this with String methods and using scanner and for/while loops.

    I have something like this
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class GortemakerPeterA2Q3 {
     
     
      public static void  main(String[]  args)  throws  FileNotFoundException { 
     
        Scanner inputFile = new Scanner(new File("nba-stats.txt"));
        Scanner readFile = new Scanner(System.in);
        Scanner lineScan;
     
        String playerName = JOptionPane.showInputDialog("Which NBA players P1010 score would you like to see?");
        final int P_1010 = 2;
     
        String playerName2, line, stats;
        int length;
        String name = "";
     
        }
        while(!inputFile.hasNextInt()){
     
          name += " " + inputFile.next();
          statsStart = name.length();
          stats
    I am panicking because I have no idea what I'm doing and my teacher hasn't responded to my email and I don't know anyone personally in my class. How do I separate the ints and names of each individual player? How can I take what the user inputs and have the program find that name and its associated stats and pull out JUST that player and his stats? I'm entirely lost.
    Last edited by BongoPeg; June 16th, 2014 at 07:52 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Scanner/while loops/substrings

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    How do I separate the ints and names of each individual player?
    If the names and numbers are on one line, the String class's split() method can be used to separate them into a String array. Once in the array you caj access each value as an element of the array. The Integer class's parse method can be used to convert String's to int values.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Scanner/while loops/substrings

    We have not learned how to use arrays. I could figure it out but we are supposed to complete the assignment without arrays.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Scanner/while loops/substrings

    Ok, another way to scan the contents of a String with several tokens (name and numbers) is to use the Scanner class's constructor that takes a String. Then use the Scanner class's methods to read the tokens from the String passed to its constructor. You can have more than one Scanner class object in the program: one to read the lines from the file and the other to read the tokens from the line that was read.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Scanner/while loops/substrings

    My problem is that the .txt file is huge file with a lot of names and numbers. I don't know how to draw a specific line (one players name and his stats) out of the whole file. It is exactly as shown and there are hundreds of entries. Also I'm only learning the basics right now so what might sound simple to you is difficult for me to grasp at this point.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with Scanner/while loops/substrings

    how to draw a specific line (one players name and his stats) out of the whole file.
    If you can't save the file's data in an array or other type of storage structure, the code will have to read the file each time a new player's data is to be found.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Scanner/while loops/substrings

    I think that was the point. You have to re run the short program for every name you put in. It isn't supposed to be beautiful or efficient. It's supposed to force us to use certain methods. This is was at the top of my assignment:

    Material Covered:
    •If-statements
    •Loops (while, do-while, for)
    •Nested structures
    •Simple string methods
    o length(), charAt(), equals(), equalsIgnoreCase(), toLowerCase(), toUpperCase()

    There was a part 1 to my assignment that I have completed already and it was all about using piles of if/else statements.

    I think it's supposed to be as basic as possible.

Similar Threads

  1. Replies: 6
    Last Post: March 12th, 2014, 12:43 PM
  2. Question about substrings
    By hkfrenchtoast in forum Java Theory & Questions
    Replies: 2
    Last Post: February 15th, 2014, 01:06 AM
  3. Replies: 1
    Last Post: April 26th, 2012, 10:06 AM
  4. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM