Question

I need to remove the first line of code (Summer 2017 Temperature ?°C vs Ice Cream...

I need to remove the first line of code (Summer 2017 Temperature ?°C vs Ice Cream Sales) and put the rest in a array where the number before the comma is the index and the number after is the value at said index.

The array should be of size 30.

Code:

import java.util.Scanner;
public class Q3 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String[] chart = new String[30];
String input1= "";
String inputTitle = "";
while(!(input1.equals("end"))){
input1= sc.nextLine();
System.out.println(input1 + " ");
  }
}
}

Current Output:

Summer 2017 Temperature ?°C vs Ice Cream Sales
14,200
16,300
11,100
15,300
18,400
22,500
19,400
25,600
23,500
18,400
17,400
end

Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for posting the question, we are glad to help you. Here is how you can avoid the first line and continue reading the second line and onwards and store the value at the given index.

________________________________________________________________________________________________

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Q3 {

    private static final String filePath="F:\\temp.txt";

    public static void main(String[] args){
        File file = new File(filePath);
        int[] chart = new int[30];
        try  {
            Scanner sc = new Scanner(file);
// read the first line and print it out
            System.out.println(sc.nextLine());
// from the second line onwards we start the while loop 
            while (sc.hasNext()){
                String readLine=sc.nextLine();
// if we encounter the line as end then we break out of the while loop
                if(readLine.equals("end")){
                    break;
                }else{
                    // split the line by comma, returns array of strings
                    String[] tokens = readLine.split(",");

                    int index = Integer.parseInt(tokens[0]);
                    int value = Integer.parseInt(tokens[1]);

                    chart[index]=value;
                }
            }
            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
// prints the value of the int[] array and see the values are stored at the given index
// as given in the file
        for(int index=0; index<chart.length;index++){
           System.out.println("Index: "+index+" Value: "+chart[index]);
        }

    }
}

______________________________________________________________________________________________

image screenshot

囚HomeworkLib [C:\Usersus lde Project Chegg] _ srcQ3.java [Chegg1-1 telliJ IDEA Eile Edit View Novigate Code Analyze Refactor Build Run Iools VCS Window Help C:\Program Files\Java\jdk-11\bin\ exe -javaagent :C:\Prograr Files JetBrains\IntelliJ IDEA Community Edition 2018.2.4\lib\ideart-jar=53836:C:\Program Files\JetBrains\Intell Activate Windows Proccss finished with exit code 0 仨6: TODO Terminal -0: Messages 4: Run Compilation completed successfully in 2 s 38 ms (4 minutes ago) 1 Event Log UTF-8# 35:1 CRLF:

thank you : )

Add a comment
Know the answer?
Add Answer to:
I need to remove the first line of code (Summer 2017 Temperature ?°C vs Ice Cream...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT