Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:55:48+00:00 2026-06-03T08:55:48+00:00

I am reading two files and I want for each line in file one

  • 0

I am reading two files and I want for each line in file one sum up all the lines in file two. So far I have written the code below but it only does it for the first line in the files. Below is my code and sample files. Note I am getting java.util.NoSuchElementException

 import java.io.*;
 import java.util.Scanner;
 import java.util.StringTokenizer;
    public class knn {
    public static void main (String[]args)throws IOException{
      Scanner in = new Scanner(new FileInputStream("knn.txt"));
      Scanner in2 = new Scanner(new FileInputStream("knn2.txt"));
        while(in.hasNextLine()){
           String linetoprocess = in.nextLine();
           StringTokenizer st = new StringTokenizer(linetoprocess, " :");
             while(in2.hasNextLine()){
                String linetoprocess2 = in2.nextLine();
                StringTokenizer st2 = new StringTokenizer(linetoprocess2, " :");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
                System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
               System.out.println("("+st.nextToken()+","+st2.nextToken()+")");

            }
        }
    }

knn.txt

1 1:5.1 2:3.5 3:1.4 4:0.2
1 1:4.9 2:3.0 3:1.4 4:0.2
1 1:4.7 2:3.2 3:1.3 4:0.2

knn2.txt

1 1:5.4 2:3.7 3:1.5 4:0.2
1 1:4.8 2:3.4 3:1.6 4:0.2
1 1:4.8 2:3.0 3:1.4 4:0.1
1 1:4.3 2:3.0 3:1.1 4:0.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:55:50+00:00Added an answer on June 3, 2026 at 8:55 am

    I got it I should have called called the st.nextToken() before the inner while loop and store them in a variable which I can use later. Thanks for your suggestions. The complete code is below.

    import java.io.*;
     import java.util.Scanner;
     import java.util.StringTokenizer;
     public class knn {
     public static void main (String[]args)throws IOException{
        Scanner in = new Scanner(new FileInputStream("knn.txt"));
    
        while(in.hasNextLine()){
            String linetoprocess = in.nextLine();
            StringTokenizer st = new StringTokenizer(linetoprocess, " :");
            Scanner in2 = new Scanner(new FileInputStream("knn2.txt"));
            String a = st.nextToken();
            String b = st.nextToken();
            String c = st.nextToken();
            String d = st.nextToken();
            String e = st.nextToken();
            String f = st.nextToken();
            String g = st.nextToken();
            String h = st.nextToken();
            String i = st.nextToken();
    
            while(in2.hasNextLine()){
            String linetoprocess2 = in2.nextLine();
            StringTokenizer st2 = new StringTokenizer(linetoprocess2, " :");
            System.out.print("("+a+","+st2.nextToken()+"), ");
            System.out.print("("+b+","+st2.nextToken()+"), ");
            System.out.print("("+c+","+st2.nextToken()+"), ");
            System.out.print("("+d+","+st2.nextToken()+"), ");
            System.out.print("("+e+","+st2.nextToken()+"), ");
            System.out.print("("+f+","+st2.nextToken()+"), ");
            System.out.print("("+g+","+st2.nextToken()+"), ");
            System.out.print("("+h+","+st2.nextToken()+"), ");
            System.out.println("("+i+","+st2.nextToken()+")");
    
            }
        }
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. I'm reading from one sql-format file to another, and two bytes in
I want to parse two files. I have Yacc/lex code which generates the parser.
I'm writing a program reading a file containing two values in each line. Those
I have two threads, one reading streams and producing data-objects from their content. I
I was reading another question pertaining the efficiency of two lines of code, and
I have a set of elements/keys which I'm reading from two different config files.
I have a huge line-separated text file and I want to make some calculations
I wish to: Reading in two files Split the files into individual strings Compare
So I'm reading a CSV file with two columns, and then taking the product
I am reading data from a file that has, unfortunately, two types of character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.