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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:15:54+00:00 2026-05-31T05:15:54+00:00

I have an array with types and numbers and when printing this is the

  • 0

I have an array with types and numbers and when printing this is the outcome:

    car:1 car:2 car:3 boat:1 boat:2 boat:3 plane:1 plane:2 plane:3

I am looking for a way to determine when the type changes, and then make a new line. which means to print a “\n” after car:3 (and boat:3) so all the vehicles are on their own row.

I am printing all these items with a for-loop like this:

 for(Types filename: TypeTable)
    {
        Scanner s;
        s = new Scanner(filename.toString());
        while (s.hasNext())
        {
            System.out.print(s.nextLine()+ " ");
        }

and I guess I am in need for some local loop and to save the first type in some temp variable and in a loop print a newline when it changes, but I am kinda stuck.

edit2:

after taking a break and then coming back i managed to fix the problem, and even without having a blank line in beginning. the problem was that i had to define oldTransport in main-class 🙂 ofc you couldnt have known how my structure was. thank you hovercraft of eel 🙂

  • 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-05-31T05:15:54+00:00Added an answer on May 31, 2026 at 5:15 am

    Rather than printing the line via System.out.print(...) get the line and put it into a variable. Split it via the String#split(...) method, and compare the first part obtained (the String in the [0] spot of the array obtained from split) with the previous String’s first part (that was also saved to a variable). If different, make a new line before printing the line out.

    Also, if you are going to extract a nextLine() from the scanner, check for a hasNextLine(), not hasNext().

    In pseudocode

    String oldTransportation gets assigned ""
    while the scanner has a next line
       String variable line gets scanner's next line
       String array called tokens gets this line split on ":"
       String newTransportation gets the [0] item in the tokens array
       Check if newTransportation equals(...) oldTransportation, 
           if different, call System.out.println().
       print the line variable String
       oldTransportation gets assigned newTransportation
    end while scanner...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to have an associative array with arbitrary value types
I'm looking for a kind of array data-type that can easily have items added,
I have an array which contains 2 types of items (lets call them type
i have an array of card types that looks something like this var cards
i have an array which contains only 2 types of numbers(x and x-1) eg:-
I have two big numbers (type int) which are stored in array with a
I have an array of uint-types in C#, After checking if the program is
I have an object whose value may be one of several array types like
If I have a variant array which holds nothing but simple types, and possible
Here's an example of what I mean: I have an array: array('type'=>'text', 'class'=>'input', 'name'=>'username',

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.