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

  • Home
  • SEARCH
  • 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 9135829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:52:26+00:00 2026-06-17T08:52:26+00:00

New to java here. I’m trying to split a string into variables in an

  • 0

New to java here. I’m trying to split a string into variables in an object. As the title says the last field in the string splits together with the first field.

A line from my txt file all the other lines are identical.

Here’s the output:

Rolling Stone#Jann Wenner#Bi-Weekly#Boston#9000
Rolling Stone#Jann Wenner#Bi-Weekly#Philadelphia#8000
Rolling Stone#Jann Wenner#Bi-Weekly#London#10000
The Economist#John Micklethwait#Weekly#New York#42000
The Economist#John Micklethwait#Weekly#Washington#29000
Nature#Philip Campbell#Weekly#Pittsburg#4000
Nature#Philip Campbell#Weekly#Berlin#6000

    Exception in thread "main" java.lang.NumberFormatException: For input string: "9000 Rolling Stone"

9000 should be an integer value. And the last index jumps onto the next line because there’s no # what should I do?

I believe this much code is sufficient

    static ArrayList<String> al = new ArrayList<String>(); // Consists of lines of the text file
    static ArrayList<Magazine> bl = new ArrayList<Magazine>(); // Consists of Magazine objects

for (int i = 0; i < al.size(); i++) {
                String result[] = al.get(i).split("\\#");
                for (int j = 0; j < result.length; j++) {
                    System.out.println(result[0] + "1 " + result[1] + "2 " +  result[2] + "3 " +  result[3] + "4 "+  result[4]);
                    int num = Integer.parseInt(result[4]);

I’m having a spider sense somewhere close split(“\#”) but i dont know what…

Pastebin: http://pastebin.com/Vp9T6aSd

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Magazine {

    private String MagazineName;
    private String Publisher;
    private String Frequency;
    private String City;
    private String objectName = "mg" + loopCount;
    private int Distribution;
    private static int loopCount = 0;

    static ArrayList<String> al = new ArrayList<String>(); // Consists of lines of the text file
    static ArrayList<Magazine> bl = new ArrayList<Magazine>(); // Consists of Magazine objects

    private static void readData() throws FileNotFoundException {
        java.io.File file = new java.io.File(
                "/Users/henrydang/Desktop/Zines.txt");
        Scanner sc = new Scanner(file);
        while (sc.hasNext()) {
            sc.useDelimiter("\\n");
            String line = sc.next();
            System.out.println(line);
            al.add(line);
            Magazine objectName;

            for (int i = 0; i < al.size(); i++) {
                String result[] = al.get(i).split("#");
                for (int j = 0; j < result.length; j++) {
                    System.out.println(result[0] + "1 " + result[1] + "2 " +  result[2] + "3 " +  result[3] + "4 "+  result[4]);
                    int num = Integer.parseInt(result[4]);

                    objectName = new Magazine();
                    objectName.setMagazine(result[0]);
                    objectName.setPublisher(result[1]);
                    objectName.setFrequency(result[2]);
                    objectName.setCity(result[3]);
                    objectName.setDistribution(num);
                    bl.add(objectName);             
                }

            }
            loopCount++;
            sc.close();

        }
    }

    public Magazine() {

    }

    public void setMagazine(String name) {
        this.MagazineName = name;
    }

    public void setPublisher(String name) {
        this.Publisher = name;
    }

    public void setFrequency(String name) {
        this.Frequency = name;
    }

    public void setCity(String name) {
        this.City = name;
    }

    public void setDistribution(int num) {
        this.Distribution = num;
    }

    public String getMagazine() {
        return MagazineName;
    }

    public String getPublisher() {
        return Publisher;
    }

    public String getFrequency() {
        return Frequency;
    }

    public String getCity() {
        return City;
    }

    public int getDistribution() {
        return Distribution;
    }

    public static void main(String args[]) throws FileNotFoundException {
        readData();
    }

}

EDIT: more information
SOLVED: The text file had endings with /r/n instead of “/n”

  • 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-17T08:52:26+00:00Added an answer on June 17, 2026 at 8:52 am

    So Henry…

    A couple of problems with your code:

    1. sc.close() was being called inside the while (sc.hasNext()){} loop.
    2. sc.setDelimiter() being called inside the sc.hasNext() is sort of very shady but it works I guess as you call .next() after setting the delimiter.
    3. We have figured out that the problem was with the input file itself and not your code per-se. Always double check your files if you are writing some platform speciic code (such as with your \n delimiter).

    And here is your code with the proper / ideal way I’d use a scanner for your code:

    private static void readData() throws FileNotFoundException {  
      java.io.File file = new java.io.File("Zines.txt");  
      Scanner sc = new Scanner(file);  
      while (sc.hasNextLine()) {  
        String line = sc.nextLine();  
      }  
      sc.close();  
    }
    

    Hope it all helped!

    Edit: Ignore the comments conversation below. We are not mad, I redid the answer.

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

Sidebar

Related Questions

Hi i am trying to parse a string into a java.sql.date Here is what
I'm trying to split a java string in a Rhino javascript program var s
here is the code String DateOfBirth[]=strDOB.split(/); Date dateOfBirth = new Date(); dateOfBirth.setYear(Integer.parseInt(DateOfBirth[2].trim())); dateOfBirth.setMonth(Integer.parseInt(DateOfBirth[1].trim())); dateOfBirth.setDate(Integer.parseInt(DateOfBirth[0].trim()));
I'm new here and kinda new to java. I've encountered a problem. I have
hi I am new here and new in Java programming as well actually my
I'm new to Java and this is my first post on here so hopefully
Possible Duplicate: Programming java to determine a symmetrical word am new here, but I
Here is a java code for breadth-first travel: void breadthFirstNonRecursive(){ Queue<Node> queue = new
I am trying to read a config file based on the code here: http://www.opencodez.com/java/read-config-file-in-java.htm
When I execute following code in scala REPL console: java.util.Collections.max(new java.util.ArrayList[String]()) NoSuchMethodError exception is

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.