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 8 of 8

Thread: An array problem...

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

    Default An array problem...

    I will make a program that read floats from a textfile.
    The floats in the file will be ordered like this on the same row seperated by semicolons: 23.5;355.5;21.3;76.2...

    I have been reading forums and other places online up and down about arrays and how to "split" out the semicolons so that it just read the floats, but i just dont get this right...

    1. open the textfile <- this part i get right but when reading the textfile, it presents everything as string...
    2. read and calculate biggest and smallest float-number.
    3. present biggest and smallest float-number to the user.

    I feel lost and angry...

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An array problem...

    If the data is read from the file as a String with values separated by a character like a ;
    use the String class's split method to create an array of Strings containing the values between the ;s
    Use the Arrays class's toString method for debugging to see what is in the array:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    Once the data is isolated, use the Double class's parseDouble method to convert the String in each slot of the array to double.

    Be sure to wrap all posted code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: An array problem...

    Quote Originally Posted by Norm View Post
    If the data is read from the file as a String with values separated by a character like a ;
    use the String class's split method to create an array of Strings containing the values between the ;s
    Use the Arrays class's toString method for debugging to see what is in the array:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    Once the data is isolated, use the Double class's parseDouble method to convert the String in each slot of the array to double.

    Be sure to wrap all posted code in code tags.
    Well, i get to the point when i have the terminal presenting each number without the semicolon and that is what i want... But from this point, how do i convert the String from the file to floats correctly?
    Here is what i got, but when i do a parseFloat all hell breaks loose in the terminal even though there is no syntax error when compiling...

    //
    public class FileRead
    {
    public static void main(String[] args) throws IOException
    {
    File file = new File("Numbers.txt");
    Scanner inputFile = new Scanner(file);
    float highestNumber;
    float lowestNumber;
    float allaNummer;
    String floatNumber;



    while (inputFile.hasNext()){
    floatNumber = inputFile.nextLine();


    String[] numberSplit = floatNumber.split(";");


    for(int i = 0;i <numberSplit.length; i++)


    System.out.println(numberSplit[i]);

    }

    inputFile.close();
    //

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An array problem...

    how do i convert the String from the file to float
    See the Float class's parseFloat method.

    when i do a parseFloat all hell breaks loose
    Please copy the full text of any error messages and paste it here.
    Also post the code that is causing the errors.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Unhappy Re: An array problem...

    Yes, i did the correct way to do a parseFloat, but i just dont know where in the code to do the "conversion" from String to float the right way...

    public class FileRead
    {
        public static void main(String[] args) throws IOException
        {
            File file = new File("Numbers.txt");  
            Scanner inputFile = new Scanner(file);
            float highestNumber;
            float lowestNumber;
            float allaNummer;
            String floatNumber;
     
     
     
            while (inputFile.hasNext()){
                floatNumber = inputFile.nextLine();
                String[] numberSplit = floatNumber.split(";");
     
                for(int i = 0;i <numberSplit.length; i++)
                System.out.println(numberSplit[i]);
     
     
            }
     
     
            inputFile.close();

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An array problem...

    where in the code to do the "conversion"
    After the String to be converted is available. What is supposed to be done with the float value?
    Currently the code only prints the split Strings.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: An array problem...

    Yes, now it only prints the numbers as i want them to be presented, but i have not come up with the correct way to convert it to floats...

    Now it reads a file and

    Splits out the ";" and presents only the numbers as i want them to be presented like this:
    44,5
    77,5
    43,1

    But i wonder, this is where i get lost, as i want to convert that to floats. Where should i do the parseFloat and with what variable ...the inputFile? I've tried this amongst others: "allaNummer = Float.parseFloat(inputFile);"
    public class FileRead
    {
        public static void main(String[] args) throws IOException
        {
            File file = new File("Numbers.txt");  
            Scanner inputFile = new Scanner(file);
            float highestNumber;
            float lowestNumber;
            float allaNummer;
            String floatNumber;
     
     
     
            while (inputFile.hasNext()){
                floatNumber = inputFile.nextLine();
                String[] numberSplit = floatNumber.split(";");
                //allaNummer = Float.parseFloat(floatNumber); < - no syntax error, but not working excecuting
                for(int i = 0;i <numberSplit.length; i++)
                System.out.println(numberSplit[i]);
     
     
            }
     
     
            inputFile.close();

    As you might tell, im really new at this and it makes me think that there must be an easier way to do it rather than the way im doing it....
    All i want it to do is to read the file, convert to float and present highest and lowest value...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: An array problem...

    The parse method should be used with the individual Strings created by the split method, not the full String that includes all the numbers.
    The println statement prints out the String that should be passed to the parse method.


    All i want it to do is to read the file, convert to float
    You left out the step to isolate each of the numbers in the String read from the file before converting that isolated String to float.
    If there were only ONE number on each line, then the posted code would work. However there is more than one separated by commas. So the code needs to get each number separately using the split method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Array Problem
    By Stark in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 14th, 2014, 06:01 PM
  2. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. array problem
    By aizen92 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 11:06 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM