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 8446609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:58:40+00:00 2026-06-10T09:58:40+00:00

I am creating a program that saves the first X prime numbers but when

  • 0

I am creating a program that saves the first X prime numbers but when I save the ArrayList to a text file the list is completely wrong. Here is my code:

import java.util.Scanner;
public class Class1
{
  // This method tests whether a given number is prime or not.
   public boolean isPrime ( int num )
 {
boolean prime = true;
int limit = (int) Math.sqrt ( num );  

for ( int i = 2; i <= limit; i++ )
{
  if ( num % i == 0 )
  {
    prime = false;
break;
  }
}

return prime;
}

public static void main ( String[] args )
 {
  Class1 ob1=new Class1();
System.out.println("Where do you want to stop?");
Scanner scan = new Scanner(System.in);
int y = scan.nextInt();
for ( int i = 2; i <= y; i++ )
{
  if ( ob1.isPrime ( i ) )
System.out.println ( i );
  }
 }
 }

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;


 public class Output {
    public static void main(String args[]) throws IOException {
 Class1 obj = new Class1();OutputStream outputstream = new FileOutputStream("write.txt");
 Writer writer1 = new OutputStreamWriter(outputstream);

 try{

 int max =100;
  List<Integer> record = new ArrayList<Integer>(max);


  // Going write in this file

  for ( int i = 2; i <= max; i++ )
  {
   if ( obj.isPrime ( i ) )
    System.out.println ( i );
    record.add(i);

    } 
   for(Integer str: record){
   writer1.write(str);
    }

    }catch(IOException e){

    }finally{writer1.close();}
    }

   }

And this is what the text file looks like:

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\
  • 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-10T09:58:41+00:00Added an answer on June 10, 2026 at 9:58 am

    There’s two issues. One is the code point issue Marko points out above.

    The other is

    if ( obj.isPrime ( i ) )
        System.out.println ( i );
        record.add(i);
    

    Your if statement only covers the system.out.println – it doesn’t also cover the record.add since it has no brackets.

    Consider this instead:

    if ( obj.isPrime ( i ) ) { // notice the curly bracket
        System.out.println ( i );
        record.add(i);
    } // notice the curly bracket
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a small program that saves a int value into a text file,
Bassicly im creating a program that reads information from an xml file into a
I'm creating a program using C# (in ASP.NET environment) that makes an XML file.
I am creating a program that encrypts/hashs messages I have a list that contains
I'm creating a program where I save information in a XML, in that XML
I'm creating a program that authenticates from a before it runs. I also want
I am currently creating a program that dynamically adds Image to a dockPanel every
I am creating a program that communicates with a PHP script on a web
I am creating a client program that talks to a server (which I programmed).
When creating DLL files for a program that already exists, is it customary to

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.